A1123. Is It a Complete AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<= 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print "YES" if the tree is complete, or "NO" if not.
Sample Input 1:
5 88 70 61 63 65
Sample Output 1:
70 63 88 61 65 YES
Sample Input 2:
8 88 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68 NO
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 using namespace std; 7 typedef struct NODE{ 8 struct NODE* lchild, *rchild; 9 int data, lev; 10 }node; 11 int N, cnt = 0; 12 int height(node* root){ 13 if(root == NULL) 14 return 0; 15 else return root->lev; 16 } 17 void update(node* root){ 18 root->lev = max(height(root->lchild), height(root->rchild)) + 1; 19 } 20 void L(node* &root){ 21 node* temp = root; 22 root = root->rchild; 23 temp->rchild = root->lchild; 24 root->lchild = temp; 25 update(temp); 26 update(root); 27 } 28 void R(node* &root){ 29 node* temp = root; 30 root = root->lchild; 31 temp->lchild = root->rchild; 32 root->rchild = temp; 33 update(temp); 34 update(root); 35 } 36 void insert(node* &root, int x){ 37 if(root == NULL){ 38 root = new node; 39 root->lchild = NULL; 40 root->rchild = NULL; 41 root->data = x; 42 root->lev = 1; 43 return; 44 } 45 if(x <= root->data){ 46 insert(root->lchild, x); 47 update(root); 48 if(abs(height(root->lchild) - height(root->rchild)) == 2){ 49 if(height(root->lchild->lchild) - height(root->lchild->rchild) == 1){ 50 R(root); 51 }else if(height(root->lchild->lchild) - height(root->lchild->rchild) == -1){ 52 L(root->lchild); 53 R(root); 54 } 55 } 56 }else{ 57 insert(root->rchild, x); 58 update(root); 59 if(abs(height(root->lchild) - height(root->rchild)) == 2){ 60 if(height(root->rchild->rchild) - height(root->rchild->lchild) == 1){ 61 L(root); 62 }else if(height(root->rchild->rchild) - height(root->rchild->lchild) == -1){ 63 R(root->rchild); 64 L(root); 65 } 66 } 67 } 68 } 69 70 int levelOrder(node* root){ 71 int tag = 1, prt = 0; 72 queue<node*> Q; 73 Q.push(root); 74 while(Q.empty() == false){ 75 node* temp = Q.front(); 76 Q.pop(); 77 cnt++; 78 if(temp == NULL){ 79 if(cnt < N + 1) 80 tag = 0; 81 }else{ 82 prt++; 83 if(prt == N) 84 printf("%d\n", temp->data); 85 else printf("%d ", temp->data); 86 Q.push(temp->lchild); 87 Q.push(temp->rchild); 88 } 89 } 90 return tag; 91 } 92 int main(){ 93 scanf("%d", &N); 94 int num; 95 node* root = NULL; 96 for(int i = 0; i < N; i++){ 97 scanf("%d", &num); 98 insert(root, num); 99 } 100 int isCom = levelOrder(root); 101 if(isCom == 1) 102 printf("YES\n"); 103 else printf("NO\n"); 104 cin >> N; 105 return 0; 106 }
总结:
1、按插入顺序建立平衡二叉树,然后再判断该树是否是完全二叉树。
2、建立平衡二叉树: 在左子树插入后,先更新根节点高度,再求平衡因子。求平衡因子判断平衡应判断左减右是否等于2,而不是绝对值。
3、左旋右旋共三步。旋转完成之后必须更新temp和root的高度,由于temp会成为root的子树,所以先更新temp高度,再更新root。
4、判断完全二叉树: 将null节点也加入队列。设置计数器记录访问节点个数,当访问时遇到空节点看计数器是否大于N,如果否,则不是完全二叉树。