1.头文件及类型定义
#include<stdio.h>
#include<stdlib.h>
#define ElemType int
2.二叉排序树结点类型定义
typedef struct BSTNode {
ElemType data;
struct BSTNode* lchild, * rchild;
}BSTNode, * BSTree;
3.函数声明
BSTNode* BST_Search(BSTree T, ElemType key);
BSTNode* BSTSearch(BSTree T, ElemType key);
bool BST_Insert(BSTree& T, ElemType key);
bool BSTInsert(BSTree& T, ElemType key);
void Create_BST(BSTree& T, ElemType arr[], int n);
BSTNode* FirstNode(BSTNode* p);
BSTNode* LastNode(BSTNode* p);
BSTNode* BST_Delete1(BSTree& T, ElemType key);
BSTNode* BST_Delete2(BSTree& T, ElemType key);
void InOrder(BSTree T);
4.基本操作
4.1 查找操作
4.1.1 非递归查找
BSTNode* BST_Search(BSTree T, ElemType key) {
while (T != NULL && key != T->data) {
if (key < T->data)
T = T->lchild;
else
T = T->rchild;
}
return T;
}
4.1.2 递归查找
BSTNode* BSTSearch(BSTree T, ElemType key) {
if (T == NULL)
return NULL;
if (key == T->data)
return T;
else if (key < T->data)
return BSTSearch(T->lchild, key);
else
return BSTSearch(T->rchild, key)