#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
struct TreeNode
{
ElementType Element;
struct TreeNode *Left;
struct TreeNode *Right;
};
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
SearchTree MakeEmpty(SearchTree T);
//创建空树
Position Find (ElementType X,SearchTree T);
//查找X
Position FindMin(SearchTree T);
//查找树的的最小值
Position FindMax(SearchTree T);
//查找树的最大值
SearchTree Insert(ElementType X,SearchTree T);
//插入X
SearchTree Delete(ElementType X,SearchTree T);
//删除X
ElementType Retrieve(Position P);
void Traveral_Q (SearchTree T);
//前序遍历
void Traveral_Z (SearchTree T);
//中序遍历
void Traveral_H (SearchTree T);
// 后序遍历
void Print(Position P);
//打印P结点的值
int main(void)
{
SearchTree T = NULL;
int M;
int a[5] = {2,8,6,5,3};
int i;
Position P;
ElementType ele;
MakeEmpty(T);
T = Insert(6,T);
//注意前面一定要加T = ,不然会崩溃, 因为传入的是T的复制品
for( i =0; i < 5; i++)
Insert(a[i],T);
//后面可以不加,因为加了等于,T = T,和不加一样
//但是第一次要加,因为不加的话,T 一直为NULL。
Traveral_Z(T);
//
Delete(99,T);
//
Print(FindMax(T));
return 0;
}
SearchTree MakeEmpty(SearchTree T)
{
if(NULL != T)
{
MakeEmpty(T->Left);
MakeEmpty(T->Right);
free(T);
}
return NULL;
}
Position Find (ElementType X,SearchTree 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(SearchTree T)
//使用尾递归也可以下面的方式
{
if(NULL == T)
return NULL;
else
if(NULL == T->Left)
return T;
else
return FindMin(T->Left);
}
Position FindMax(SearchTree T)
//使用循环,因为传入的是 T的复制,所以不会改变T
{
if( NULL != T)
{
while(NULL != T->Right)
T = T->Right;
}
return T;
}
SearchTree Insert(ElementType X,SearchTree T)
//插入X
{
if(NULL == T)
{
T = (SearchTree)malloc(sizeof(struct TreeNode));
if( NULL == T)
printf(“Out of Space\n”);
else
{
T->Element = X;
T->Left = T->Right = NULL;
}
}
else
if( X < T->Element)
//没有考虑存在X的情况
T->Left = Insert(X,T->Left);
else
if(X > T->Element)
T->Right = Insert(X,T->Right);
return T;
}
void Print(Position P)
{
printf(“%d”,P->Element);
}
SearchTree Delete(ElementType X,SearchTree T)
{
Position TmpCell;
if(NULL == T)
{
printf(“No Found\n”);
}
else
if(X < T->Element)
T->Left = Delete(X,T->Left);
else
if(X > T->Element)
T->Right = Delete(X,T->Right);
else
if(T->Left && T->Right)
// 存在两个儿子节点
{
TmpCell = FindMin(T->Right);
//从右子树中找到最小值覆盖该值,等于删除该数
T->Element = TmpCell->Element;
T->Right = Delete(T->Element,T->Right);
//递归在右子树中删除这个最小值
}
else
//存在一个或者0个儿子节点
{
TmpCell = T;
if(NULL == T->Left)
//如果右边为空,用左边儿子代替该节点
T = T->Right;
else if(NULL == T->Right )
//如果左边为空,用右边儿子代替该节点
T = T->Left;
free(TmpCell);
//删除该节点
}
return T;
}
void Traveral_H (SearchTree T)
{
if(NULL != T->Left)
{
Traveral_H(T->Left);
}
if(NULL != T->Right)
{
Traveral_H(T->Right);
}
printf(“%d “,T->Element);
}
void Traveral_Z (SearchTree T)
{
if(NULL != T->Left)
{
Traveral_Z(T->Left);
}
printf(“%d “,T->Element);
if(NULL != T->Right)
{
Traveral_Z(T->Right);
}
}
void Traveral_Q (SearchTree T)
{
printf(“%d “,T->Element);
if(NULL != T->Left)
{
Traveral_Q(T->Left);
}
if(NULL != T->Right)
{
Traveral_Q(T->Right);
}
}