PAT_A1123#Is It a Complete AVL Tree
Source:
Description:
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, orNO
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
Keys:
- 二叉树的建立
- 二叉树的遍历
- 完全二叉树(Complete Binary Tree)
- 平衡二叉树(Self-balancing Binary Search Tree,AVL tree)
Attention:
- 判断完全二叉树,while的固定写法
- Rotation中,先Update root,再Update temp,否则会影响结果,注意
Code:
1 /* 2 Data: 2019-06-24 15:36:45 3 Problem: PAT_A1123#Is It a Complete AVL Tree 4 AC: 35:46 5 6 题目大意: 7 由插入序列构造一棵AVL树,输出层次遍历并判断是否为一棵完全二叉树 8 9 基本思路: 10 构造平衡二叉树, 11 中序遍历并判断是否为完全二叉树 12 */ 13 #include<cstdio> 14 #include<queue> 15 #include<algorithm> 16 using namespace std; 17 struct node 18 { 19 int data; 20 int height; 21 node *lchild, *rchild; 22 }; 23 24 int GetHeight(node *root) 25 { 26 if(root == NULL) 27 return 0; 28 else 29 return root->height; 30 } 31 32 int GetBalanceFactor(node *root) 33 { 34 return GetHeight(root->lchild) - GetHeight(root->rchild); 35 } 36 37 void UpdataHeight(node *&root) 38 { 39 root->height = max(GetHeight(root->lchild),GetHeight(root->rchild))+1; 40 } 41 42 void LeftRotation(node *&root) 43 { 44 node *temp = root->rchild; 45 root->rchild = temp->lchild; 46 temp->lchild = root; 47 UpdataHeight(root); 48 UpdataHeight(temp); 49 root = temp; 50 } 51 52 void RightRotation(node *&root) 53 { 54 node *temp = root->lchild; 55 root->lchild = temp->rchild; 56 temp->rchild = root; 57 UpdataHeight(root); 58 UpdataHeight(temp); 59 root = temp; 60 } 61 62 void Insert(node *&root, int x) 63 { 64 if(root == NULL) 65 { 66 root = new node; 67 root->data = x; 68 root->height=1; 69 root->lchild = root->rchild = NULL; 70 } 71 else if(x < root->data) 72 { 73 Insert(root->lchild, x); 74 UpdataHeight(root); 75 if(GetBalanceFactor(root) == 2) 76 { 77 if(GetBalanceFactor(root->lchild) == 1) 78 RightRotation(root); 79 else 80 { 81 LeftRotation(root->lchild); 82 RightRotation(root); 83 } 84 } 85 } 86 else 87 { 88 Insert(root->rchild, x); 89 UpdataHeight(root); 90 if(GetBalanceFactor(root) == -2) 91 { 92 if(GetBalanceFactor(root->rchild) == -1) 93 LeftRotation(root); 94 else 95 { 96 RightRotation(root->rchild); 97 LeftRotation(root); 98 } 99 } 100 } 101 } 102 103 int IsComplete(node *root, int n) 104 { 105 queue<node*> q; 106 q.push(root); 107 int cnt=0, ans=1; 108 while(!q.empty()) 109 { 110 root = q.front(); 111 q.pop(); 112 if(root) 113 { 114 printf("%d%c", root->data,++cnt==n?'\n':' '); 115 q.push(root->lchild); 116 q.push(root->rchild); 117 } 118 else 119 { 120 if(cnt==n) 121 break; 122 else 123 { 124 ans=0; 125 while(!q.empty()) 126 { 127 root = q.front(); 128 if(root) break; 129 else q.pop(); 130 } 131 } 132 } 133 } 134 return ans; 135 } 136 137 138 int main() 139 { 140 #ifdef ONLINE_JUDGE 141 #else 142 freopen("Test.txt", "r", stdin); 143 #endif // ONLINE_JUDGE 144 145 int n,x; 146 node *root = NULL; 147 scanf("%d", &n); 148 for(int i=0; i<n; i++) 149 { 150 scanf("%d", &x); 151 Insert(root, x); 152 } 153 if(IsComplete(root, n)) 154 printf("YES"); 155 else 156 printf("NO"); 157 158 return 0; 159 }