BinTree Insert(BinTree BST, ElementType X)
{
if (BST == NULL)
{
BST = (BinTree)malloc(sizeof(struct TNode));
BST->Data = X;
BST->Left = NULL;
BST->Right = NULL;
}
else if (X < BST->Data)
BST->Left = Insert(BST->Left, X);
else if (X > BST->Data)
BST->Right = Insert(BST->Right, X);
return BST;
}
Position Find(BinTree BST, ElementType X)
{
if (BST==NULL)
return NULL;
if (X == BST->Data)
return BST;
else if (X < BST->Data)
Find(BST->Left, X);
else
Find(BST->Right, X);
}
Position FindMin(BinTree BST)
{
if (BST)
{
while (BST->Left != NULL)
BST = BST->Left;
}
return BST;
}
Position FindMax(BinTree BST)
{
if (BST)
{
while (BST->Right != NULL)
BST = BST->Right;
}
return BST;
}
BinTree Delete(BinTree BST, ElementType X)
{
BinTree p;
if (BST == NULL)
{
printf("Not Found
");
return BST;
}
if (X < BST->Data) {
BST->Left = Delete(BST->Left, X);
}
else if (X > BST->Data) {
BST->Right = Delete(BST->Right, X);
}
else
{
if (BST->Left && BST->Right) {
p = FindMax(BST->Left);
BST->Data = p->Data;
BST->Left = Delete(BST->Left, BST->Data);
}
else {
p = BST;
if (!BST->Left) {
BST = BST->Right;
}
else if (!BST->Right) {
BST = BST->Left;
}
free(p);
}
}
return BST;
}