#include#include"fatal.h"
structTreeNode;
typedefstruct TreeNode *Position;
typedefstruct TreeNode *SearchTree;
typedefintElementType;
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);structTreeNode
{
ElementType Element;
SearchTree left;
SearchTree right;
};
SearchTree MakeEmpty(SearchTree T)
{if(T!=NULL)
{
MakeEmpty(T->left);
MakeEmpty(T->right);
free(T);
}returnNULL;
}
Position Find(ElementType X,SearchTree T)
{if(T==NULL)returnNULL;if(XElement)return Find(X,T->left);else if(X>T->Element)return Find(X,T->right);else
returnT;
}
Position FindMin(SearchTree T)
{if(T==NULL)returnNULL;if(T->left==NULL)returnT;else
return FindMin(T->left);
}
Position FindMax(SearchTree T)
{if(T==NULL)returnNULL;else if(T->right==NULL)returnT;else
return FindMax(T->right);
}
SearchTree Insert(ElementType X,SearchTree T)
{if(T==NULL)
{
T=malloc(sizeof(structTreeNode));if(T==NULL)
FatalError("Out of space!!!");else{
T->Element=X;
T->left=T->right=NULL;
}
}else if(XElement)
T->left=Insert(X,T->left);else if(X>T->Element)
T->right=Insert(X,T->right);returnT;
}
SearchTree Delete(ElementType X,SearchTree T)
{
Position TmpCell;if(T==NULL)
Error("Error not found");else if(XElement)
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(X,T->right);
}else{
TmpCell=T;if(T->left==NULL)
T=T->right;else if(T->right=NULL)
T=T->left;
free(TmpCell);
}returnT;
}
ElementType Retrieve(Position P)
{if(P==NULL)return -1;else
return P->Element;
}