PAT (Advanced Level) 1123 Is It a Complete AVL Tree (30分)
- 构建一颗完全二叉树(相关代码有注释)
- 通过BFS实现层次遍历(亦可与获取完全二叉树合并,在一个DFS中实现)
- 通过DFS这棵树获取每个结点在完全二叉树的下标 i n d e x index index,检查该完全二叉树是否有空位,即可做出判断
- 在使用指针进行操作的时候,一定特别注意指针为空的情况!!!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
using namespace std;
//1.50 14:00
#define LL long long int
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 10;
int n; int flag;
int Ans[maxn];// record the level traversal answer!
int id[maxn];// check if it is a full BT
int k;// count for the number of ans;(BFS)
struct Node {
int data;
Node* left;
Node* right;
};
int Get_Height(Node* root) {
if (root == NULL) return 0;// WA点!
int Ans1 = 1; int Ans2 = 1;//相比于其他程序 额外申请了堆栈。
if (root->left != NULL) {
Ans1 += Get_Height(root->left);
}
if (root->right != NULL) {
Ans2 += Get_Height(root->right);
}
return max(Ans1, Ans2);
}
Node* LeftRotation(Node* root) { //左旋一棵子树
Node *temp; temp = root->right;
root->right = temp->left;
temp->left = root;
return temp;// temp is new_root;
}
Node* RightRotaion(Node *root) { //右旋一颗子树
Node *temp; temp = root->left;
root->left = temp->right;
temp->right = root;
return temp;// temp is new_root
}
Node* insert(Node* &root, int data) {
if (root == NULL) {
root = new Node();
root->data = data;
root->left = root->right = NULL;
return root;
}
if (data < root->data) {
root->left = insert(root->left, data);
if (Get_Height(root->left) - Get_Height(root->right) == 2) {
if (Get_Height(root->left->left) - Get_Height(root->left->right) == 1) { // LL
root = RightRotaion(root);
}
else if (Get_Height(root->left->right) - Get_Height(root->left->left) == 1) { // LR
root->left = LeftRotation(root->left);
root = RightRotaion(root);
}
}
}
else { // data >= root->data
root->right = insert(root->right, data);
if (Get_Height(root->right) - Get_Height(root->left) == 2) {
if (Get_Height(root->right->right) - Get_Height(root->right->left) == 1) { // RR
root = LeftRotation(root);
}
else if (Get_Height(root->right->left) - Get_Height(root->right->right)) { // RL
root->right = RightRotaion(root->right);
root = LeftRotation(root);
}
}
}
return root;
}
void Level_Traversal(Node* root) { //层次遍历
queue<Node*> Q;
Q.push(root);
Ans[++k] = root->data;
while (!Q.empty()) {
Node* top = Q.front(); Q.pop();
if (top->left != NULL) {
Ans[++k] = top->left->data;
Q.push(top->left);
}
if (top->right != NULL) {
Ans[++k] = top->right->data;
Q.push(top->right);
}
}
}
void dfs(Node *root, int index) { //标记完全二叉树序号
id[index] = 1;
if (root->left != NULL) {
dfs(root->left, 2 * index);
}
if (root->right != NULL) {
dfs(root->right, 2 * index + 1);
}
}
int main() {
flag = 1;
Node* root; root = NULL;
cin >> n;
for (int i = 1; i <= n; i++) {
int data;
cin >> data;
root = insert(root, data);
}
//cout << "OK" << endl;
dfs(root, 1);
Level_Traversal(root);
for (int i = 1; i <= k; i++) {
if (i ^ 1) cout << " ";
cout << Ans[i];
if (i == k) cout << endl;
}
for (int i = 1; i <= n; i++) { // 顺序遍历完全二叉树,如果有空位,则非完全二叉树
if (id[i] == 0) {
flag = 0; break;
}
}
if (flag) puts("YES");
else puts("NO");
return 0;
}