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.
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
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的基本功,难度中上,我第一次我太会写,就看前辈的代码,写的真的非常清晰,第二遍我就尽量自己独立完成,把别人的代码背会理解掌握了,知识就是我们自己的了,第一遍做不出来的题目,多做几遍,做的自己可以独立完成。
pat 1123

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
struct node{
    int data;
    int level;
    node *lchild,*rchild;
};
typedef node* nodePtr;
int N;
int getLevel(nodePtr t)
{
    if(t==NULL)
        return 0;
    return t->level;
}
int max(int a,int b)
{
    return a>b?a:b;
}
nodePtr SingleLeftRotation(nodePtr &t) //左旋
{
    nodePtr T=t->rchild;
    t->rchild=T->lchild;
    T->lchild=t;
    T->level = max( getLevel(T->lchild),getLevel(T->rchild) ) + 1;
    t->level = max( getLevel(t->lchild),getLevel(t->rchild) ) + 1;
    return T;
}
nodePtr SingleRightRotation(nodePtr &t) //右旋
{
    nodePtr T=t->lchild;
    t->lchild=T->rchild;
    T->rchild=t;
    T->level = max( getLevel(T->lchild),getLevel(T->rchild) ) + 1;
    t->level = max( getLevel(t->lchild),getLevel(t->rchild) ) + 1;
    return T;
}
nodePtr LeftRightRotation(nodePtr &t) //左旋右旋
{
    t->lchild=SingleLeftRotation(t->lchild); //左子树右旋
    return SingleRightRotation(t); //左旋
}
nodePtr RightLeftRotation(nodePtr &t) //右旋左旋
{
    t->rchild=SingleRightRotation(t->rchild);
    return SingleLeftRotation(t);
}
nodePtr insert_AVL(nodePtr &t,int a)
{
    if(t==NULL)
    {
        t=new node;
        t->data=a;
        t->level=1;
        t->lchild=NULL;
        t->rchild=NULL;
        return t;
    }
    if(a<t->data)
    {
        t->lchild = insert_AVL(t->lchild,a); //插入到左子树
        if( getLevel( t->lchild ) - getLevel( t->rchild ) == 2 ) //左子树比右子树大二
        {
            if( a < t->lchild->data )
                t=SingleRightRotation(t); //向右旋转
            else
                t=LeftRightRotation(t); //先左旋,再右旋
        }
    }
    else
    {
        t->rchild = insert_AVL(t->rchild,a); //插入到右子树
        if( getLevel( t->lchild ) - getLevel( t->rchild ) == -2 )
        {
            if( a > t->rchild->data )
                t=SingleLeftRotation(t);
            else
                t=RightLeftRotation(t);
        }
    }
    t->level = max( getLevel( t->lchild ),getLevel( t->rchild ) ) + 1;
    return t;
}
bool flag=false;
bool cpt=true;
vector<int> ans;
void Level_tra(nodePtr root)
{
    queue<nodePtr> que;
    que.push(root);
    while(!que.empty())
    {
        nodePtr curr=que.front();
        que.pop();
        ans.push_back(curr->data);
        if(curr->lchild)
        {
            if(flag)
                cpt=false;
            que.push(curr->lchild);
        }
        else
            flag=true; //进入叶子结点状态
        if(curr->rchild)
        {
            if(flag)
                cpt=false;
            que.push(curr->rchild);
        }
        else
            flag=true;
    }
}
int main(){
    cin>>N;
    int a;
    node *root=NULL;
    for(int i=1;i<=N;i++)
    {
        scanf("%d",&a);
        root=insert_AVL(root,a);
    }
    Level_tra(root);
    for(int i=0;i<ans.size();i++)
        printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');
    cpt==true?printf("YES\n"):printf("NO\n");
}

发布了174 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41173604/article/details/100569170