自己动手写二叉平衡树

这篇博客详细介绍了如何自己动手编写二叉平衡树的代码,包括中序遍历、平衡因子测试、插入节点等功能,并提供了测试用例。通过示例展示了平衡树在插入操作后的调整过程。
摘要由CSDN通过智能技术生成

//还有不足或错误的地方,欢迎指正,仅供参考

 

#include <stdio.h>
#include <stdlib.h>

 

int num=2;
int tmpt=1;

 

typedef struct tree
{
    int data;
    int BF;     //平衡因子
    struct tree *left;
    struct tree *right;
    struct tree *parent;
}Tree;

 

void intree(Tree *t);
Tree * testbf(Tree *t);
int testcheng(Tree *t);
int changebf(Tree *t);
int insertnode(Tree *t,int dnode);

Tree *root=NULL;

 

void intree(Tree *t)  //中序遍历
{
    if(t == NULL)
        return ;
    intree(t->left);
    printf("%d bf=%d ",t->data,t->BF);
    intree(t->right);
    return ;
}

 

Tree * testbf(Tree *t)    //测试bf是否符合要求
{
    if(t->BF==-1 || t->BF==0 || t->BF==1)
        return t;
    else if(t->BF==2)
    {
        if(t->left->BF==1)
        {
            Tree *tmp=t->left;
            t->left=tmp->right;
            if(tmp->right!=NULL)
            {
                tmp->right->parent=t;
                t->left=tmp->right;
            }
            else
                t->left=NULL;
            tmp->right=t;
            if(t->parent!=NULL && t->parent->left->data==t->data)
            {
                tmp->parent=t->parent;
                t->parent->left=tmp;
                t->parent=tmp;
                t->BF=changebf(t);
                tmp->BF=changebf(tmp);
                return tmp;
            }
            else if(t->parent!=NULL && t->parent->right->data==t->data)
            {
                tmp->parent=t->parent;
                t->parent->right=tmp;
                t->parent=tmp;
                t->BF=changebf(t);
                tmp->BF=changebf(tmp);
                return tmp;
            }
            else if(t->parent==NULL)
            {
                tmp->parent=NULL;
                t->parent=tmp;
                t->BF=changebf(t);
                tmp->BF=changebf(tmp);
                root=tmp;  //因为遍历要从root开始,故这里要改变root的值
                return tmp;
            }

        }
        if(t->left->BF==-1)
        {
            Tree *tmpb=t->left;
            Tree *tmpc=tmpb->right;
            if(tmpc->left!=NULL)
            {
                tmpc->left->parent=tmpb;
                tmpb->right=tmpc->left;
            }
            else
                tmpb->right=NULL;
            if(tmpc->right!=NULL)
            {
                tmpc->right->parent=t;
                t->left=tmpc->right;
            }
            else
                t->left=NULL;
            tmpb->parent=tmpc;
            tmpc->left=tmpb;
            tmpc->right=t;
            if(t->parent!=NULL && t->parent->left->data==t->data)
            {
                tmpc->parent=t->parent;
                t->parent->left=tmpc;
                t->parent=tmpc;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                tmpc->BF=changebf(tmpc);
                return tmpc;
            }
            else if(t->parent!=NULL && t->parent->right->data==t->data)
            {
                tmpc->parent=t->parent;
                t->parent->right=tmpc;
                t->parent=tmpc;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                tmpc->BF=changebf(tmpc);
                return tmpc;
            }
            else if(t->parent==NULL)
            {
                tmpc->parent=NULL;
                t->parent=tmpc;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                tmpc->BF=changebf(tmpc);
                root=tmpc;
                return tmpc;
            }

        }
    }
    else if(t->BF==-2)
    {
        if(t->right->BF==-1)
        {
            Tree *tmpb=t->right;
            if(tmpb->left!=NULL)
            {
                tmpb->left->parent=t;
                t->right=tmpb->left;
            }
            else
                t->right=NULL;
            tmpb->left=t;
            if(t->parent!=NULL && t->parent->left->data==t->data)
            {
                tmpb->parent=t->parent;
                t->parent->left=tmpb;
                t->parent=tmpb;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                return tmpb;
            }
            else if(t->parent!=NULL && t->parent->right->data==t->data)
            {
                tmpb->parent=t->parent;
                t->parent->right=tmpb;
                t->parent=tmpb;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                return tmpb;
            }
            else if(t->parent==NULL)
            {
                tmpb->parent=NULL;
                t->parent=tmpb;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                root=tmpb;
                return tmpb;
            }
        }
        else if(t->right->BF==1 )
        {
            Tree *tmpb=t->right;
            Tree *tmpc=tmpb->left;
            if(tmpc->left!=NULL)
            {
                tmpc->left->parent=t;
                t->right=tmpc->left;
            }
            else
                t->right=NULL;
            if(tmpc->right!=NULL)
            {
                tmpc->right->parent=tmpb;
                tmpb->left=tmpc->right;
            }
            else
                tmpb->left=NULL;
            tmpc->left=t;
            tmpc->right=tmpb;
            tmpb->parent=tmpc;
            if(t->parent!=NULL && t->parent->left->data==t->data)
            {
                tmpc->parent=t->parent;
                t->parent->left=tmpc;
                t->parent=tmpc;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                tmpc->BF=changebf(tmpc);
                return tmpc;
            }
            else if(t->parent!=NULL && t->parent->right->data==t->data)
            {
                tmpc->parent=t->parent;
                t->parent->right=tmpc;
                t->parent=tmpc;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                tmpc->BF=changebf(tmpc);
                return tmpc;
            }
            else if(t->parent==NULL)
            {
                tmpc->parent=NULL;
                t->parent=tmpc;
                t->BF=changebf(t);
                tmpb->BF=changebf(tmpb);
                tmpc->BF=changebf(tmpc);
                root=tmpc;
                return tmpc;
            }
        }
    }
    else
        return;

}

 

int testcheng(Tree *t)   //返回以t为根的树的最大层数
{
    if(t->left==NULL && t->right==NULL)
    {
        num--;
        return;
    }
    if(t->left!=NULL)
    {
        if(num>tmpt)
            tmpt=num;
        num++;
        testcheng(t->left);

    }
    if(t->right!=NULL)
    {
        if(num>tmpt)
            tmpt=num;
        num++;
        testcheng(t->right);
    }
    num--;
    return tmpt;
}

 

int  changebf(Tree *t)  //修改bf
{
    Tree *tmp=NULL;
    int leftbf=0;
    int rightbf=0;
    if(t->left==NULL)
        leftbf=0;
    else
    {
        leftbf=testcheng(t->left);
        num=2;
        tmpt=1;
    }
    if(t->right==NULL)
        rightbf=0;
    else
    {
        rightbf=testcheng(t->right);
        num=2;
        tmpt=1;
    }
    t->BF=leftbf-rightbf;
    tmp=testbf(t);
    if(tmp->parent!=NULL)
    {
        changebf(tmp->parent);
        tmp=NULL;
    }
    tmp=NULL;
    return (t->BF);
}

 

int insertnode(Tree *t,int dnode)//插入节点
{
    if(t != NULL)
    {
        if(dnode < t->data)
        {
            if(t->left != NULL)
            {
                insertnode(t->left,dnode);
            }
            else
            {
                Tree *newnode=(Tree *)malloc(sizeof(Tree));
                newnode->data=dnode;
                newnode->BF=0;
                newnode->left=NULL;
                newnode->right=NULL;
                newnode->parent=t;
                t->left=newnode;
                changebf(newnode->parent);
                return ;
            }
        }
        else
        {
            if(t->right !=NULL)
            {
                insertnode(t->right,dnode);
            }
            else
            {
                Tree *newnode=(Tree *)malloc(sizeof(Tree));
                newnode->data=dnode;
                newnode->BF=0;
                newnode->left=NULL;
                newnode->right=NULL;
                newnode->parent=t;
                t->right=newnode;
                changebf(newnode->parent);
                return ;
            }
        }
    }
}

 


int main(int argc, char** argv)  //测试数据
{
    Tree *first=(Tree *)malloc(sizeof(Tree));
    first->data=5;
    first->BF=0;
    first->left=first->parent=first->right=NULL;
    root=first;
    intree(root);  printf("/n");
    insertnode(root,8);   intree(root);  printf("/n");
    insertnode(root,4);   intree(root);  printf("/n");
    insertnode(root,3);   intree(root);  printf("/n");
    insertnode(root,1);   intree(root);  printf("/n");
    insertnode(root,6);   intree(root);  printf("/n");
    insertnode(root,9);   intree(root);  printf("/n");
    insertnode(root,0);   intree(root);  printf("/n");
    insertnode(root,2);   intree(root);  printf("/n");
    return 0;
}

 // 测试结果


 
int main(int argc, char** argv)  //书本例子数据
{
    Tree *first=(Tree *)malloc(sizeof(Tree));
    first->data=13;
    first->BF=0;
    first->left=first->parent=first->right=NULL;
    root=first;
    intree(root);
    printf("/n");
    insertnode(root,24);
    intree(root);
    printf("/n");
    insertnode(root,37);
    intree(root);
    printf("/n");
    insertnode(root,90);
    intree(root);
    printf("/n");
    insertnode(root,53);
    intree(root);
    printf("/n");
    return 0;
}

 

//测试结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值