BinTree Insert( BinTree BST, ElementType X )
{
if (BST==NULL) {
BinTree tmp=(BinTree)malloc(sizeof(struct TNode));
tmp->Data=X;
tmp->Left=tmp->Right=NULL;
return tmp;
};
if (X<BST->Data)
BST->Left=Insert(BST->Left,X);
else
BST->Right=Insert(BST->Right,X);
return BST;
}
Position Find( BinTree BST, ElementType X ) {
if (BST==NULL||BST->Data==X) return BST;
if (X<BST->Data) return Find (BST->Left,X);
else return Find (BST->Right,X);
}
Position FindMin( BinTree BST ) {
if (BST==NULL||BST->Left==NULL) return BST;
else return FindMin (BST->Left);
}
Position FindMax( BinTree BST ) {
if (BST==NULL||BST->Right==NULL) return BST;
else return FindMax (BST->Right);
}
BinTree Delete( BinTree BST, ElementType X ) {
BinTree TMP;
if (BST==NULL) {
printf ("Not Found\n");
return NULL;
}
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!=NULL&&BST->Right!=NULL) {
TMP=FindMin (BST->Right);
BST->Data=TMP->Data;
BST->Right=Delete (BST->Right,TMP->Data);
}
else {
TMP=BST;
if (BST->Left!=NULL)
BST=BST->Left;
else if (BST->Right!=NULL)
BST=BST->Right;
else BST=NULL;
}
}
return BST;
}