AVL树(C++) 2018.3.28

// ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
//
///avl树
#include <stdio.h>
#include <stdlib.h>
///树结点----------------------------------------------------------------------------------------------
typedef struct AVLNode
{
    int height;
    int BF ;
    int data;
    AVLNode* Lchild;
    AVLNode* Rchild;
}*AVLTree;
///初始化------------------------------------------------------------------------------------------------
void Init_AVL(AVLTree &p)
{
    p = 0;
}
///判空--------------------------------------------------------------------------------------------------
bool Is_Empty(AVLTree &p)
{
    if (!p)
        return 1;
    else
        return 0;
}
///增----------------------------------------------------------------------------------------------------
bool En_Node(AVLTree &p,int val)
{
    void adjust_AVL(AVLTree &p);
    void height_(AVLTree &p);
    if (p == 0)///递归出口
    {
        p = new AVLNode;
        p->data = val;
        p->height=1;
        p->Lchild=NULL;
        p->Rchild=NULL;
        p->BF=0;
        return 1;
    }
    if (p->data == val)
        return 0;
    else if (val < p->data)///递归入口
        En_Node(p->Lchild, val);
    else if (val > p->data)///递归入口
        En_Node(p->Rchild, val);
    adjust_AVL(p);
    height_(p);
    return 1;
}
///平衡---------------------------------------------------------------------------------------------------------------------------
void adjust_AVL(AVLTree &p)
{
    void BF_(AVLTree &p);
    ///平衡因子
    ///检查平衡因子
    void R_Route(AVLTree &p);
    void L_Route(AVLTree &p);
    void height_(AVLTree &p);
    BF_(p);
    if(p->BF==2)
    {
        if(p->Lchild->BF>0)
            {
                R_Route(p);
                BF_(p->Lchild);
                BF_(p->Rchild);
                BF_(p);
                height_(p);
            }
        else {
            L_Route(p->Lchild);
            R_Route(p);
            BF_(p);
            BF_(p->Lchild);
            BF_(p->Rchild);
        }
    }
    else if (p->BF==-2)
    {
        if(p->Rchild->BF<0)
            {
                L_Route(p);
                BF_(p->Lchild);
                BF_(p->Rchild);
                BF_(p);
            }
        else
        {
            R_Route(p->Rchild);///
            L_Route(p);
            BF_(p);
            BF_(p->Rchild);
            BF_(p->Lchild);
        }
    }
}
///递归寻找需要删除节点的中序遍历直接前驱输入的p为根的左子树指针----------------------------------------------------------------
AVLTree Find_(AVLTree &p,AVLTree &n)
{   void height_(AVLTree &p);
    void BF_(AVLTree &p);
    if(p->Rchild==0)
    {
        AVLTree q=p;
        n->Rchild=p->Lchild;
        return q;
    }
    else
    {
        AVLTree m=Find_(p->Rchild,p);///传回最右子树的指针
        height_(p);
        BF_(p);
        return m;
    }

}
///删------------------------------------------------------------------------------------------------------
void Delete_AVL(AVLTree &p,int val)
{
    void adjust_AVL(AVLTree &p);
    void BF_(AVLTree &p);
    void height_(AVLTree &p);
    if(p==0)
        return;///未找到
    if(p->data==val)///找到此结点
    {///删掉此结点
        if(!p->Lchild||!p->Rchild)///有一个子树不存在
        {
            if(p->Lchild)
            {
                AVLTree q=p;
                p=p->Lchild;
                free(q);
            }
            else if(p->Rchild)
            {
                AVLTree q=p;
                p=p->Rchild;
                free(q);
            }
            else///两子树均不存在
                {p=NULL;return;}
        }
        else///两棵子树均存在
        {///递归
            AVLTree t=NULL;
            AVLTree q=Find_(p->Lchild,t);
            q->Lchild=p->Lchild;
            q->Rchild=p->Rchild;
            BF_(q);
            height_(q);
            free(p);
            p=q;
        }
    }
    ///否则继续查找
    else if(val<p->data)
    {
        Delete_AVL(p->Lchild,val);
        adjust_AVL(p);
        BF_(p);
    }
    else if(val>p->data)
    {
        Delete_AVL(p->Rchild,val);
        adjust_AVL(p);
        BF_(p);
    }
}
///BF-------------------------------------------------------------------------------------------------
void BF_(AVLTree &p)
{
    if (p->Lchild && p->Rchild)
        p->BF = p->Lchild->height - p->Rchild->height;
    else
    {
        if (p->Rchild!=0)
            p->BF = 0 - p->Rchild->height;
        else if(p->Lchild!=0)
            p->BF = p->Lchild->height;
        else
            p->BF=0;
    }
    ///平衡因子
}
///右旋-------------------------------------------------------------------------------------------------
void R_Route(AVLTree &p)
{
    AVLTree q=p->Lchild;
    p->Lchild=q->Rchild;
    q->Rchild=p;
    p=q;
    void height_(AVLTree &p);
    height_(p->Rchild);
    height_(p);
}

///左旋-----------------------------------------------------------------------------------------------------
void L_Route(AVLTree &p)
{
    AVLTree q=p->Rchild;
    p->Rchild=q->Lchild;
    q->Lchild=p;
    p=q;
    void height_(AVLTree &p);
    height_(p->Lchild);
    height_(p);
}
///计算树高-------------------------------------------------------------------------------------------------
void height_(AVLTree &p)
{
    if(!p)
        p->height=0;
    if (p->Lchild && p->Rchild)
    {
        if(p->Lchild->height>p->Rchild->height)
            p->height=p->Lchild->height+1;
        else
            p->height=p->Rchild->height+1;
    }
    else if(p->Rchild!=0)
        p->height=p->Rchild->height+1;
    else if(p->Lchild!=0)
        p->height=p->Lchild->height+1;
    else p->height=1;
}
///先序遍历----------------------------------------------------------------------------------------
bool Visit_AVL(AVLTree &p)
{
    printf("%d\t height=%d\tBF=%d\n", p->data,p->height,p->BF);
    if (p->Lchild)
    {
        Visit_AVL(p->Lchild);
    }
    if (p->Rchild)
    {
        Visit_AVL(p->Rchild);
    }
    return 1;

}
///测试代码-----------------------------------------------------------------------------------------
int main()
{
    AVLTree q;
    Init_AVL(q);
    En_Node(q, 5);
    En_Node(q, 8);
    En_Node(q, 10);
    En_Node(q, 1);
    En_Node(q, 12);
    En_Node(q,6);
    En_Node(q, 11);
    En_Node(q, 15);
    En_Node(q, 13);
    En_Node(q, 7);
    En_Node(q, 16);
    En_Node(q, 20);
    Visit_AVL(q);
    printf("\n\n");
    Delete_AVL(q,8);
    Visit_AVL(q);
    printf("\n\n");
    Delete_AVL(q,10);
    printf("\n\n");
    Visit_AVL(q);
    Delete_AVL(q,13);
    printf("\n\n");
    Visit_AVL(q);
    return 0;
}

    原文作者:AVL树
    原文地址: https://blog.csdn.net/lxhseph/article/details/79732950
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞