1123 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
题意:
根据给出的插入序列构建一棵AVL Tree,然后按照层次遍历输出。
思路:
1.构建AVL Tree时插入结点会遇到四种情况。
右旋:
|
左旋:
|
---|---|
先右旋后左旋:
|
先左旋后右旋:
|
求某一个结点的左右孩子深度时,可以用递归函数求解。最后判断是不是完全二叉树,可以先记录第一个缺失孩子的节点,如果该节点后又出现了有孩子的节点则,不是完全二叉树。
Code:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 typedef struct Node* node; 6 7 struct Node { 8 int val; 9 node left; 10 node right; 11 Node(int v) { 12 val = v; 13 left = NULL; 14 right = NULL; 15 } 16 }; 17 18 node rightRotate(node root) { 19 node temp = root->left; 20 root->left = temp->right; 21 temp->right = root; 22 return temp; 23 } 24 25 node leftRotate(node root) { 26 node temp = root->right; 27 root->right = temp->left; 28 temp->left = root; 29 return temp; 30 } 31 32 node leftRightRotate(node root) { 33 root->left = leftRotate(root->left); 34 return rightRotate(root); 35 } 36 37 node rightLeftRotate(node root) { 38 root->right = rightRotate(root->right); 39 return leftRotate(root); 40 } 41 42 int findHeight(node root) { 43 if (root == NULL) return 0; 44 int l = findHeight(root->left); 45 int r = findHeight(root->right); 46 return max(l, r) + 1; 47 } 48 49 void insertNode(int v, node& root) { 50 if (root == NULL) { 51 root = new Node(v); 52 } else if (v < root->val) { 53 insertNode(v, root->left); 54 int l = findHeight(root->left); 55 int r = findHeight(root->right); 56 if (abs(r - l) >= 2) { 57 if (v < root->left->val) { 58 root = rightRotate(root); 59 } else { 60 root = leftRightRotate(root); 61 } 62 } 63 } else { 64 insertNode(v, root->right); 65 int l = findHeight(root->left); 66 int r = findHeight(root->right); 67 if (abs(r - l) >= 2) { 68 if (v > root->right->val) { 69 root = leftRotate(root); 70 } else { 71 root = rightLeftRotate(root); 72 } 73 } 74 } 75 } 76 77 bool levelTravel(node root) { 78 queue<node> que; 79 que.push(root); 80 bool isCompleteBinaryTree = true; 81 bool isFirst = true; 82 bool isStart = false; 83 cout << root->val; 84 while (!que.empty()) { 85 node temp = que.front(); 86 if (isStart) 87 cout << " " << temp->val; 88 else 89 isStart = true; 90 que.pop(); 91 if (temp->left) { 92 que.push(temp->left); 93 if (!isFirst) isCompleteBinaryTree = false; 94 } else if (isFirst) { 95 isFirst = false; 96 } 97 if (temp->right) { 98 que.push(temp->right); 99 if (!isFirst) isCompleteBinaryTree = false; 100 } else if (isFirst) { 101 isFirst = false; 102 } 103 } 104 cout << endl; 105 return isCompleteBinaryTree; 106 } 107 108 int main() { 109 int n, t; 110 cin >> n; 111 node root = NULL; 112 while (n--) { 113 cin >> t; 114 insertNode(t, root); 115 } 116 if (levelTravel(root)) 117 cout << "YES" << endl; 118 else 119 cout << "NO" << endl; 120 return 0; 121 }
相关文章
- The type javax.swing.JComponent cannot be resolved. It is indirectly referenced from required .class files
- 【RAC】Diskgroup shows offline after restart even it is mounted in ASM instance
- [RxJS] Reactive pattern: Passing Observable and trigger side effect until it complete
- tesseract is not installed or it's not in your PATH
- 有一些专业的it提供商,为什么企业还是不用他们?比如尘封做crm的,但是不能和我们的业务不能紧密结合,
- 用Ossim管理IT资产(视频)
- ERROR: This jdwp native library will not work with this VM‘s version of JVMTI (11.0.0), it needs JVM
- SAP CRM One Order object type in line item - when it is filled
- 【2017 Multi-University Training Contest - Team 7】Just do it
- Why close date in S2 view is not displayed correctly after clicking it
- 成功解决building ‘snappy._snappy‘ extension error: Microsoft Visual C++ 14.0 is required. Get it with “B
- error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“:
- 软件技术专业-就业提示(IT类详细岗位分化)
- 已解决because it does not have a ‘pyproject.toml’ and the ‘wheel’ package is not installed. pip 23.1 wi
- IT日语
- HDU 1325 Is It A Tree?
- 阿里程序员回老家被哥们吐槽,破IT就破IT,还阿里巴巴
- “冷转码”频发背后:一流院校生为何多走弯路也要转行IT业?
- POJ1308 Is It A Tree?
- PAT 1123 Is It a Complete AVL Tree C++版
- 小白的测试人生(三)小白如何进入IT行业及如何选择培训机构