Avl树的建立及其基础操作

#include
#include
#define  Max(a,b)    ( (a) > (b) ? (a) : (b))


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


AvlTree MakeEmpty(AvlTree T); //清空 
Position Find(ElementType X,AvlTree T); //查找 X
Position FindMin(AvlTree T); //查找最小值 
Position FindMax(AvlTree T); // 查找最小值 
AvlTree Insert(ElementType X,AvlTree T); //插入X 
AvlTree Delete(ElementType X,AvlTree T); //删除X 
ElementType Retrieve(Position P); //返回P结点的值 
static int Height(Position P); //返回P结点的高度 
static Position SingleRotateWithLeft(Position K2); //单左旋 
static Position SingleRotateWithRight(Position K2); //单右旋 
static Position DoubleleRotateWithLeft(Position K3); //双左旋 
static Position DoubleleRotateWithRight(Position K3); //双右旋 
void Traveral_Q (AvlTree T); //前序遍历 
void Traveral_Z (AvlTree T); //中序遍历 
void Traveral_H (AvlTree T); // 后序遍历 
void Print(Position P); //打印P结点的值 




int main(void)
{
AvlTree T = NULL;

MakeEmpty(T); 


T = Insert(99,T); //右边删除实验 
T = Insert(96,T);
T = Insert(101,T);
T = Insert(90,T);
T = Insert(100,T);
T = Insert(104,T);

T = Insert(106,T);

T = Delete(100,T);


// T = Insert(99,T); //左边删除实验 
// T = Insert(96,T);
// T = Insert(100,T);
// T = Insert(94,T);
// T = Insert(103,T);
// T = Insert(97,T);


// T = Insert(95,T);

T = Delete(90,T);
T = Delete(104,T);
T = Delete(101,T);


T = Delete(96,T);

printf("Max = %d\n",Retrieve(FindMax(T)));

printf("Min = %d\n",Retrieve(FindMin(T)));

printf("T->Left = %d\n",Retrieve(T->Left));
printf("T->Right = %d\n",Retrieve(T->Right));
Traveral_Z(T);

return 0;
}




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

return NULL;
}


Position Find(ElementType X,AvlTree T) //查找X,和普通二叉树一样 
{
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)
{
while(NULL != T->Right)
T = T->Right;
}
return T;

}
AvlTree Insert(ElementType X,AvlTree T) //插入X ,不支持插入已有的元素 
{
if(NULL == T) //找到插入的结点,直接插入,插入后都要返回上一个递归 
{
T = (AvlTree)malloc(sizeof(struct AvlNode));
if(NULL == T)
printf("Out of Space\n");
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 = SingleRotateWithRight(T);
else 
T = DoubleleRotateWithRight(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 = SingleRotateWithLeft(T);
else 
T = DoubleleRotateWithLeft(T);
}
T->Height = Max(Height(T->Left),Height(T->Right)) + 1; //更新高度 

return T;
}


AvlTree Delete(ElementType X,AvlTree T) //删除 
{
Position TmpCell,T2;
if(NULL == T)
{
printf("No Found\n");
}
else 
if(X < T->Element)
{
T->Left = Delete(X,T->Left); //在左边查找删除点 

// printf("Height(T->Right)=%d,Height(T->Left)=%d\n",Height(T->Right),Height(T->Left)); 

if((Height(T->Right) - Height(T->Left) ) == 2  )
{
T2 = T->Right;
if(Height(T2->Left) < Height(T2->Right)) //平衡 
T = SingleRotateWithLeft(T);
else 
T = DoubleleRotateWithLeft(T);

}

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

else 
if(X > T->Element)
{
T->Right = Delete(X,T->Right);

if((Height(T->Left) - Height(T->Right)) == 2)
{
T2 = T->Left;
if(Height(T2->Left) > Height(T2->Right))
T = SingleRotateWithRight(T);
else 
T = DoubleleRotateWithRight(T);

}

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

else 
if(T->Left && T->Right) // 存在两个儿子节点 
{
if(Height(T->Right) >= Height(T->Left)) //右高左底,从右边删除,左高右底,从左边删除 
{ //这样做保证删除后不需要多一步旋转 
TmpCell = FindMin(T->Right); //从右子树中找到最小值覆盖该值,等于删除该数 
T->Element = TmpCell->Element;
T->Right = Delete(T->Element,T->Right); //递归在右子树中删除这个最小值 
}
else
{
TmpCell = FindMax(T->Left);
T->Element = TmpCell->Element;
T->Left = Delete(T->Element,T->Left);
}
  }
else //存在一个或者0个儿子节点 
{
TmpCell = T;
if(NULL == T->Left) //如果右边为空,用左边儿子代替该节点 
T = T->Right;
else if(NULL == T->Right ) //如果右边为空,用左边儿子代替该节点 
T =  T->Left;
free(TmpCell); //删除该节点 ,这里不需要平衡,因为返回上一个递归时会平衡 

}
return T;
}


ElementType Retrieve(Position P)
{
if(P)
{
return P->Element; 
}
else
{
printf("No the Position and ");
return 0;
}

}


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


static Position SingleRotateWithRight(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 SingleRotateWithLeft(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 Position DoubleleRotateWithRight(Position K3) //右双旋转 
{
K3->Left = SingleRotateWithLeft(K3->Left); //先左旋,再右旋 

return SingleRotateWithRight(K3);
}


static Position DoubleleRotateWithLeft(Position K3) //左双旋 
{
K3->Right = SingleRotateWithRight(K3->Right); //先右旋,再左旋 

return SingleRotateWithLeft(K3);



void Traveral_H (AvlTree T)
{
if(NULL != T->Left)
{
Traveral_H(T->Left);
}
if(NULL != T->Right)
{
Traveral_H(T->Right);
}
printf("%d ",T->Element);
}


void Traveral_Z (AvlTree T)
{
if(NULL != T->Left)
{
Traveral_Z(T->Left);
}

printf("%d ",T->Element);

if(NULL != T->Right)
{
Traveral_Z(T->Right);
}
}


void Traveral_Q (AvlTree T)
{
printf("%d ",T->Element);
if(NULL != T->Left)
{
Traveral_Q(T->Left);
}
if(NULL != T->Right)
{
Traveral_Q(T->Right);
}

 

你可能感兴趣的:(C语言基本算法)