AVL树的实现

AVL树是带有平衡条件的BST。

AVL树满足以下两个条件:

(1)任何一个节点的左子树上的数值都比该节点小,右子树上的数值都比该节点大

(2)每个节点的左右子树的高度差的绝对值不差过1


c语言实现:

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

//#define Max(a,b)  ((a)>(b))?(a):(b)

typedef struct avltree{
    int key;
    struct avltree* lchild;
    struct avltree* rchild;
    int height;
}avltree,*avlptr;

avltree* avl_insert(int x,avltree* t);

avltree* avl_delete(int x,avltree* t);

avltree* findmin(avltree* t);

avltree* findmax(avltree* t);

avltree* Rrotation(avltree*);

avltree* Lrotation(avltree*);

avltree* LRrotation(avltree*);

avltree* RLrotation(avltree*);

void avl_print(avltree* t);

void pre_print(avltree* t);

int height(avltree* t)
{
    if(t==NULL)
        return -1;
    else
        return t->height;
}

int Max(int x,int y)
{
    if(x>y)
        return x;
    else 
        return y;
}
main()
{
    avltree* root=NULL;
    root=avl_insert(2,root);
    root=avl_insert(1,root);
    root=avl_insert(3,root);
    root=avl_insert(4,root);
    root=avl_insert(5,root);
    root=avl_insert(6,root);
    root=avl_insert(7,root);
    root=avl_insert(16,root);
    root=avl_delete(4,root);
    root=avl_insert(15,root);
    root=avl_insert(14,root);
    root=avl_insert(13,root);
    avl_print(root);
    printf("\n");
    printf("%d\n",root->key);

    return 0;
}

void avl_print(avltree * t)
{
    if(t!=NULL)
    {
        printf("%d (%d) ",t->key,t->height);
        avl_print(t->lchild);
        //printf("%d ",t->key);
        avl_print(t->rchild);
    }
}

avltree* avl_insert(int x,avltree* root)//插入操作
{
    avltree* cur;

    if(root==NULL)
    {
        cur=(avltree *)malloc(sizeof(struct avltree));
        assert(cur!=NULL);
        cur->key=x;
        cur->height=0;
        cur->lchild=cur->rchild=NULL;
        root=cur;
        return root;
    }
    else if(x<root->key){
        root->lchild=avl_insert(x,root->lchild);
        if(height(root->lchild)-height(root->rchild)==2){
            if(x<root->lchild->key)
                root=Lrotation(root);
            else
                root=LRrotation(root);}
        else
            ;
            //root->height=Max(height(root->lchild),height(root->rchild)) +1;
    }
    else if(x>root->key){
        root->rchild=avl_insert(x,root->rchild);
        if(height(root->rchild)-height(root->lchild)==2){
            if(x>root->rchild->key)
                root=Rrotation(root);
            else
                root=RLrotation(root);}
        else
            //root->height=Max(height(root->lchild),height(root->rchild)) +1;
            ;
    }

    root->height=Max(height(root->lchild),height(root->rchild)) +1;

    return root;
}

avltree* findmin(avltree* t)
{
    if(t==NULL)
        return NULL;
    else if(t->lchild==NULL)
        return t;
    else
        return t->lchild;
}

avltree* findmax(avltree * t)
{
    if(t==NULL)
        return NULL;
    else if(t->rchild!=NULL)
        return t->rchild;
    else
        return t;
}

avltree* Rrotation(avltree* k1)
{
    avltree* k2;
    k2=k1->rchild;
    k1->rchild=k2->lchild;
    k2->lchild=k1;
    k1->height=1+Max(height(k1->lchild),height(k1->rchild));
    k2->height=1+Max(height(k2->lchild),height(k2->rchild));
    return k2;
}

avltree* Lrotation(avltree* k1)
{
    avltree * k2;
    k2=k1->lchild;
    k1->lchild=k2->rchild;
    k2->rchild=k1;
    k1->height=1+Max(height(k1->lchild),height(k1->rchild));
    k2->height=1+Max(height(k2->lchild),height(k2->rchild));
    return k2;
}

avltree* LRrotation(avltree* k1)
{
    k1->lchild=Rrotation(k1->lchild);

    return Lrotation(k1);
}

avltree* RLrotation(avltree* k1)
{
    k1->rchild=Lrotation(k1->rchild);

    return Rrotation(k1);
}

avltree* avl_delete(int x,avltree* proot)//删除操作
{
    avltree* cur;
    //assert(proot!=NULL);
    if(proot!=NULL){
    if(x<proot->key){
        proot->lchild=avl_delete(x,proot->lchild);
        if(height(proot->rchild)-height(proot->lchild)==2)
            if(height(proot->rchild->rchild)>height(proot->rchild->lchild))
                proot=Rrotation(proot);
            else
                proot=RLrotation(proot);
        else
            proot->height=Max(height(proot->lchild),height(proot->rchild))+1;
    }
    else if(x>proot->key){
        proot->rchild=avl_delete(x,proot->rchild);
        if(height(proot->lchild)-height(proot->rchild)==2)
            if(height(proot->lchild->lchild)>height(proot->lchild->rchild))
                proot=Lrotation(proot);
            else
                proot=LRrotation(proot);
        else
            proot->height=Max(height(proot->lchild),height(proot->rchild))+1;
    }
    else if(proot->lchild&&proot->rchild)
    {
        if(height(proot->rchild)>height(proot->lchild))
        {
            cur=findmin(proot->rchild);
            proot->key=cur->key;
            proot->rchild=avl_delete(proot->key,proot->rchild);
        }
        else
        {
            cur=findmax(proot->lchild);
            proot->key=cur->key;
            proot->lchild=avl_delete(proot->key,proot->lchild);
        }
    }
    else
    {
        cur=proot;
        if(proot->lchild==NULL)
            proot=proot->rchild;
        else if(proot->rchild==NULL)
            proot=proot->rchild;
        else 
            proot=NULL;
        free(cur);
    }
    //proot->height=Max(height(proot->lchild),height(proot->rchild))+1;
    }
    return proot;
}

参考:数据结构与算法分析


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