AVL平衡二叉树实现

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

#define TRUE 1
#define FALSE 0
#define EH 0
#define LH 1
#define RH -1
//平衡二叉树的节点结构
typedef struct BiTNode{
    int data;
    int bf;//平衡因子
    BiTNode *lchld,*rchild;
}BiTNode,*BiTree;

typedef int Status;
//LL调整
void R_Rotate(BiTree &T)
{
    BiTree p;
    p=T->lchild;
    T->lchild=p->rchild;
    p->rchild=T;
    T=p;
}
//RR调整
void L_Rotate(BiTree &T)
{
    BiTree p;
    p=T->rchild;
    T->rchild=p->lchild;
    p->lchild=T;
    T=p;
}
//左平衡处理LL,LR
void LeftBalance(BiTree &T)
{
    BiTree L,LR;
    L=T->lchild;
    switch(L->bf) 
    {
        case 0:
            L->bf=-1;
            T->bf=1;
            R_Rotate(T);
            break;
        case 1:
            L->bf=T->bf=0;
            R_Rotate(T);
            break;
        case -1:
            LR=L->rchild;
            switch(LR->bf)
            {
                case 0:
                    L->bf=T->bf=0;
                case -1:
                    T->bf=0;
                    L->bf=1;
                    break;
                case 1:
                    L->bf=0;
                    T->bf=-1;
                    break;
                default:
                    break;
            }
            LR->bf=0;
            L_Rotate(T->lchild);
            R_Rotate(T);
            break;
        default:
            break;
    }
}
//右平衡处理RR,RL
void RightBalance(BiTree &T)
{
    BiTree R,RL;//调用函数,T为根,右边高于左边T->bf=-1;
    R=T->rchild;//R是T的右孩子
    switch(R->bf)
    {
        case -1://如果T时右孩子和T它们的平衡因子相同,直接左旋。
            T->bf=R->bf=0;
            L_Rotate(T);
            break;
        case 0:
            T->bf=-1;
            R->bf=1;
            L_Rotate(T);
            break;
        case 1:
            RL=R->lchild;
            switch(RL->bf)
            {
                case 0:
                    T->bf=R->bf=0;
                    break;
                case -1:
                    R->bf=0;
                    T->bf=1;
                    break;
                case 1:
                    R->bf=-1;
                    T->bf=0;
                    break;
                default:
                    break;
            }
            RL->bf=0;
            R_Rotate(T->rchild);
            L_Rotate(T);
            break;
    }
}

bool InsertAVLTree(BiTree &T,int key,bool taller)
{
    if(!T)//T为空
    {
        T=(BiTree)malloc(sizeof(BiTNode));
        T->bf=0;
        T->lchild=T->rchild=NULL;
        T->data=key;
        taller=true;
        return true;
    }
    else
    {
        if(key==T->data)//已经存在key相同的元素,不用插入,返回false
        {
            taller=false;
            return false;
        }
        if(key<T->data)//所插入的元素小于根的值,找他的左孩子比较
        {
            if(!InsertAVLTree(T->lchild,key,taller))
            {
                return false;
            }
            if(taller)
            {
                switch(T->bf)
                {
                    case 0:
                        T->bf=1;
                        taller=true;
                        break;
                    case 1:
                        LeftBalance(T);
                        taller=false;
                        break;
                    case -1:
                        T->bf=0;
                        taller=false;
                        break;
                    default:
                        break;
                }
            }
            else
            {
                if(!InsertAVLTree(T->rchild,key,taller))
                {
                    return false;
                }
                if(taller)
                {
                    switch(T->bf)
                    {
                        case 0:
                            T->bf=-1;
                            taller=true;
                            break;
                        case 1:
                            T->bf=0;
                            taller=false;
                            break;
                        case -1:
                            RightBalance(T);
                            taller=false;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/drq1/p/9430960.html