1.插入
BinTree Insert( BinTree BST, ElementType X ){ if(!BST){ BST=(BinTree )malloc(sizeof(struct TNode)); BST->Data =X; BST->Left =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; }
2.查找最大值
Position FindMax( BinTree BST ){ if(BST) while(BST->Right ) BST=BST->Right ; return BST; }
3.查找最小值
Position FindMin( BinTree BST ){ if(!BST) return NULL; else if(!BST->Left ) return BST; else return FindMin(BST->Left); }
4.查找
Position Find( BinTree BST, ElementType X ){ if(!BST) return NULL; if(X>BST->Data) return Find(BST->Right ,X); else if(X<BST->Data ) return Find(BST->Left ,X); else return BST; }
5.删除
BinTree Delete( BinTree BST, ElementType X ){ Position Tmp; if(!BST) printf("Not Found "); else{ 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){ Tmp=FindMin(BST->Right ); BST->Data =Tmp->Data ; BST->Right =Delete(BST->Right ,BST->Data ); } else{ Tmp=BST; if(!BST->Left ) BST=BST->Right ; else BST=BST->Left ; free(Tmp); } } } return BST; }