PAT甲级1123 Is It a Complete AVL Tree【AVL树】

题目https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336

题意:

给定n个树,依次插入一棵AVL树,按照层序遍历输出,最后判断这棵AVL树是不是完全二叉树。

思路:

这道题过段时间还要再来手搓一发。AVL模板要记住。

判断是不是完全二叉树的话只用看,如果有一个节点儿子是空,而他之后又出现了至少有一个儿子的节点的话,就不是完全二叉树。【蛮巧妙的】

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<map>
  4 #include<set>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<vector>
  9 #include<cmath> 
 10 #include<stack>
 11 #include<queue>
 12 
 13 #define inf 0x7fffffff
 14 using namespace std;
 15 typedef long long LL;
 16 typedef pair<string, string> pr;
 17 
 18 int n;
 19 const int maxn = 25;
 20 vector<int>level[maxn];
 21 typedef struct AvlNode{
 22     int val;
 23     AvlNode *left;
 24     AvlNode *right;
 25     int height;
 26 }*AvlTree, AvlNode;
 27 
 28 int Max(AvlTree a, AvlTree b)
 29 {    
 30     int x = 0, y = 0;
 31     if(a)x = a->height;
 32     if(b)y = b->height;
 33     if(x > y)return x;
 34     else return y;
 35 }
 36 
 37 AvlTree singleRotateWithRight(AvlTree T)
 38 {
 39     AvlTree L = T->left;
 40     T->left = L->right;
 41     L->right = T;
 42     T->height = Max(T->left, T->right) + 1;
 43     L->height = Max(L->left, L->right) + 1;
 44     return L;
 45 } 
 46 
 47 AvlTree singleRotateWithLeft(AvlTree T)
 48 {
 49     AvlTree R = T->right;
 50     T->right = R->left;
 51     R->left = T;
 52     T->height = Max(T->left, T->right) + 1;
 53     R->height = Max(R->left, R->right) + 1;
 54     return R;
 55 }
 56 
 57 AvlTree doubleRotateWithLeft(AvlTree T)
 58 {
 59     T->left = singleRotateWithLeft(T->left);
 60     return singleRotateWithRight(T);
 61 }
 62 
 63 AvlTree doubleRotateWithRight(AvlTree T)
 64 {
 65     T->right = singleRotateWithRight(T->right);
 66     return singleRotateWithLeft(T);
 67 }
 68 
 69 AvlTree Insert(AvlTree T, int val)
 70 {
 71     if(T == NULL){
 72         T = (AvlNode *)malloc(sizeof(struct AvlNode));
 73         if(T){
 74             T->val = val;
 75             T->left = NULL;
 76             T->right = NULL;
 77             T->height = 0;
 78         }
 79     }
 80     else if(val < T->val){
 81         T->left = Insert(T->left, val);
 82         int l = 0, r = 0;
 83         if(T->left){
 84             l = T->left->height;
 85         }
 86         if(T->right){
 87             r = T->right->height;
 88         }
 89         if(l - r == 2){
 90             if(val < T->left->val){
 91                 T = singleRotateWithRight(T);
 92             }
 93             else{
 94                 T = doubleRotateWithLeft(T);    
 95             } 
 96         }
 97     }
 98     else if(val > T->val){
 99         T->right = Insert(T->right, val);
100         int l = 0, r = 0;
101         if(T->left)l = T->left->height;
102         if(T->right)r = T->right->height;
103         if(r - l == 2){
104             if(val > T->right->val){
105                 T = singleRotateWithLeft(T);
106             }
107             else{
108                 T = doubleRotateWithRight(T);
109             }
110         }
111     }
112     T->height = Max(T->left, T->right) + 1;
113     return T;
114 }
115 
116 bool after = false, iscomplete = true;
117 bool first = false;
118 void levelOrder(AvlTree T)
119 {
120     queue<AvlTree>que;
121     que.push(T);
122     while(!que.empty()){
123         AvlTree now = que.front();que.pop();
124         if(first)printf(" ");
125         else first = true;
126         printf("%d", now->val);
127         level[now->height].push_back(now->val);
128         if(now->left){
129             if(after)iscomplete = false;
130             que.push(now->left);
131         }
132         else{
133             after = 1;
134         }
135         if(now->right){
136             if(after)iscomplete = false;
137             que.push(now->right);
138         }
139         else{
140             after = 1;
141         }
142     }
143 }
144 
145 int main()
146 {
147     scanf("%d", &n);
148     AvlTree Tree = NULL;
149     for(int i = 0; i < n; i++){
150         int x;
151         scanf("%d", &x);
152         Tree = Insert(Tree, x);
153     }
154     levelOrder(Tree);
155     printf("\n");
156     if(iscomplete)printf("YES\n");
157     else printf("NO\n");
158     return 0;
159 }

 

posted @ 2019-04-10 16:58  wyboooo  阅读(187)  评论(0编辑  收藏  举报