PAT (Advanced Level) Practise 1123Is It a Complete AVL Tree (30)

来源:互联网 发布:环境保护实用数据手册 编辑:程序博客网 时间:2024/06/05 15:29

1123. Is It a Complete AVL Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
588 70 61 63 65
Sample Output 1:
70 63 88 61 65YES
Sample Input 2:
888 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68NO
真没想到pat会出这种题,还好这场时间撞上了cf,没报。这个题和之前的1066一样,就是把avl树的插入写上一遍,这两天稍微理了一下,补一个带点注释的代码。
#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define ms(x,y) memset(x,y,sizeof(x))#define rep(i,j,k) for(int i=j;i<=k;i++)#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])#define inone(x) scanf("%d",&x)#define intwo(x,y) scanf("%d%d",&x,&y)#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)#define lson x<<1,l,mid#define rson x<<1|1,mid+1,rconst int N = 1e3 + 10;const int INF = 0x7FFFFFFF;int n, x;int rt; //根节点int sz; //总结点个数,用于建立新节点int ch[N][2]; //记录左右儿子的标号int H[N];  //记录该节点向下最远的高度,用于维持avl数的平衡int A[N];  //记录节点的值,维持插入需要int F[N];  //记录节点的父亲节点标号int G[N];  /*记录是否为完全二叉树,方法是对于原来树上的节点重新标号,判断是否有超过n的节点,有就不是完全二叉树*/int node(int x, int f){A[sz] = x; F[sz] = f; H[sz] = 1; ms(ch[sz], 0); return sz++;}//数组模拟,建立新节点,父亲节点为f,值为x,此时高度为1,没有子节点。void Count(int x){H[x] = max(H[ch[x][0]], H[ch[x][1]]) + 1;}//重新计算该节点高度void rotate(int x, int k){int y = F[x]; ch[y][!k] = ch[x][k]; if (ch[x][k]) F[ch[x][k]] = y;if (F[y]) ch[F[y]][y == ch[F[y]][1]] = x;F[x] = F[y]; F[y] = x; ch[x][k] = y;Count(y); Count(x);}//旋转操作,x为节点编号,k为旋转类型,k=1时为右旋,k=0是为左旋int change(int x){for (int i = F[F[x]]; i; i = F[F[x]]){Count(F[x]); Count(i);if (abs(H[ch[i][0]] - H[ch[i][1]]) > 1){int y = x == ch[F[x]][0], z = F[x] == ch[i][0];y^z ? (rotate(x, y), rotate(x, z)) : rotate(F[x], z);}else x = F[x];}while (F[x]) { Count(x); x = F[x]; }return x;}/*调整过程,根据avl树的性质,如果某个节点的左右高度差超过1,那么进行调整需要注意的是,avl树的旋转都是三个节点之间的,x,F[x],F[F[x]]并且分为两种,即x和F[x]都是同方向的,那么将F[x]向上旋转即可。如果是不同方向,那么将x连续向上旋转两次。*/void insert(int &rt, int x){if (!rt) { rt = node(x, 0); return; }for (int i = rt;; i = ch[i][A[i] < x]){if (ch[i][A[i] < x]) continue;ch[i][A[i] < x] = node(x, i); rt = change(ch[i][A[i] < x]); return;}}//插入过程,像普通的排序二叉树一样插入,最后进行调整。int main(){while (inone(n) != EOF){rt = 0; sz = 1;rep(i, 1, n) inone(x), insert(rt, x);queue<int> p; p.push(rt);int flag = G[rt] = 1, cnt = 0;while (!p.empty()){int q = p.front(); p.pop();printf("%d%s", A[q], ++cnt == n ? "\n" : " ");flag &= G[q] <= n;if (ch[q][0]) p.push(ch[q][0]), G[ch[q][0]] = 2 * G[q];if (ch[q][1]) p.push(ch[q][1]), G[ch[q][1]] = 2 * G[q] + 1;}//简单的层次遍历,顺便进行重新标号,判断是否是完全二叉树printf("%s\n", flag ? "YES" : "NO");}return 0;}


0 0