二叉排序树
定义
- 二叉排序树也叫二叉搜索树,二叉排序树的左子树上的节点都小于根节点,右子树上的节点都大于根节点,每棵子树都是二叉排序树
二叉排序树实现
结构体定义
#define SUCCESS 0
#define FAILURE -1
typedef int ElemType;
struct BSTNode {
ElemType elem;
struct BSTNode *lchild, *rchild;
};
typedef struct BSTNode* BSTree;
静态函数列表
static struct BSTNode* create_bstnode(ElemType elem);
static int remove_node(BSTree *ptree);
基本函数声明
BSTree init_empty_bstree(void);
int insert_bstree(BSTree *ptree, ElemType elem);
int insert_recursion_bstree(BSTree *ptree, ElemType elem);
int remove_bstree(BSTree *ptree, ElemType elem);
int remove_recursion_bstree(BSTree *ptree, ElemType elem);
int search_bstree(BSTree tree, ElemType elem);
int search_recursion_bstree(BSTree tree, ElemType elem);
size_t size_bstree(BSTree tree);
size_t height_bstree(BSTree tree);
bool empty_bstree(BSTree tree);
void clear_bstree(BSTree *ptree);
void prev_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
void prev_bstree(BSTree tree, void (*foreach)(ElemType));
void mid_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
void mid_bstree(BSTree tree, void (*foreach)(ElemType));
void back_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
void back_bstree(BSTree tree, void (*foreach)(ElemType));
void layer_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
静态函数实现
创建一个节点 create_bstnode
static struct BSTNode* create_bstnode(ElemType elem)
{
struct BSTNode *node = (struct BSTNode*)malloc(sizeof(struct BSTNode));
if (node != NULL) {
node->elem = elem;
node->lchild = NULL;
node->rchild = NULL;
}
return node;
}
删除ptree节点 remove_node
static int remove_node(BSTree *ptree)
{
if ((*ptree)->lchild == NULL && (*ptree)->rchild == NULL) {
free(*ptree);
*ptree = NULL;
} else if ((*ptree)->lchild == NULL || (*ptree)->rchild == NULL) {
struct BSTNode *node = *ptree;
*ptree = node->lchild != NULL ? node->lchild : node->rchild;
free(node);
} else {
struct BSTNode *node = *ptree;
for (ptree = &(*ptree)->lchild; (*ptree)->rchild != NULL; ptree = &(*ptree)->rchild);
node->elem = (*ptree)->elem;
remove_node(ptree);
}
return SUCCESS;
}
基本函数实现
初始化为空 (好看) init_empty_bstree
BSTree init_empty_bstree(void)
{
return NULL;
}
非递归插入元素 insert_bstree
int insert_bstree(BSTree *ptree, ElemType elem)
{
assert(ptree != NULL);
while (*ptree != NULL) {
if (elem < (*ptree)->elem) {
ptree = &(*ptree)->lchild;
} else if (elem > (*ptree)->elem) {
ptree = &(*ptree)->rchild;
} else {
return FAILURE;
}
}
*ptree = create_bstnode(elem);
if (*ptree == NULL) {
return FAILURE;
}
return SUCCESS;
}
递归插入元素 insert_recursion_bstree
int insert_recursion_bstree(BSTree *ptree, ElemType elem)
{
assert(ptree != NULL);
if (*ptree == NULL) {
*ptree = create_bstnode(elem);
if (*ptree == NULL) {
return FAILURE;
}
return SUCCESS;
}
if (elem < (*ptree)->elem) {
return insert_recursion_bstree(&(*ptree)->lchild, elem);
} else if (elem > (*ptree)->elem) {
return insert_recursion_bstree(&(*ptree)->rchild, elem);
}
return FAILURE;
}
非递归删除元素 remove_bstree
int remove_bstree(BSTree *ptree, ElemType elem)
{
assert(ptree != NULL);
while (*ptree != NULL) {
if (elem < (*ptree)->elem) {
ptree = &(*ptree)->lchild;
} else if (elem > (*ptree)->elem) {
ptree = &(*ptree)->rchild;
} else {
break;
}
}
if (*ptree == NULL) {
return FAILURE;
}
return remove_node(ptree);
}
递归删除元素 remove_recursion_bstree
int remove_recursion_bstree(BSTree *ptree, ElemType elem)
{
assert(ptree != NULL);
if (*ptree == NULL) {
return FAILURE;
}
assert(ptree != NULL);
if (elem < (*ptree)->elem) {
return remove_recursion_bstree(&(*ptree)->lchild, elem);
} else if (elem > (*ptree)->elem) {
return remove_recursion_bstree(&(*ptree)->rchild, elem);
}
return remove_node(ptree);
}
非递归查找 search_bstree
int search_bstree(BSTree tree, ElemType elem)
{
while (tree != NULL) {
if (elem < tree->elem) {
tree = tree->lchild;
} else if (elem > tree->elem) {
tree = tree->rchild;
} else {
return SUCCESS;
}
}
return FAILURE;
}
递归查找 search_recursion_bstree
int search_recursion_bstree(BSTree tree, ElemType elem)
{
if (tree == NULL) {
return FAILURE;
}
if (elem < tree->elem) {
return search_recursion_bstree(tree->lchild, elem);
} else if (elem > tree->elem) {
return search_recursion_bstree(tree->rchild, elem);
}
return SUCCESS;
}
树结点数 size_bstree
size_t size_bstree(BSTree tree)
{
if (tree == NULL) {
return 0;
}
return size_bstree(tree->lchild) + 1 + size_bstree(tree->rchild);
}
树高度 height_bstree
size_t height_bstree(BSTree tree)
{
if (tree == NULL) {
return 0;
}
size_t lh = height_bstree(tree->lchild) + 1;
size_t rh = height_bstree(tree->rchild) + 1;
return lh > rh ? lh : rh;
}
树是否为空 empty_bstree
bool empty_bstree(BSTree tree)
{
return tree == NULL;
}
清除 clear_bstree
void clear_bstree(BSTree *ptree)
{
assert(ptree != NULL);
if (*ptree != NULL) {
clear_bstree(&(*ptree)->lchild);
clear_bstree(&(*ptree)->rchild);
free(*ptree);
*ptree = NULL;
}
}
递归前序遍历 prev_foreach_bstree
void prev_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree != NULL) {
foreach(tree->elem);
prev_foreach_bstree(tree->lchild, foreach);
prev_foreach_bstree(tree->rchild, foreach);
}
}
非递归前序遍历 prev_bstree
void prev_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree == NULL) {
return;
}
ST st = create_stack(sizeof(struct BSTNode*));
push_stack(st, &tree);
while (!empty_stack(st)) {
struct BSTNode *node = NULL;
pop_stack(st, &node);
foreach(node->elem);
if (node->rchild) {
push_stack(st, &node->rchild);
}
if (node->lchild) {
push_stack(st, &node->lchild);
}
}
destroy_stack(st);
}
递归中序遍历 mid_foreach_bstree
void mid_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree != NULL) {
mid_foreach_bstree(tree->lchild, foreach);
foreach(tree->elem);
mid_foreach_bstree(tree->rchild, foreach);
}
}
非递归中序遍历 mid_bstree
void mid_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree == NULL) {
return;
}
ST st = create_stack(sizeof(struct BSTNode*));
struct BSTNode *node = tree;
while (node != NULL || !empty_stack(st)) {
while (node != NULL) {
push_stack(st, &node);
node = node->lchild;
}
pop_stack(st, &node);
foreach(node->elem);
node = node->rchild;
}
destroy_stack(st);
}
递归后序遍历 back_foreach_bstree
void back_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree != NULL) {
back_foreach_bstree(tree->lchild, foreach);
back_foreach_bstree(tree->rchild, foreach);
foreach(tree->elem);
}
}
非递归后序遍历 back_bstree
void back_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree == NULL) {
return;
}
ST st1 = create_stack(sizeof(struct BSTNode*));
ST st2 = create_stack(sizeof(ElemType));
struct BSTNode *node = tree;
push_stack(st1, &node);
while (!empty_stack(st1)) {
pop_stack(st1, &node);
push_stack(st2, &node->elem);
if (node->lchild != NULL) {
push_stack(st1, &node->lchild);
}
if (node->rchild != NULL) {
push_stack(st1, &node->rchild);
}
}
ElemType elem = 0;
while (!empty_stack(st2)) {
pop_stack(st2, &elem);
foreach(elem);
}
destroy_stack(st1);
destroy_stack(st2);
}
层序遍历 layer_foreach_bstree
void layer_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
assert(foreach != NULL);
if (tree == NULL) {
return;
}
QE que = create_queue(sizeof(struct BSTNode*));
push_queue(que, &tree);
while (!empty_queue(que)) {
BSTree node = NULL;
pop_queue(que, &node);
foreach(node->elem);
if (node->lchild != NULL) {
push_queue(que, &(node->lchild));
}
if (node->rchild != NULL) {
push_queue(que, &(node->rchild));
}
}
destroy_queue(que);
}