AVL树的实现与演示

接上一篇的查找二叉树

AvlTree.h

/* * AvlTree.h * * Created on: Jan 17, 2016 * Author: youngwan */

#ifndef AVLTREE_H_
#define AVLTREE_H_

typedef int ElementType;
typedef struct _AvlNode_
{
    ElementType Element;
    struct _AvlNode_ *Left;
    struct _AvlNode_ *Right;
    int Height;
}AvlNode, *Tree, *Position, *AvlTree;

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);
#endif /* AVLTREE_H_ */

AvlTree.cpp

/*
 * AvlTree.cpp
 *
 *  Created on: Jan 17, 2016
 *      Author: youngwan
 */

#include "../AvlTree/AvlTree.h"

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

AvlTree MakeEmpty(AvlTree T)
{
    if (NULL != T)
    {
        MakeEmpty(T->Left);
        MakeEmpty(T->Right);
        free(T);
    }
    return NULL;
}


Position Find(ElementType X, AvlTree T)
{
    if (NULL == T)
        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 (NULL == T)
        return NULL;
    else if (NULL == T->Left)
        return T;
    else
        return FindMin(T->Left);
}

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

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

static inline int Max(int X, int Y)
{
    return (X > Y ? X : Y);
}

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

static 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(K1->Height, Height(K2->Right)) + 1;
    return K2;
}

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

    return SingleRotateWithLeft(K3);
}

static Position DoubleRotateWithRight(Position K1)
{
    K1->Right = SingleRotateWithLeft(K1->Right);

    return SingleRotateWithRight(K1);
}

AvlTree Insert(ElementType X, AvlTree T)
{
    if (NULL == T)
    {
        T = (AvlTree)malloc(sizeof(AvlNode));
        if (NULL == T)
        {
            fprintf(stderr, "malloc Error\n");
            return NULL;
        }
        else
        {
            T->Element = X;
            T->Height = 0;
            T->Left = T->Right = NULL;
        }
    }
    else if (X < T->Element)
    {
        T->Left = Insert(X, T->Left);
        if (2 == (Height(T->Left) - Height(T->Right)))
        {
            if (X < T->Left->Element)
                T = SingleRotateWithLeft(T);
            else
                T = DoubleRotateWithLeft(T);
        }
    }
    else if (X > T->Element)
    {
        T->Right = Insert(X, T->Right);
        if (2 == (Height(T->Right) - Height(T->Left)))
        {
            if (X > T->Right->Element)
                T = SingleRotateWithRight(T);
            else
                T = DoubleRotateWithRight(T);
        }
    }
    T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
    return T;
}

AvlTree  Delete(ElementType X, AvlTree T)
{
    AvlTree tmpTree;
    if (NULL == T)
    {
        fprintf(stderr, "T is NULL\n");
        return NULL;
    }
    else if (X > T->Element)
    {
        T->Right = Delete(X, T->Right);
        if (2 == (Height(T->Left) - Height(T->Right)))
        {
            if (Height(T->Left->Right) > Height(T->Left->Left))
                T = DoubleRotateWithLeft(T);
            else
                T = SingleRotateWithLeft(T);
        }
    }
    else if (X < T->Element)
    {
        T->Left = Delete(X, T->Left);
        if (2 == (Height(T->Right) - Height(T->Left)))
        {
            if (Height(T->Right->Left) > Height(T->Right->Right))
                T = DoubleRotateWithRight(T);
            else
                T = SingleRotateWithRight(T);
        }
    }
    else if (T->Left && T->Right)
    {
        tmpTree = FindMin(T->Right);
        T->Element = tmpTree->Element;
        T->Right = Delete(T->Element, T->Right);
        if (2 == (Height(T->Left) - Height(T->Right)))
        {
            if (Height(T->Left->Right) > Height(T->Left->Left))
                T = DoubleRotateWithLeft(T);
            else
                T = SingleRotateWithLeft(T);
        }
    }
    else
    {
        tmpTree = T;
        if (NULL == T->Left)
            T = T->Right;
        else
            T = T->Left;
        free(tmpTree);
    }

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

main.cpp使用和上一篇一样的.

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