




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年烷基化工艺作业考试模拟题及解析
- 2025年绿色食品冷链配送服务合作协议
- 2025年汽车配件行业备货协议-新能源汽车关键部件定制合同
- 2025年办公楼搬迁及全方位商务接待与后勤保障服务合同
- 2025年商务酒店翻新合同详列装修项目及验收规范
- 2025年新型商业综合体车位租赁与互联网营销一体化合同
- 2025年度环保设施设备供应与维护合同规范范本
- 二零二五年消防设施班组劳务承揽协议
- 2025年公共机构合同争议规避及应急响应专项服务协议
- 中医院特色病历资料数字化制作与打印外包合作协议
- 住院病人防止走失课件
- 2024年重庆永川区招聘社区工作者后备人选笔试真题
- 医学技术专业讲解
- 2025年临床助理医师考试试题及答案
- 唯奋斗最青春+课件-2026届跨入高三第一课主题班会
- 2025民办中学教师劳务合同模板
- 2025年南康面试题目及答案
- 2025年事业单位考试贵州省毕节地区纳雍县《公共基础知识》考前冲刺试题含解析
- 高中喀斯特地貌说课课件
- 黄冈初一上数学试卷
- 留疆战士考试试题及答案
评论
0/150
提交评论