1123. Is It a Complete AVL Tree (30)

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>

#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>

using namespace std;

struct node
{
    int data,h;
    int level;
    int id;
    node* l;
    node* r;
};
node* root=NULL;

int geth(node* p)//为了防止出现访问到NULL->h的情况,最好写一个函数出来
{
    if(p==NULL)
        return 0;
    return p->h;
}
void update(node* p)//更新高度
{
    p->h=max( geth(p->l),geth(p->r) )+1;
}

int bf(node* p)
{
    return geth(p->l) - geth(p->r);
}

void L(node* &p)//L旋就是逆时针转。不用理会什么LR、RR之类的,那些是树型
{
    node* temp;//必须要用temp保存即 将是新的根 的地址,因为要更新两个节点的h,不保存的话就找不到了
    temp=p->r;
    p->r=temp->l;
    temp->l=p;
    update(p);
    update(temp);

    p=temp;//之所以要引用的原因
}
void R(node* &p)//将上面函数里的l r互换即可
{
    node* temp;
    temp=p->l;
    p->l=temp->r;
    temp->r=p;
    update(p);
    update(temp);

    p=temp;
}

void insertt(node* &p,int x)
{
    if(p==NULL)
    {
        p=(node*)malloc(sizeof(node));
        p->data=x;
        p->h=1;
        p->l=NULL;
        p->r=NULL;

        return;
    }

    if(x < p->data)
    {
        insertt(p->l,x);
        update(p);//插入完毕之后要递归更新节点的高度。这样触底反弹之后return可以最早碰到最小不平衡子树的根
        if(bf(p)==2)
        {
            if(bf(p->l)==1)//斜线型:        o
            {              //              o
                R(p);      //             o
            }
            else if
                (bf(p->l)==-1)//先左转后右转     o
             {                 //               o
                L(p->l);      //                 o
                R(p);
            }
        }
    }
    else
    {
        insertt(p->r,x);
        update(p);
        if(bf(p)==-2)
        {
            if(bf(p->r)==-1)
            {
                L(p);
            }
            else if(bf(p->r)==1)
            {
                R(p->r);
                L(p);
            }
        }
    }

    return;//
}

queue<node> q;
int flag=0;
int maxx=-1;
void bfs(node* p)
{
    q.push(*p);
    while(!q.empty())
    {
        node t=q.front();
        if(flag==0)
        {
            flag=1;
            printf("%d",t.data);
        }
        else
        {
            printf(" %d",t.data);
        }

        q.pop();
        if(t.l!=NULL)
        {
            (t.l)->level=t.level+1;
            (t.l)->id=t.id*2;
            if(t.id*2>maxx)
                maxx=t.id*2;
            q.push(*(t.l));
        }
        if(t.r!=NULL)
        {
            (t.r)->level=t.level+1;
            (t.r)->id=t.id*2+1;
            if(t.id*2+1>maxx)
                maxx=t.id*2+1;
            q.push(*(t.r));
        }
    }
}

int power(int x,int p)
{
    if(p==0)
        return 1;

    int ans=1;
    for(int i=0;i<p;i++)
        ans*=x;

    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);

    int N;;
    scanf("%d",&N);

    for(int i=0;i<N;i++)
    {
        int t;
        scanf("%d",&t);
        insertt(root,t);
    }

    root->level=1;
    root->id=1;
    bfs(root);

    if(N==1)
    {
        printf("\nYES",root->data);
        return 0;
    }

    if(maxx==N)
        printf("\nYES");
    else
        printf("\nNO");

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值