用c++实现平衡二叉排序树的各种算法 用函数实现如下(平衡)二叉排序树算法: (1) 插入新结点 (2)前序、中序、后序遍历二叉树(递归)(3)前序、中序、后序遍历的非递归算法(4)层次遍历二叉树 (5)在二叉树中查找给定关键字(函数返回值为成功1. (6)交换各结点的左右子树 求二叉树的深度(8)叶子结点数 (9)删除某结点。完整代码

时间: 2023-10-28 11:04:34 浏览: 29
抱歉,我并不能为您提供完整代码,但是我可以为您提供实现平衡二叉排序树的各种算法的代码实现,您可以参考并自行编写完整代码。 ```c++ #include <iostream> #include <queue> using namespace std; // 定义树节点结构体 struct TreeNode { int val; int height; // 节点高度 TreeNode* left; TreeNode* right; TreeNode(int v): val(v), height(1), left(NULL), right(NULL) {} }; // 计算节点高度 int getHeight(TreeNode* root) { if (root == NULL) return 0; return root->height; } // 计算节点平衡因子 int getBalanceFactor(TreeNode* root) { return getHeight(root->left) - getHeight(root->right); } // 更新节点高度 void updateHeight(TreeNode* root) { root->height = max(getHeight(root->left), getHeight(root->right)) + 1; } // 右旋 void rightRotate(TreeNode*& root) { TreeNode* tmp = root->left; root->left = tmp->right; tmp->right = root; updateHeight(root); updateHeight(tmp); root = tmp; } // 左旋 void leftRotate(TreeNode*& root) { TreeNode* tmp = root->right; root->right = tmp->left; tmp->left = root; updateHeight(root); updateHeight(tmp); root = tmp; } // 平衡二叉树插入节点 void insert(TreeNode*& root, int val) { if (root == NULL) { root = new TreeNode(val); return; } if (val < root->val) { insert(root->left, val); updateHeight(root); if (getBalanceFactor(root) == 2) { if (getBalanceFactor(root->left) == 1) { rightRotate(root); } else { leftRotate(root->left); rightRotate(root); } } } else { insert(root->right, val); updateHeight(root); if (getBalanceFactor(root) == -2) { if (getBalanceFactor(root->right) == -1) { leftRotate(root); } else { rightRotate(root->right); leftRotate(root); } } } } // 递归前序遍历 void preOrder(TreeNode* root) { if (root == NULL) return; cout << root->val << " "; preOrder(root->left); preOrder(root->right); } // 递归中序遍历 void inOrder(TreeNode* root) { if (root == NULL) return; inOrder(root->left); cout << root->val << " "; inOrder(root->right); } // 递归后序遍历 void postOrder(TreeNode* root) { if (root == NULL) return; postOrder(root->left); postOrder(root->right); cout << root->val << " "; } // 非递归前序遍历 void preOrderNonRecursion(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> st; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); st.pop(); cout << node->val << " "; if (node->right != NULL) st.push(node->right); if (node->left != NULL) st.push(node->left); } } // 非递归中序遍历 void inOrderNonRecursion(TreeNode* root) { stack<TreeNode*> st; TreeNode* node = root; while (node != NULL || !st.empty()) { while (node != NULL) { st.push(node); node = node->left; } node = st.top(); st.pop(); cout << node->val << " "; node = node->right; } } // 非递归后序遍历 void postOrderNonRecursion(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> st1, st2; st1.push(root); while (!st1.empty()) { TreeNode* node = st1.top(); st1.pop(); st2.push(node); if (node->left != NULL) st1.push(node->left); if (node->right != NULL) st1.push(node->right); } while (!st2.empty()) { TreeNode* node = st2.top(); st2.pop(); cout << node->val << " "; } } // 层次遍历 void levelOrder(TreeNode* root) { if (root == NULL) return; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { TreeNode* node = q.front(); q.pop(); cout << node->val << " "; if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); } cout << endl; } } // 查找节点 bool search(TreeNode* root, int val) { if (root == NULL) return false; if (root->val == val) return true; if (val < root->val) return search(root->left, val); else return search(root->right, val); } // 查找最小节点 TreeNode* findMin(TreeNode* root) { while (root->left != NULL) root = root->left; return root; } // 删除节点 void remove(TreeNode*& root, int val) { if (root == NULL) return; if (val < root->val) { remove(root->left, val); updateHeight(root); if (getBalanceFactor(root) == -2) { if (getBalanceFactor(root->right) == -1) { leftRotate(root); } else { rightRotate(root->right); leftRotate(root); } } } else if (val > root->val) { remove(root->right, val); updateHeight(root); if (getBalanceFactor(root) == 2) { if (getBalanceFactor(root->left) == 1) { rightRotate(root); } else { leftRotate(root->left); rightRotate(root); } } } else { if (root->left == NULL && root->right == NULL) { delete root; root = NULL; } else if (root->left == NULL || root->right == NULL) { TreeNode* tmp = root; root = (root->left != NULL) ? root->left : root->right; delete tmp; } else { TreeNode* tmp = findMin(root->right); root->val = tmp->val; remove(root->right, tmp->val); updateHeight(root); if (getBalanceFactor(root) == 2) { if (getBalanceFactor(root->left) == 1) { rightRotate(root); } else { leftRotate(root->left); rightRotate(root); } } } } } // 交换各节点的左右子树 void swap(TreeNode* root) { if (root == NULL) return; swap(root->left); swap(root->right); TreeNode* tmp = root->left; root->left = root->right; root->right = tmp; } // 计算二叉树深度 int getDepth(TreeNode* root) { if (root == NULL) return 0; int leftDepth = getDepth(root->left); int rightDepth = getDepth(root->right); return max(leftDepth, rightDepth) + 1; } // 计算二叉树叶子节点数 int getLeafCount(TreeNode* root) { if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 1; return getLeafCount(root->left) + getLeafCount(root->right); } // 测试 int main() { TreeNode* root = NULL; insert(root, 5); insert(root, 3); insert(root, 7); insert(root, 2); insert(root, 4); insert(root, 6); insert(root, 8); cout << "递归前序遍历结果:"; preOrder(root); cout << endl; cout << "递归中序遍历结果:"; inOrder(root); cout << endl; cout << "递归后序遍历结果:"; postOrder(root); cout << endl; cout << "非递归前序遍历结果:"; preOrderNonRecursion(root); cout << endl; cout << "非递归中序遍历结果:"; inOrderNonRecursion(root); cout << endl; cout << "非递归后序遍历结果:"; postOrderNonRecursion(root); cout << endl; cout << "层次遍历结果:" << endl; levelOrder(root); int val = 6; bool isExist = search(root, val); cout << val << (isExist ? " 存在于" : " 不存在于") << "二叉树中" << endl; swap(root); cout << "交换各节点的左右子树后,层次遍历结果:" << endl; levelOrder(root); int depth = getDepth(root); cout << "二叉树深度为:" << depth << endl; int leafCount = getLeafCount(root); cout << "二叉树叶子节点数为:" << leafCount << endl; int delVal = 3; remove(root, delVal); cout << "删除节点 " << delVal << " 后,层次遍历结果:" << endl; levelOrder(root); return 0; } ```

相关推荐

最新推荐

recommend-type

华为OD机试D卷 - 用连续自然数之和来表达整数 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

Screenshot_2024-05-10-20-21-01-857_com.chaoxing.mobile.jpg

Screenshot_2024-05-10-20-21-01-857_com.chaoxing.mobile.jpg
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

输出这段Python代码输出所有3位整数中,个位是5且是3的倍数的整数

``` for i in range(100,1000): if i%10 == 5 and i%3 == 0: print(i) ``` 输出结果: ``` 105 135 165 195 225 255 285 315 345 375 405 435 465 495 525 555 585 615 645 675 705 735 765 795 825 855 885 915 945 975 ```