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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Puppettt/article/details/82180462

1123 Is It a Complete AVL Tree(30 分)

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.

F1.jpg F2.jpg
F3.jpg F4.jpg

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树,判断是否是完全二叉树。

思路:主要是AVL树的建树;

AVL树是一种自平衡搜索树,小于父节点插在左子树,大于等于父节点插在右子树,然后是旋转问题。

要求左子树和右子树的高度差不能超过1(即<=1)

当插入一个结点时,发现一棵子树高度差超过1(称该子树根结点为问题结点);

1.当插在左子树的左子树上,进行右旋(RR);

        问题结点的左子树成为问题结点父节点的儿子,问题结点成为左子树的右儿子,左子树的右儿子成为问题结点的左儿子。

2.当插在右子树的右子树上,进行左旋(LL)

        问题结点的右子树成为问题结点父节点的儿子,问题结点成为右子树的左儿子,右子树的左儿子成为问题结点的右儿子。

3.当插在左子树的右子树上,进行左右旋(LR)

        先将问题结点的左子树进行左旋,再将问题结点进行右旋。

4.当插在右子树的左子树上,进行右左旋(RL)

        先将问题结点的左子树进行左旋,再将问题结点进行右旋。

最后层序遍历 并判断是否为完全二叉树。记层序遍历最后一个点的位置是否为n就行了。

代码:

#include <bits/stdc++.h>
using namespace std;
int n,a[21];
typedef struct node{
    int v;
    struct node *left,*right;
}Node;

int getHeight(Node *t)
{
    if(t==NULL)return 0;

    int l=getHeight(t->left);
    int r=getHeight(t->right);

    if(l>r)return l+1;
    else return r+1;
}
Node*LL(Node*tre)
{
    Node*tr=tre->right;
    tre->right=tr->left;
    tr->left=tre;
    return tr;
}
Node*RR(Node*tre)
{
    Node*tl=tre->left;
    tre->left=tl->right;
    tl->right=tre;
    return tl;
}
Node*LR(Node*tre)
{
    tre->left=LL(tre->left);
    tre=RR(tre);
    return tre;
}
Node*RL(Node*tre)
{
    tre->right=RR(tre->right);
    tre=LL(tre);
    return tre;
}
Node*Insert(Node*tre,int val)
{
    if(tre==NULL)
    {
        tre=new Node();
        tre->v=val;
        return tre;
    }
    if(tre->v>val)//小 左
    {
        tre->left=Insert(tre->left,val);
        int l=getHeight(tre->left);
        int r=getHeight(tre->right);
        //高度差为2 tre为问题结点
        if(l-r>=2)
        {
            //插在左子树的左子树上 右旋
            if(val<tre->left->v)
            {
                tre=RR(tre);
            }
            else//插在左子树的右子树上 左右旋
            {
                tre=LR(tre);
            }
        }
    }
    else {//大 右

        tre->right=Insert(tre->right,val);
        int l=getHeight(tre->left);
        int r=getHeight(tre->right);
        //高度差为2 tre为问题结点
        if(r-l>=2)
        {
            //插在右子树的右子树上 左旋
            if(val>tre->right->v)
            {
                tre=LL(tre);
            }
            else//插在右子树的左子树上 右左旋
            {
                tre=RL(tre);
            }
        }
    }
    return tre;
}
int main()
{
    scanf("%d",&n);
    Node *tre=NULL;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        tre=Insert(tre,a[i]);
    }
    int flag=0,num=1,maxn=1;
    queue<Node*>q;
    q.push(tre);
    while(!q.empty())
    {

        Node *pos=q.front();
        Node *l=pos->left;
        Node *r=pos->right;
        q.pop();
        if(flag)printf(" ");flag++;
        printf("%d",pos->v);
        if(l){num++;maxn=max(maxn,num);q.push(l);}
        else num=0;
        if(r){num++;maxn=max(maxn,num);q.push(r);}
        else num=0;
    }
    printf("\n");
    if(maxn==n)printf("YES\n");
    else printf("NO\n");
}

猜你喜欢

转载自blog.csdn.net/Puppettt/article/details/82180462