PAT_A1123#Is It a Complete AVL Tree
Posted blue-lin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
- 愣是用一个变量搞定了输出和判断0,0
Code:
1 /* 2 Data: 2019-06-03 19:58:23 3 Problem: PAT_A1123#Is It a Complete AVL Tree 4 AC: 61:05 5 6 题目大意: 7 由插入序列构造一棵AVL树,输出层次遍历并判断是否为一棵完全二叉树 8 */ 9 10 #include<cstdio> 11 #include<algorithm> 12 #include<queue> 13 using namespace std; 14 struct node 15 16 int data,height; 17 node *lchild, *rchild; 18 ; 19 20 int GetH(node *root) 21 22 if(root == NULL) 23 return 0; 24 else 25 return root->height; 26 27 28 void UpH(node* &root) 29 30 root->height=max(GetH(root->lchild),GetH(root->rchild))+1; 31 32 33 int GetF(node *root) 34 35 return GetH(root->lchild)-GetH(root->rchild); 36 37 38 void LeftRot(node* &root) 39 40 node *temp = root->rchild; 41 root->rchild = temp->lchild; 42 temp->lchild = root; 43 UpH(root); 44 UpH(temp); 45 root = temp; 46 47 48 void RightRot(node* &root) 49 50 node *temp = root->lchild; 51 root->lchild = temp->rchild; 52 temp->rchild = root; 53 UpH(root); 54 UpH(temp); 55 root = temp; 56 57 58 void Insert(node* &root, int x) 59 60 if(root == NULL) 61 62 root = new node; 63 root->data = x; 64 root->height=1; 65 root->lchild=root->rchild=NULL; 66 return; 67 68 if(x < root->data) 69 70 Insert(root->lchild, x); 71 UpH(root); 72 if(GetF(root) == 2) 73 74 if(GetF(root->lchild) == 1) 75 RightRot(root); 76 else 77 LeftRot(root->lchild); 78 RightRot(root); 79 80 81 82 else 83 84 Insert(root->rchild, x); 85 UpH(root); 86 if(GetF(root) == -2) 87 88 if(GetF(root->rchild) == -1) 89 LeftRot(root); 90 else 91 RightRot(root->rchild); 92 LeftRot(root); 93 94 95 96 97 98 void Travel(node *root) 99 100 int ans=0; 101 queue<node*> q; 102 q.push(root); 103 printf("%d", root->data); 104 while(!q.empty()) 105 106 root = q.front(); 107 q.pop(); 108 if(root) 109 if(ans!=0) printf(" %d", root->data); 110 else ans=1; 111 q.push(root->lchild); 112 q.push(root->rchild); 113 114 else 115 ans++; 116 while(!q.empty()) 117 root = q.front(); 118 if(root) break; 119 else q.pop(); 120 121 if(ans==2 && q.empty()) 122 printf("\nYES\n"); 123 return; 124 125 126 127 printf("\nNO\n"); 128 129 130 131 int main() 132 133 #ifdef ONLINE_JUDGE 134 #else 135 freopen("Test.txt", "r", stdin); 136 #endif 137 138 int n,x; 139 scanf("%d", &n); 140 node *root = NULL; 141 for(int i=0; i<n; i++) 142 143 scanf("%d", &x); 144 Insert(root, x); 145 146 Travel(root); 147 148 return 0; 149
以上是关于PAT_A1123#Is It a Complete AVL Tree的主要内容,如果未能解决你的问题,请参考以下文章
1123 Is It a Complete AVL Tree (30 分)难度: 难 / 平衡树 未完成