内容来源于《数据结构与算法:C语言描述》
1. 抽象数据类型
#ifndef _Tree_H
struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
SearchTree MakeEmpty(SearchTree T);
Position Find(ElementType X,SearchTree T);
Position FindMin(SearchTree T);
Position FindMax(SearchTree T);
SearchTree Insert(ElementType X,SearchTree T);
SearchTree Delete(ElementType X,SearchTree T);
ElementType Retrieve(Position P);
#endif /*_Tree_H*/
/*Place in the implementation file */
struct TreeNode{
ElementType Element;
SearchTree Left;
SearhTree Right;
};
2.具体实现
2.1FIND
递归
Position
Find( ElementType X, SearchTree T ){
if(T == NULL)
return NULL;
if(T->Element == X)
return T;
else if(T->Element < X)
return Find( X, T->Right );
else
return Find( X, T->Left );
}
非递归
Position
Find( ElementType X, SearchTree T ){
SearchTree ret = T;
while(ret && ret->Element != X){
if(ret->Element > X)
ret = ret->Left;
else
ret = ret->Right;
}
return ret;
}
FindMax、FindMin
找最大值和最小值操作实际上差不多
就只写FindMax
非递归
Position
FindMax( SearchTree T){
SearchTree ret = T;
while(ret && ret->Right)
ret = ret->Right;
return ret;
}
递归
Position
FindMax( SearchTree T){
if(T == NULL || T->Right == NULL) return T;
return FindMax(T->Right);
}
Insert
二叉查找树插入操作并不会改变原先树的状态
只是不断向远离根的方向生长
递归
SearchTree Insert(ElementType X,SearchTree T){
if(T == NULL){
SearchTree node = (SearchTree) malloc(sizeof(TreeNode));
node->Left = NULL;node->Right = NULL;
node->Element = X;
}
if(X < T->Element )
T->Left = Insert( X, T->Left );
else
T->Right = Insert( X, T->Right );
return T;
}
非递归
SearchTree Insert(ElementType X,SearchTree T){
SearchTree node = (SearchTree) malloc(sizeof(TreeNode));
node->Left = NULL;node->Right = NULL;
node->Element = X;
if(T == NULL) T = node;
else{
SearchTree cur = T;
while(cur){
if(cur->Element > X ){
if(cur->Left) cur = cur->Left;
else{
cur->Left = node;
break;
}
}
else{
if(cur->Right) cur = cur->Right;
else{
cur->Right = node;
break;
}
}
}
}
return T;
}
Delete
//书上的
SearchTree Delete(ElementType X,SearchTree T){
Position TmpCell;
if(T == NULL)
Error("Element not 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
{
TmpCell = T;
if(T->Left == NULL)
T = T->Right;
if(T->Right == NULL)
T = T->Left;
free(TmpCell);
}
return T;
}
SearchTree Delete(ElementType X,SearchTree T){
if(T == NULL) return NULL;
if( T->Element == X ){
SearchTree L = T->Left;
SearchTree R = T->Right;
SearchTree tar = NULL;
if(R){
SearchTree pre = R;
while(pre->Left && pre->Left->Left)
pre = pre->Left;
tar = pre->Left;
pre->Left = tar->Left->Right;
}
else if(L){
SearchTree pre = L;
while(pre->Right && pre->Right->Right)
pre = pre->Right;
tar = pre->Right;
pre->Right = tar->Right->Left;
}
if(tar){
tar->Left = L;
tar->Right = R;
}
free(T);
}
else if(T->Element < X)
T->Right = Delete( X, T->Right);
else
T->Left = Delete( X, T->Left);
return T;
}