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

//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<stdlib.h>
#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;
}



#### AVL树的插入与删除，重点是四种旋转

2015-12-03 15:01:13

#### AVL树插入删除算法详解（有图） -- C++语言实现

2016-08-14 21:42:44

#### AVL平衡树递归删除实现 C语言

2016-09-23 21:34:37

#### AVL树的插入与删除(均为递归实现)

2016-09-21 21:22:41

#### avl树的插入删除

2008年04月03日 14KB 下载

#### AVL树的插入与删除操作

2016-07-22 20:30:19

#### AVL树的旋转、插入、删除及遍历C语言实现

2016-12-13 08:58:28

#### 算法--AVL树的删除

2013-02-27 03:42:09

#### AVL树的插入与旋转算法解析

2017-04-05 22:07:14

#### AVL树的旋转图解和简单实现

2017-07-07 18:03:51