AVL树删除算法 (使用树高)

以下代码大部分直接搬运自Mark Allen 的数据结构与算法分析, 略有改动。

鉴于书本及示例代码均未提及删除操作, 在此提供一种思路。


头文件:

//avltree.h
typedef int ElementType;

#ifndef _AVLTREE_H_
#define _AVLTREE_H_

struct AvlNode;
typedef AvlNode * AvlTree;
typedef AvlNode * Position;

AvlTree CreateAvlTree(void);
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);
ElementType Retrieve(Position P);

#endif // _AVLTREE_H_

实现:
//avltree.c
#include
#include"avltree.h"
#include"fatal.h"

struct AvlNode{
    ElementType Element;
    AvlTree Left;
    AvlTree Right;
    int Height;
};

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

static int Max(int Lhs, int Rhs)
{
    return Lhs > Rhs ? Lhs : Rhs;
}

static AvlTree 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), K2->Height) + 1;

    return K1;
}

static AvlTree SingleRotateWithRight(Position K2)
{
    Position K1;

    K1 = K2->Right;
    K2->Right = K1->Left;
    K1->Left = K2;

    K2->Height = Max(Height(K2->Left), Height(K2->Right)) + 1;
    K1->Height = Max(Height(K1->Right), K2->Height) + 1;

    return K1;
}

static AvlTree DoubleRotateWithLeft(Position K3)
{
    SingleRotateWithRight(K3->Left);

    return SingleRotateWithLeft(K3);
}

static AvlTree DoubleRotateWithRight(Position K3)
{
    SingleRotateWithLeft(K3->Right);

    return SingleRotateWithRight(K3);
}

AvlTree CreateAvlTree(void)
{
    return NULL;
}

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->Element)
        return Find(X, T->Left);
    else if (X > T->Element)
        return Find(X, T->Right);
    else
        return T;
}

Position FindMin(AvlTree T)
{
    if (T != NULL)
        while (T->Left != NULL)
            T = T->Left;
    return T;
}

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

ElementType Retrieve(Position P)
{
    return P->Element;
}

AvlTree Insert(ElementType X, AvlTree T)
{
    if (T == NULL)
    {
        T = malloc(sizeof(struct AvlNode));
        if (T == NULL)
            Error("Out of space");
        T->Element = X;
        T->Left = T->Right = NULL;
        T->Height = 0;
    }
    else if (X < T->Element)
    {
        T->Left = Insert(X, T->Left);
        if (Height(T->Left) - Height(T->Right) == 2)
            if (X < T->Left->Element)
                SingleRotateWithLeft(T);
            else
                DoubleRotateWithLeft(T);
    }
    else if (X > T->Element)
    {
        T->Right = Insert(X, T->Right);
        if (Height(T->Right) - Height(T->Left) == 2)
            if (X > T->Right->Element)
                SingleRotateWithRight(T);
            else
                DoubleRotateWithRight(T);
    }

    T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
    return T;
}


/*以下为删除操作*/

static AvlTree DeleteNode(AvlTree T, Position P)
{
    if (P == NULL)
    {
        P = T->Left;
        free(T);
        return P;
    }
    if (P->Left == NULL)
    {
        P->Left = T->Left;
        free(T);
        if (Height(P->Left) - Height(P->Right) == 2)
            if (Height(P->Left->Left) - Height(P->Right) == 1)
                P = SingleRotateWithLeft(P);
            else
                P = DoubleRotateWithLeft(P);
        return P;
    }
    if (P->Left->Left == NULL)
    {
        P->Left->Left = T->Left;
        P->Left->Right = T->Right;
        free(T);
        return P;
    }
    else
    {
        T = DeleteNode(T, P->Left);
        if (Height(P->Right) - Height(P->Left) == 2)
            if (Height(P->Right->Right) - Height(P->Left) == 1)
                P = SingleRotateWithRight(P);
            else
                P = DoubleRotateWithRight(P);
        P->Height = Max(Height(P->Left), Height(P->Right)) + 1;
        return T;
    }
}

AvlTree Delete(ElementType X, AvlTree T)
{
    if (T == NULL)
        return NULL;
    if (X < T->Element)
    {
        if (T->Left = Delete(X, T->Left) == NULL)
            return NULL;
        if (Height(T->Right) - Height(T->Left) == 2)
            if (Height(T->Right->Right) - Height(T->Left) == 1)
                T = SingleRotateWithRight(T);
            else
                T = DoubleRotateWithRight(T);
        T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
    }
    else if (X > T->Element)
    {
        if (T->Right = Delete(X, T->Right) == NULL)
            return NULL;
        if (Height(T->Left) - Height(T->Right) == 2)
            if (Height(T->Left->Left) - Height(T->Right) == 1)
                T = SingleRotateWithLeft(T);
            else
                T = DoubleRotateWithLeft(T);
        T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
    }
    else if (X == T->Element)
    {
        return DeleteNode(T, T->Right);
    }
    return T;
}



你可能感兴趣的:(数据结构,C)