/* 二叉查找树的基本操作实现测试 */
/* 节点的左子树比节点小, 右子树比节点大 */
#include <cstdio>
#include <stack>
typedef struct _tBINARY_SEARCH_TREE_
{
_tBINARY_SEARCH_TREE_ *lchild;
_tBINARY_SEARCH_TREE_ *rchild;
int value;
}tBINARY_SEARCH_TREE;
tBINARY_SEARCH_TREE *insert_node_binary_search_tree(tBINARY_SEARCH_TREE *&pTree, int value);
tBINARY_SEARCH_TREE *find_min_node(tBINARY_SEARCH_TREE *pTree);
tBINARY_SEARCH_TREE *find_max_node(tBINARY_SEARCH_TREE *pTree);
//建二叉排序树
tBINARY_SEARCH_TREE *create_binary_search_tree(void)
{
int num;
tBINARY_SEARCH_TREE *pTree = NULL;
printf("Input the node value(-999: exit):\n");
scanf("%d", &num);
while (num != -999)
{
insert_node_binary_search_tree(pTree, num);
scanf("%d", &num);
}
return pTree;
}
//向树中插入节点
tBINARY_SEARCH_TREE *insert_node_binary_search_tree(tBINARY_SEARCH_TREE *&pTree, int value)
{
if (pTree == NULL)
{
pTree = new tBINARY_SEARCH_TREE;
pTree->value = value;
pTree->lchild = NULL;
pTree->rchild = NULL;
}
else
{
if (value > pTree->value)
{
pTree->rchild = insert_node_binary_search_tree(pTree->rchild, value);
}
else if (value < pTree->value)
{
pTree->lchild = insert_node_binary_search_tree(pTree->lchild, value);
}
else
; //不考虑重复的值
}
return pTree;
}
//递归中序遍历
void inorder_traveser_binary_search_tree(tBINARY_SEARCH_TREE *pTree)
{
if (pTree != NULL)
{
inorder_traveser_binary_search_tree(pTree->lchild);
printf("%d ", pTree->value);
inorder_traveser_binary_search_tree(pTree->rchild);
}
}
//非递归中序遍历, 借用栈
void inorder_traveser_binary_search_tree2(tBINARY_SEARCH_TREE *pTree)
{
std::stack<tBINARY_SEARCH_TREE *> search_binary_tree_stack;
tBINARY_SEARCH_TREE *pRoot = pTree;
while (pRoot != NULL || !search_binary_tree_stack.empty())
{
if (pRoot != NULL)
{
search_binary_tree_stack.push(pRoot);
pRoot = pRoot->lchild;
}
else
{
pRoot = search_binary_tree_stack.top();
printf("%d ", pRoot->value);
search_binary_tree_stack.pop();
pRoot = pRoot->rchild;
}
}
return;
}
//删除数中的节点
tBINARY_SEARCH_TREE *delete_node_in_binary_search_tree(tBINARY_SEARCH_TREE *&pTree, int value)
{
tBINARY_SEARCH_TREE *tmp;
if(pTree == NULL)
return NULL;
if (value > pTree->value)
{
pTree->rchild = delete_node_in_binary_search_tree(pTree->rchild, value);
}
else if (value < pTree->value)
{
pTree->lchild = delete_node_in_binary_search_tree(pTree->lchild, value);
}
else
{
/* 节点有两孩子时, 找到右子树上的最小值覆盖当前值, 继续递归删除右子树上的最小值 */
if (pTree->lchild != NULL && pTree->rchild != NULL)
{
tmp = find_min_node(pTree->rchild);
pTree->value = tmp->value;
pTree->rchild = delete_node_in_binary_search_tree(pTree->rchild, pTree->value);
}
else
{
tmp = pTree;
if (pTree->lchild == NULL)
pTree = pTree->rchild;
else if (pTree->rchild == NULL)
pTree = pTree->lchild;
delete tmp;
}
}
return pTree;
}
//查找数的最大节点
tBINARY_SEARCH_TREE *find_min_node(tBINARY_SEARCH_TREE *pTree)
{
if (pTree != NULL)
{
while (pTree->lchild != NULL)
{
pTree = pTree->lchild;
}
}
return pTree;
}
//查找树的最大节点
tBINARY_SEARCH_TREE *find_max_node(tBINARY_SEARCH_TREE *pTree)
{
if (pTree == NULL)
return NULL;
else
{
if (pTree->rchild == NULL)
return pTree;
else
return find_max_node(pTree->rchild);
}
}
//销毁二叉排序树
void destory_binary_search_tree(tBINARY_SEARCH_TREE *pTree)
{
if (pTree != NULL)
{
destory_binary_search_tree(pTree->lchild);
destory_binary_search_tree(pTree->rchild);
delete pTree;
pTree = NULL;
}
}
int main(void)
{
printf("create a binary search tree:\n");
tBINARY_SEARCH_TREE *pTree = create_binary_search_tree(); /* 依次输入:9, 6, 13, 2, 7, 12, 15, 1, 3 建树 */
if (pTree != NULL)
{
printf("recursion inorder traverse:\n");
inorder_traveser_binary_search_tree(pTree); /*递归中序遍历输出结果, 1 2 3 6 7 9 12 13 15 */
printf("\n");
printf("no recursion inorder traverse:\n");
inorder_traveser_binary_search_tree2(pTree); /*非递归中序遍历输出结果, 1 2 3 6 7 9 12 13 15 */
printf("\n");
delete_node_in_binary_search_tree(pTree, 7); /* 删除节点7 */
inorder_traveser_binary_search_tree(pTree); /* 输出结果: 1 2 3 6 9 12 13 15 */
printf("\n");
destory_binary_search_tree(pTree);
printf("\n");
}
return 0;
}
二叉排序树的基本操作测试
最新推荐文章于 2022-05-12 00:31:19 发布