// 二叉搜索树的查找操作Find
Position Find(ElementType X, BinTree BST)
{
if(!BST) return NULL;
if(X > BST->Data)
return Find(X, BST->Right);
else if(X < BST->Data)
return Find(X, BST->Left);
else
return BST;
} // 尾递归(可以用循环代替)
Position IterFind(ElementType X, BinTree BST)
{
while(BST)
{
if(X > BST->Data)
BST = BST->Right;
else if(X < BST->Data)
BST = BST->Left;
else
return BST;
}
return NULL;
} // 查找效率决定于树的高度
// 查找最大值和最小值
Position FindMin(BinTree BST)
{
if(!BST) return NULL;
else if(!BST->Left)
return BST;
else
return FindMin(BST->Left);
}
Position FindMax(BinTree BST)
{
if(BST)
while(BST->Right) BST = BST->Right;
return BST;
}
// 插入元素
BinTree Insert(ElementType X, BinTree BST)
{
if(!BST)
{
BST = malloc(sizeof(struct TreeNode));
BST->Data = X;
BST->Left = BST->Right = NULL;
}
else
{
if(X < BST->Data)
BST->Left = Insert(X, BST->Left);
else if(X > BST->Data)
BST->Right = Insert(X, BST->Right);
}
return BST;
}
// 删除结点
BinTree Delete(ElementType X, BinTree BST)
{
Position Tmp;
if(!BST) printf("要删除的元素未找到");
else if(X < BST->Data)
BST->Left = Delete(X, BST->Left); // 左子树递归删除
else if(X > BST->Data)
BST->Right = Delete(X, BST->Right); // 右子树递归删除
else
{
if(BST->Left && BST->Right) // 被删除结点有左右两个子节点
{
Tmp = FindMin(BST->Right);
/* 在右子树中找最小的元素填充删除结点 */
BST->Data = Tmp->Data;
BST->Right = Delete(BST->Data, BST->Right);
/* 在删除结点的右子树中删除最小元素 */
}
else // 被删除结点有一个或无子节点
{
Tmp = BST;
if(!BST->Left) // 有右孩子或无子节点
BST = BST->Right;
else if(!BST->Right) // 有左孩子或无子节点
BST = BST->Left;
free(Tmp);
}
}
return BST;
}