关闭

AVL平衡树的实现

457人阅读 评论(0) 收藏 举报
分类:

avltree.h

#ifndef AVLTREE_H_INCLUDED
#define AVLTREE_H_INCLUDED

typedef  int ElementType;
struct  avlnode;
typedef  struct  avlnode *Position;
typedef  Position AvlTree;
struct  avlnode
{
    ElementType data;
    AvlTree  left;
    AvlTree  right;
    int  height;
};


AvlTree  MakeEmpty(AvlTree  T);
Position  Find(ElementType  X,AvlTree T);
Position  FindMin(AvlTree  T);
Position  FindMax(AvlTree  T);
AvlTree  Insert(ElementType  X,AvlTree  T);
AvlTree  Delete(ElementType X,AvlTree  T);
int  Height(Position  P);

Position  SingleRotateWithLeft(Position  T);
Position  SingleRotateWithRight(Position  T);
Position  DoubleRotateWithLeft(Position  T);
Position  DoubleRotateWithRight(Position  T);
int Max(int h1,int h2);

void  PreOrder(AvlTree T);
void  MidOrder(AvlTree T);
void  PosOrder(AvlTree T);



#endif // AVLTREE_H_INCLUDED

avltree.c

#include  <stdio.h>
#include  <malloc.h>
#include  "avltree.h"

int  Height(Position  P)
{
    if(P==NULL)
        return  -1;
    else
        return P->height;
}


AvlTree  MakeEmpty(AvlTree  T)
{
    if(T!=NULL)
    {
        MakeEmpty(T->left);
        MakeEmpty(T->right);
        free(T);
    }

    return NULL;
}


Position  Find(ElementType  X,AvlTree T)
{
    if(T==NULL)
        return  NULL;

    if(X<T->data)
        return Find(X,T->left);
    else if(X>T->data)
        return Find(X,T->right);
    else
        return  T;
}


Position  FindMin(AvlTree  T)
{
    if(T==NULL)
        return NULL;
    else  if(T->left ==NULL)
        return  T;
    else
        return FindMin(T->left);
}


Position  FindMax(AvlTree  T)
{
    if(T!=NULL)
    {
        while(T->right !=NULL)
        T=T->right;
    }

    return  T;
}


AvlTree  Insert(ElementType  X,AvlTree  T)
{
    if(T==NULL)
    {
        T=(AvlTree)malloc(sizeof(struct  avlnode));
        if(T==NULL)
            printf("Error: out of space!!!");
        else
        {
            T->data=X;  T->height=0;
            T->left =T->right =NULL;
        }
    }
    else  if(X<T->data)
    {
        T->left =Insert(X,T->left);

        if(Height(T->left) - Height(T->right) ==2)
        {
            if(X<T->left->data)
                T=SingleRotateWithLeft(T);
            else
                T=DoubleRotateWithLeft(T);
        }
    }
    else if(X>T->data)
    {
        T->right =Insert(X,T->right);

        if(Height(T->right) - Height(T->left) ==2)
        {
            if(X>T->right->data)
                T=SingleRotateWithRight(T);
            else
                T=DoubleRotateWithRight(T);
        }
    }

    T->height =Max(Height(T->left) ,Height(T->right)) +1;
    return  T;
}





Position  SingleRotateWithLeft(Position  K2)
{
    Position  K1;
    K1=K2->left;
    K2->left =K1->right;
    K1->right =K2;

    K2->height =Max(Height(K2->left) ,Height(K2->right)) +1;
    K1->height =Max(Height(K1->left) ,Height(K1->right)) +1;

    return  K1;
}


Position  SingleRotateWithRight(Position  K1)
{
    Position  K2;
    K2=K1->right;
    K1->right=K2->left;
    K2->left =K1;

    K1->height=Max(Height(K1->left) ,Height(K1->right)) +1;
    K2->height =Max(Height(K2->left) ,Height(K2->right)) +1;

    return K2;
}

Position  DoubleRotateWithLeft(Position  K3)
{
    K3->left =SingleRotateWithRight(K3->left);

    return SingleRotateWithLeft(K3);
}


Position  DoubleRotateWithRight(Position  K3)
{
    K3->right =SingleRotateWithLeft(K3->left);

    return SingleRotateWithRight(K3);
}


int Max(int h1,int h2)
{
    return h1 >h2 ? h1:h2 ;
}


void  PreOrder(AvlTree T)
{
    if(T)
    {
        printf("%d \t",T->data);
        PreOrder(T->left);
        PreOrder(T->right);
    }
}
void  MidOrder(AvlTree T)
{
     if(T)
    {
         MidOrder(T->left);
        printf("%d \t",T->data);
        MidOrder(T->right);
    }
}
void  PosOrder(AvlTree T)
{
     if(T)
    {

        PosOrder(T->left);
        PosOrder(T->right);
        printf("%d \t",T->data);
    }
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include  "avltree.c"

int main()
{
    int i ,j;
    j=0;
    AvlTree  T;
    T=MakeEmpty(NULL);

    for( i = 0; i < 50; i++, j ++ )
        T=Insert(j,T);

    PreOrder(T);
    return 0;
}
各个例程并未完全测试,欢迎各位讨论!


代码来自数据结构与算法分析


代码中参数为一级指针仍能实现对数据的改变,关键在于返回值为一指针。如返回值为void 等类型则参数需要使用二级指针。


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:67234次
    • 积分:1440
    • 等级:
    • 排名:千里之外
    • 原创:73篇
    • 转载:22篇
    • 译文:0篇
    • 评论:12条
    最新评论