建立空树
void MakeEmpty(SearchTree T) { if ( T != NULL) { MakeEmpty(T->Left); MakeEmpty(T->Right); free(T); } }
查找
Position Find(ElementType X, SearchTree T) { if (T == NULL) return NULL; if (X < T->Element) return Find(X, T->Left); else if (X > T->Element) return Find(x, T->Right); else return T; }
最小值
Position FindMin(SearchTree T) { if (T == NULL) { return NULL; } else if (T->Left == NULL) { return T; } else return FindMin(T-Left); }
插入
SearchTree Insert(ElementType X, SearchTree T) { if (T == NULL) { T == malloc(sizeof(struct TreeNode)); if (T == NULL) { Error("out of space"); } else { T->Element = X; T->Left = T-Right = NULL; } } else if (X < T->Element) { T->Left = Insert(X, T->Left); } else T-Right = Insert(X, T->Right); return T; }
删除
SearchTree DeleteMin(ElementType X, SearchTree T) { Position TmpCell; if (T == NULL) { Error("Not found"); } else if (X < T->Element) 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(T->Element, T->Right); } else { TmpCell = T; if (T->Left == NULL) T = T->Right; else if (R->Right == NULL) T = T->Left; free(TmpCell); } return T; }