平衡二叉树

#include<stdio.h>
#include<malloc.h>

typedef struct BSTNode{
    int data;
    int bf;//平衡因子
    struct BSTNode *lchild,*rchild;
}BSTNode,*BSTTree;

int InsertBST(BSTTree &T,int key,int &taller);
void L_Rotate(BSTTree &T);
void R_Rotate(BSTTree &T);
void LeftBalance(BSTTree &T);
void RightBalance(BSTTree &T);

int main()
{
    int n,i,taller=0;
    scanf("%d",&n);
    int *a=(int *)malloc(sizeof(int)*n);
    BSTTree T=NULL;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        InsertBST(T,a[i],taller);
    }
    free(a);
    return 0;
}

//向平衡二叉树插入节点,插入成功返回1,插入失败返回0
int InsertBST(BSTTree &T,int key,int &taller)//taller用于标记树是否变高
{
    if(!T)
    {
        T=(BSTTree)malloc(sizeof(BSTNode));
        T->data=key;
        T->lchild=T->rchild=NULL;
        T->bf=0;
        taller=1;
    }
    else
    {
        if(T->data==key)
        {
            taller=0;
            return 0;
        }
        else if(key<T->data)
        {
            if(!InsertBST(T->lchild,key,taller))
                return 0;
            if(taller)
            {
                switch(T->bf)
                {
                case 1:LeftBalance(T);taller=0;break;
                case 0:T->bf=1;taller=1;break;
                case -1:T->bf=0;taller=0;break;
                }
            }
        }
        else if(key>T->data)
        {
            if(!InsertBST(T->rchild,key,taller))
                return 0;
            if(taller)
            {
                switch(T->bf)
                {
                case 1:T->bf=0;taller=0;break;
                case 0:T->bf=-1;taller=1;break;
                case -1:RightBalance(T);taller=0;break;
                }
            }
        }
    }
    return 1;
}

//左旋转
void L_Rotate(BSTTree &T)
{
    BSTTree rc=T->rchild;
    T->rchild=rc->lchild;
    rc->lchild=T;
    T=rc;
}


//右旋转
void R_Rotate(BSTTree &T)
{
    BSTTree lc=T->lchild;
    T->lchild=lc->rchild;
    lc->rchild=T;
    T=lc;
}

void LeftBalance(BSTTree &T)
{
    BSTTree lc=T->lchild;
    if(lc->bf==1)
    {
        T->bf=0;
        lc->bf=0;
        L_Rotate(T);
    }
    else if(lc->bf==-1)
    {
        BSTTree rc=lc->rchild;
        //最终lc->rchild指向rc->lchild;T->lchild最终指向rc->rchild;
        if(rc->bf==1)
            {T->bf=-1;lc->bf=0;}
        else if(rc->bf==0)
            {T->bf=0;lc->bf=0;}
        else if(rc->bf==-1)
            {T->bf=0;lc->bf=0;}
        rc->bf=0;
        L_Rotate(T->lchild);
        R_Rotate(T);
    }
}

void RightBalance(BSTTree &T)
{
    BSTTree rc=T->rchild;
    if(rc->bf==-1)
    {
        T->bf=0;
        rc->bf=0;
        L_Rotate(T);
    }
    else if(rc->bf==1)
    {
        BSTTree lc=rc->lchild;
        //最终rc->lchild指向lc->rchild;T->rchild最终指向lc->lchild;
        if(lc->bf==1)
        {
            T->bf=0;
            rc->bf=-1;
        }
        else if(lc->bf==0)
        {
            T->bf=0;
            rc->bf=0;
        }
        else if(lc->bf=-1)
        {
            T->bf=1;
            rc->bf=0;
        }
        lc->bf=0;
        R_Rotate(T->rchild);
        L_Rotate(T);
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值