平衡二叉树

Posted 陌上柳絮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了平衡二叉树相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;
const int maxn=107;

typedef struct node
{
    node():height(0), lchild(NULL), rchild(NULL) {}
    int data;
    int height;
    struct node* lchild;
    struct node* rchild;
} AVLTreeNode, *AVLTree;

int Getheight(AVLTree &T)
{
    if(T)
        return T->height;
    else
        return -1;
}

///查找最大元素
AVLTree FindMax(AVLTree T)
{
    if(T)
    {
        while(T->rchild)
            T=T->rchild;
    }
    return T;
}

///查找最小元素
AVLTree FindMin(AVLTree T)
{
    if(T)
    {
        while(T->lchild)
            T=T->lchild;
    }
    return T;
}


AVLTree SingleLeftRotate(AVLTree &A)
{
    AVLTree B=A->lchild;
    A->lchild=B->rchild;
    B->rchild=A;
    A->height=max(Getheight(A->lchild), Getheight(A->rchild))+1;
    B->height=max(Getheight(B->lchild), A->height)+1;
    return B;
}

AVLTree SingleRightRotate(AVLTree &A)
{
    AVLTree B=A->rchild;
    A->rchild=B->lchild;
    B->lchild=A;
    A->height=max(Getheight(A->lchild), Getheight(A->rchild))+1;
    B->height=max(Getheight(B->rchild), A->height)+1;
    return B;
}


AVLTree DoubleLeftRightRotate(AVLTree &A)
{
    A->lchild=SingleRightRotate(A->lchild);
    return SingleLeftRotate(A);
}

AVLTree DoubleRightLeftRotate(AVLTree &A)
{
    A->rchild=SingleLeftRotate(A->rchild);
    return SingleRightRotate(A);
}
///插入函数
AVLTree AVLInsert(AVLTree &T, int x)
{
    if(!T)/**若插入空树, 则新建包含一个结点的树*/
    {
        T=new AVLTreeNode();
        T->data=x;
        T->height=0;
        T->lchild=NULL;
        T->rchild=NULL;
    }
    else if(x < T->data)/**插入T的左子树*/
    {
        T->lchild=AVLInsert(T->lchild, x);
        if(Getheight(T->lchild)-Getheight(T->rchild)==2)/**需要左旋*/
        {
            if(x < T->lchild->data)
                T=SingleLeftRotate(T);///左单旋
            else
                T=DoubleLeftRightRotate(T);///左-右双旋
        }
    }

    else if(x > T->data)/**插入T的右子树*/
    {
        T->rchild=AVLInsert(T->rchild, x);
        if(Getheight(T->lchild)-Getheight(T->rchild)==-2)
        {
            if(x > T->rchild->data)
                T=SingleRightRotate(T);///右单旋
            else
                T=DoubleRightLeftRotate(T);///右-左双旋
        }
    }
    T->height=max(Getheight(T->lchild), Getheight(T->rchild))+1;
    return T;
}

///删除函数
bool AVLDelete(AVLTree &T, int x)
{
    if(T==NULL)
        return false;

    if(T->data==x)
    {
        if(T->lchild!=NULL && T->rchild!=NULL)
        {
            if(Getheight(T->lchild) > Getheight(T->rchild))
            {
                AVLTree p=FindMax(T->lchild);
                T->data=p->data;
                AVLDelete(T->lchild, T->data);
            }
            else
            {
                AVLTree p=FindMin(T->rchild);
                T->data=p->data;
                AVLDelete(T->rchild, T->data);
            }
        }
        else
        {
            AVLTree p=T;
            if(T->lchild!=NULL)
                T=T->lchild;
            else
                T=T->rchild;
            delete p;
        }
    }
    else if(x < T->data)
    {
        AVLDelete(T->lchild, x);
        if(Getheight(T->rchild)-Getheight(T->lchild)>1)
        {
            if(Getheight(T->rchild->lchild)>Getheight(T->rchild->rchild))
                T=DoubleRightLeftRotate(T);
            else
                T=SingleRightRotate(T);
        }
        else
            T->height=max(Getheight(T->lchild), Getheight(T->rchild))+1;
    }
    else
    {
        AVLDelete(T->rchild, x);
        if(Getheight(T->lchild)-Getheight(T->rchild)>1)
        {
            if(Getheight(T->lchild->rchild)>Getheight((T->lchild->lchild)))
                T=DoubleLeftRightRotate(T);
            else
                T=SingleLeftRotate(T);
        }
        else
            T->height=max(Getheight(T->lchild), Getheight(T->rchild))+1;
    }
    return true;
}

///查找函数
AVLTree IterFind(AVLTree T, int x)
{
    while(T)
    {
        if(x > T->data)
            T=T->rchild;
        else if(x < T->data)
            T=T->lchild;
        else
            return T;
    }
    return NULL;
}

void InOrder(AVLTree T)
{
    if(T)
    {
        InOrder(T->lchild);
        printf("%d ", T->data);
        InOrder(T->rchild);
    }
}

void LevelOrder(AVLTree T)
{
    queue<AVLTree>Q;
    Q.push(T);

    while(!Q.empty())
    {
        T=Q.front();
        Q.pop();
        printf("%d ", T->data);

        if(T->lchild!=NULL)
            Q.push(T->lchild);
        if(T->rchild!=NULL)
            Q.push(T->rchild);
    }
}
int main()
{
    AVLTree T=NULL;
    int a[11]= {3, 2, 1, 4, 5, 6, 7, 10, 9, 8};
    for(int i=0; i<10; i++)
        AVLInsert(T, a[i]);
    InOrder(T);
    printf("\n");

    LevelOrder(T);
    printf("\n");

    if(AVLDelete(T, 6))
    {
        puts("Delete Success");
        InOrder(T);
        printf("\n");
    }
    else
        puts("Delete Failed");

    AVLTree x=IterFind(T, 6);
    if(!x)
        puts("NotFound");
    else
        puts("Found");
    return 0;
}

 

以上是关于平衡二叉树的主要内容,如果未能解决你的问题,请参考以下文章

求数据结构算法平衡二叉树实现代码

平衡二叉树详解——PHP代码实现

PHP代码实现平衡二叉树详解

判断一颗二叉树是否为二叉平衡树 python 代码

树--07---二叉树--04--平衡二叉树(AVL树)

平衡二叉树的定义及基本操作(查找插入删除)及代码实现