PAT (Advanced Level) Practice 1123 Is It a Complete AVL Tree (30分) (AVL树建立+完全二叉树定义加判断)
1.题目
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
2.题目分析
1.AVL树的建立
图片参考(https://blog.csdn.net/qq_25343557/article/details/89110319)
1.右旋LL操作
tree rightrotate(tree t)
{
tree x = t->left;
tree t3 = x->right;
x->right = t;
t->left = t3;
return x;
}
2.左旋RR操作
tree leftrotate(tree t)
{
tree x = t->right;
tree t3 = x->left;
x->left = t;
t->right = t3;
return x;
}
3.LR先左旋再右旋
tree leftrightrotate(tree t)
{
t->left=leftrotate(t->left);
return rightrotate(t);
}
4.RL先右旋再左旋
tree rightleftrotate(tree t)
{
t->right=rightrotate(t->right);
return leftrotate(t);
}
2.层序遍历
使用队列进行遍历
3.完全二叉树的判断
最通俗的讲,完全二叉树其实就是层序遍历时,必须从左到右节点都不是NULL,直至结束。如果存在一个节点为NULL,而之后又有非NULL的节点,就不是二叉树。
于是我的判断方法就是即使节点是NULL也放入队列,再输出的时候遇到NULL判断已经输出的个数是不是总节点的个数,不是的话就不是完全二叉树。
3.代码
#include<iostream>
#include<queue>
using namespace std;
typedef struct node * tree;
struct node
{
int data;
tree left;
tree right;
};
int getheight(tree t)
{
if (!t)return 0;
int l = getheight(t->left) + 1;
int r = getheight(t->right) + 1;
return l > r ? l : r;
}
tree rightrotate(tree t)
{
tree x = t->left;
tree t3 = x->right;
x->right = t;
t->left = t3;
return x;
}
tree leftrotate(tree t)
{
tree x = t->right;
tree t3 = x->left;
x->left = t;
t->right = t3;
return x;
}
tree leftrightrotate(tree t)
{
t->left=leftrotate(t->left);
return rightrotate(t);
}
tree rightleftrotate(tree t)
{
t->right=rightrotate(t->right);
return leftrotate(t);
}
tree insert(tree t, int x)
{
if (!t)
{
t = (tree)malloc(sizeof(struct node));
t->left = t->right = NULL;
t->data = x;
return t;
}
if (t->data > x)
{
t->left = insert(t->left, x);
if (getheight(t->left) - getheight(t->right) > 1)
{
if (x<t->left->data)t=rightrotate(t);
else t=leftrightrotate(t);
}
}
else
{
t->right = insert(t->right, x);
if (getheight(t->right) - getheight(t->left) > 1)
{
if (x>t->right->data)t=leftrotate(t);
else t=rightleftrotate(t);
}
}
return t;
}
bool iscomplete = true;
int leveloutput(tree t,int n)
{
if (!t)return 0;
int c = 0;
queue<tree>list;
tree p = t;
bool space = false;
list.push(t);
while (!list.empty())
{
p = list.front(); list.pop();
if (p == NULL)
{
if(c<n)iscomplete = false;
continue;
}
printf("%s%d", space == false ? "" : " ", p->data); space = true; c++;
list.push(p->left);
list.push(p->right);
}
}
int main()
{
int n,a;
tree t = NULL;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a);
t = insert(t, a);
}
leveloutput(t,n);
printf("\n");
if (iscomplete)printf("YES\n");
else printf("NO\n");
}