二叉排序树的基本操作测试
来源:互联网 发布:腾讯云域名认证失败 编辑:程序博客网 时间:2024/06/04 08:21
/* 二叉查找树的基本操作实现测试 *//* 节点的左子树比节点小, 右子树比节点大 */#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;}
0 0
- 二叉排序树的基本操作测试
- 二叉排序树的基本操作
- 二叉排序树的基本操作
- 二叉排序树的基本操作
- 二叉排序树的基本操作
- 二叉排序树的基本操作
- 二叉排序树的基本操作
- 二叉排序树的基本操作
- 基于二叉排序树的基本操作
- 二叉排序树的基本操作(完整代码)
- 二叉排序树基本操作详解
- *二叉排序树基本操作*
- C++ 二叉排序树基本操作1
- 二叉排序树(二叉查找树)的基本操作
- 数据结构课设--6二叉排序树的基本操作
- 数据结构——二叉排序树的基本操作(BST)
- 二叉排序树的基本算法
- 二叉排序树的操作
- VBS 处理断开excel数据链接格式,只保留值
- 在现有SSH项目中提供REST方式的WebService
- PHP实现装饰模式
- 2013年12月 那年的数据导出
- sass揭秘之@mixin,%,@function
- 二叉排序树的基本操作测试
- Uva 10617 Again Palindrome (区间DP+回文串)
- 当Webservice已经绑定了一个IP后再次修改IP时怎么设置
- x264源码分析 -- x264_slicetype_path
- 第九周项目二 分数的累加 第二种做法
- ArcGIS教程:ArcGIS栅格图像矢量配准
- Content Provider的加载
- JFrame
- 阶乘