走进数据结构和算法(c++版)(16)——平衡二叉树( AVL 树)

平衡二叉树( AVL 树)

  在上面一章中我们介绍了二叉排序树,它是一种既方便查找也有利于插入和删除的方法。但是当输入的排好序数据组时,二叉排序树为斜树,不利于查找效率的稳定性差。现在我们介绍一种高度平衡的二叉排序树——平衡二叉树( AVL 树)。

平衡二叉树,是一种二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1。

  将二叉树上结点的左子树深度减去右子树深度的值称为平衡因子BF (Balance Factor) 。我们来根据上一章的代码进行改进:

#include<vector>
#define LH +1//左高
#define EH 0//等高
#define RH -1//右高
using namespace std;

class BinSortT
{
public:
    BinSortT();
    ~BinSortT();
    friend void CreateBSTree(vector<int>t, int k, BinSortT* T, BinSortT *P);//根据一组数据创建二叉排序树
    friend bool SearchBST(BinSortT* T, int key);//查找
    friend bool InsertBST(BinSortT* T, int key);//查找不成功插入
    friend bool DeleteBST(BinSortT* T, int key);//删除数据
    friend bool Delete(BinSortT* T);//删除操作
    friend bool InsertAVL(BinSortT* T, int e, bool *taller, int &f);//插入元素
    friend void R_Rotate(BinSortT* T);//右旋处理
    friend void L_Rotate(BinSortT* T);//左旋处理
    friend void LeftBalance(BinSortT* T);//左平衡旋转处理
    friend void RightBalance(BinSortT* T);//右平衡旋转处理
    int data;//结点数据
    int bf; //结点的平衡因子
private:
    BinSortT*lchild, *rchild;//左右孩子指针
};
#include "BinSortT.h"


BinSortT::BinSortT()
{
}


BinSortT::~BinSortT()
{
}
void R_Rotate(BinSortT* T)//右旋处理
{

    BinSortT *L = new BinSortT;
    *L = *T->lchild;
    T->lchild = L->rchild;
    BinSortT *p = new BinSortT;
    *p = *T;
    L->rchild = p;
    *T = *L;
    delete L;
}
void L_Rotate(BinSortT* T)//左旋处理
{
    BinSortT *R = new BinSortT;
    *R = *T->rchild;
    T->rchild =R->lchild;
    BinSortT *p = new BinSortT;
    *p = *T;
    R->lchild = p;
    *T= *R;
    delete R;
}
void LeftBalance(BinSortT* T)//左平衡旋转处理
{
    BinSortT *L, *Lr;
    L = T->lchild;
    switch(T->bf)
    {
    case LH:
        T->bf = EH; 
        L->bf = EH;
        R_Rotate(T);
        break;
    case EH:
        T->bf = LH;
        L->bf = RH;
        R_Rotate(T);
        break;
    case RH:
        Lr = T->rchild;
        switch (Lr->bf)
        {
        case LH:
            T->bf = RH;
            L->bf = EH;
            break;
        case EH:
            T->bf = EH; 
            L->bf = EH;
            break;
        case RH:
            T->bf = EH;
            L->bf = LH;
            break;
        }
        Lr->bf = EH;
        L_Rotate(T);
        R_Rotate(T);
    }
}
void RightBalance(BinSortT* T)//右平衡旋转处理
{
    BinSortT *R, *Rl;
    R = T->rchild;
    switch (T->bf)
    {
    case RH:
        T->bf = EH; 
        R->bf = EH;
        L_Rotate(T);
        break;
    case EH:
        T->bf = RH;
        R->bf = LH;
        L_Rotate(T);
        break;
    case LH:
        Rl = T->lchild;
        switch (Rl->bf)
        {
        case RH:
            T->bf = LH;
            R->bf = EH;
            break;
        case EH:
            T->bf = EH;
            R->bf = EH;
            break;
        case LH:
            T->bf = EH;
            R->bf = RH;
            break;
        }
        Rl->bf = EH;
        R_Rotate(T);
        L_Rotate(T);
    }
}
#include <iostream>
#include "BinSortT.h"
using namespace std;

int main()
{
    int e,n,f=0;
    bool *taller=new bool;
    *taller = true;
    vector<int> vec;
    cout << "输入数据元素个数:";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> e;
        vec.push_back(e);
    }
    BinSortT *T = new BinSortT;
    for (int i = 0; i < n; i++)
    {
        InsertAVL(T, vec[i], taller,f);
    }

    system("pause");
    return 0;
}
void CreateBSTree(vector<int>t, int k, BinSortT *T, BinSortT *P)//根据一组数据创建二叉排序树
{
    T->data = t[k++];
    while (k < t.size())
    {
        if (t[k] < P->data)
        {
            if (!P->lchild)
            {
                P->lchild = new BinSortT;
                P->lchild->data = t[k++];
                P = T;
            }
            else
            {
                P = P->lchild;
            }
        }
        else if (t[k] > P->data)
        {
            if (!P->rchild)
            {
                P->rchild = new BinSortT;
                P->rchild->data = t[k++];
                P = T;
            }
            else
            {
                P = P->rchild;
            }
        }
    }


}
bool SearchBST(BinSortT* T, int key)//查找
{
    if (!T)
        return false;
    if (key == T->data)
        return true;
    if (key < T->data)
        SearchBST(T->lchild, key);
    else
        SearchBST(T->rchild, key);
}
bool InsertBST(BinSortT* T, int key)//查找不成功插入
{
    if (!T)
    {
        T = new BinSortT;
        T->data = key;
        return false;
    }
    while (T)
    {
        if (key == T->data)return true;
        if (key < T->data)
        {
            if (!T->lchild)
            {
                T->lchild = new BinSortT;
                T->lchild->data = key;
                return false;
            }
            else
            {
                T = T->lchild;
            }
        }
        else if (key > T->data)
        {
            if (!T->rchild)
            {
                T->rchild = new BinSortT;
                T->rchild->data = key;
                return false;
            }
            else
            {
                T = T->rchild;
            }
        }

    }

}
bool DeleteBST(BinSortT* T, int key)//删除数据
{
    if (!T)return false;
    if (key == T->data)
        return Delete(T);
    else if (key < T->data)
        return DeleteBST(T->lchild, key);
    else if (key >T->data)
        return DeleteBST(T->rchild, key);
}
bool Delete(BinSortT* T)//删除操作
{
    BinSortT *q, *s;
    if (T->rchild == NULL)
    {
        q = T->lchild;
        *T = *(T->lchild);
        delete q;

    }
    else if (T->lchild == NULL)
    {
        q = T->rchild;
        *T = *(T->rchild);
        delete q;
    }
    else
    {
        s = T->lchild;
        q = T;
        while (s->rchild)
        {
            q = s;
            s = s->rchild;
        }

        T->data = s->data;
        if (q != T)
            q->rchild = s->lchild;
        else
            q->lchild = s->lchild;
        delete s;
    }
    return true;
}
bool InsertAVL(BinSortT* T, int e, bool *taller,int &f)//插入元素
{
    if (!f)
    {
        T->data = e;
        T->bf = EH;
        T->lchild = T->rchild = NULL;
        *taller = true;
        f = 1;
    }
    else
    {
        if (e == T->data)
        {
            *taller = false;
            return false;
        }
        if (e < T->data)
        {
            if (!T->lchild)
            {
                T->lchild = new BinSortT;
                f = 0;
            }
            if (!InsertAVL(T->lchild, e, taller,f))
                return false;
            if (*taller)
            {
                switch (T->bf)
                {
                case LH:
                    LeftBalance(T);
                    *taller = false;
                    break;
                case EH:
                    T->bf = LH;
                    *taller = true;
                    break;
                case RH:
                    T->bf = EH;
                    *taller = false;
                    break;
                }
            }
        }
        else
        {
            if (!T->rchild)
            {
                T->rchild = new BinSortT;
                f = 0;
            }
            if (!InsertAVL(T->rchild, e, taller,f))
                return false;
            if (*taller)
            {
                switch (T->bf)
                {
                case RH:
                    RightBalance(T);
                    *taller = false;
                    break;
                case EH:
                    T->bf = RH;
                    *taller = true;
                    break;
                case LH:
                    T->bf = EH;
                    *taller = false;
                    break;
                }
            }
        }
    }
    return true;
}

  在VS上运行结果如下:
1
2

猜你喜欢

转载自blog.csdn.net/hu_weichen/article/details/80820453