全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#includeusing namespace std;templatestruct TreeNode T data; TreeNode *left, *right;templateclass BSTree /: public BinaryTree public: BSTree():root(NULL); BSTree() destroy(root); TreeNode *Find(const T &x);/ TreeNode *Find(const T &x) return Find(root, x) void Delete(TreeNode*& node); void FindAndDelete(const T &x); void Insert(const T &x); / void Insert(const T &x) Insert(root, x); void PreOrder() PreOrder(root); void InOrder() InOrder(root); private: TreeNode *root; void destroy(TreeNode *t); void PreOrder(TreeNode *t); void InOrder(TreeNode *t);/ void Insert(TreeNode* &ptr, const T &x);/ TreeNode *Find(TreeNode *t, const T &x); ;templateTreeNode* BSTree:Find(const T &x) TreeNode *temp = root; while(temp) if(temp-data = x) return temp; else if(temp-data right; else temp = temp-left; return NULL;/查找的递归写法templateTreeNode* BSTree:Find(TreeNode* t, const T &x) if(t = NULL) return NULL; if(t-data = x) return t; else if(t-data x) return Find(t-left, x); else return Find(t-right, x); templatevoid BSTree:Insert(const T &x) TreeNode *p = root, *q = 0; while(p) q = p; if(p-data = x) return; else if(p-data right; else p = p-left; TreeNode *r = new TreeNode; r-data = x; r-left = r-right = NULL; if(root) if(x data) q-left = r; else q-right = r; else root = r;/插入的递归程序templatevoid BSTree:Insert(TreeNode* &ptr, const T &x) if(ptr = NULL) ptr = new TreeNode; ptr-data = x; else if(x data) Insert(ptr-left, x); else Insert(ptr-right, x);templatevoid BSTree:Delete(TreeNode*& node) TreeNode *p, *q=node; if(node-right=0) node=node-left; else if(node-left=0) node=node-right; else p = node; q = node-left; while(q-right) p = q; q = q-right; node-data = q-data; if(p=node) p-left = q-left; else p-right = q-left; delete q;templatevoid BSTree:FindAndDelete(const T &x) TreeNode *node = root, *p = 0; while(node) if(node-data = x) break; p = node; if(node-data right; else node = node-left; if(node != 0 & node-data = x) if(node = root) Delete(root); else if(p-left=node) Delete(p-left); else Delete(p-right); else if(root != 0) coutx不在二叉树中n; else cout二叉树为空n;templatevoid BSTree:destroy(TreeNode *t) if(t) destroy(t-left); destroy(t-right); delete t; templatevoid BSTree:PreOrder(TreeNode *t) if(t) coutdataleft); PreOrder(t-right); templatevoid BSTree:InOrder(TreeNode *t) if(t) InOrder(t-left); coutdataright); int main() BSTree a; a.Insert(50); a.Insert(40); a.Insert(70); a.Insert(10); a.Insert(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 绿色能源项目合作协议
- 劳务合同变更协议
- 2026年福建省宁德市单招职业倾向性测试题库及答案1套
- 2026年资阳口腔职业学院单招综合素质考试必刷测试卷必考题
- 2026年贵州民用航空职业学院单招职业适应性考试题库及答案1套
- 2026年郑州电力职业技术学院单招职业倾向性考试必刷测试卷及答案1套
- 2026年温州职业技术学院单招职业倾向性测试题库带答案
- 2026年潍坊理工学院单招职业倾向性考试必刷测试卷及答案1套
- 常敬涛课件教学课件
- 曲笔课件教学课件
- 北极航道利用中的法律冲突与协调:困境与出路
- 节能硅冶炼工艺优化报告
- 安全员c1证与安全员考试试题及答案
- 龙门吊拆除施工方案
- T-CITSA 57-2025 高速公路基础设施主数据标准
- 昆明巨大变化的概述
- DB37 T 4337-2021 岩溶区桩基施工技术规程
- 2025仁爱版八年级英语上册单词表
- 落地式双排脚手架施工技术交底
- 教招时政热点题目及答案
- 2025年篮球裁判三级理论考试试题及答案
评论
0/150
提交评论