###### AVL平衡树的实现

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;
}


#### java数据结构与算法之平衡二叉树(AVL树)的设计与实现

2016-12-31 23:40:43

#### STL源码笔记（18）—平衡二叉树AVL（C++封装+模板）

2016-05-20 10:40:20

#### 数据结构----AVL平衡树----AVL平衡树的基本操作

2017-03-29 14:11:24

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

2016-09-23 21:34:37

#### 红黑树和AVL平衡树的区别

2014-10-10 22:41:23

#### 平衡二叉树（AVL）图解与实现

2015-10-31 21:31:14

#### AVL平衡树(详解)-JAVA/C版本

2015-04-13 10:21:43

#### C语言实现AVL-平衡二叉树

2016-03-23 21:22:30

#### AVL平衡树及插入操作的C语言实现

2016年06月21日 2KB 下载

#### 各种平衡树Treap/SBT/Avl/Splay tree

2015-04-08 18:14:27

AVL平衡树的实现