版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、用二叉树实现存储信息 用二叉树实现存储信息.txt老子忽悠孩子叫教 育,孩子忽悠老子叫欺骗,互相忽悠叫代沟。 男人这花花世界,我要用什么颜色来吸引你。#in elude #inelude using n amespaee std;typedef struct _stude ntint id;ehar20 Name;Int Age ;int Math;/(数学成绩);/学生结构体struct treeitemstruct treeitem *lchild;struct treeitem *rchild;struct _stude nt mystude nt;/二叉树结构体class Stude
2、ntpublic:Stude nt(void);Stude nt(void);void Create(struct treeitem *Tnode );/创建void Find(struct treeitem *tree,int i);/ 查看节点是否存在void Search(Stude nt &tree);查询节点voidChan ge(structtreeitem*Tno de,i nti);/ 修改void DeleteNode(struct treeitem *tree,int i);/删除Stude nt *n ode;/根节点private:bool tj;int i_id,i_
3、age,i_math;char c_ch;.cpp=Stude nt:Stude nt(void)tj=false;Student:Stude nt(void)voidStude nt:Create(structtreeitem*Tnode)/ 创建 1ci nid,c_ch,i_age,i_math;if(id!=null&ch!=&age!=null&math!=null)struct _stude nt *Ts;Ts-id=id;Ts-Name=c_ch;Ts-Age=i_age;Ts-Math=i_math;Tno de=(struct treeitem*)malloc(sizeof(
4、structtreeitem);Tno de-mystude nt=Ts;Create(T no de-lchild); /生成左子树Create(T no de-rchild); /生成右子树coutCreate Complated!e ndl;elseTn ode=NULL; coutvNo Create Complated!id=i)tj=true;return;if(tree-lchild匸NULL)f(tree-lchild-id = i)tj=true;return; if(tree-rchild!=NULL)f(tree-rchild-id = i)tj=true;return;
5、Fin d(tree-lchild,i);Fin d(tree-rchild,i);void Student:Search(Student &tree)if(tree!NULL)if(tree-lchild-lchild=NULL&tree-lc hild-rchild=NULL)retur n tree-lchild;elsef(tree-rchild-lchild=NULL&tree-rchi d-rchild=NULL)return tree-rchild; return Search(tree-lchild); return Search(tree-rchild);treeitemvo
6、idStude nt:Change(struct*Tnode, int i)Find (Tnode, i );if(!tj)coutvv无此节点! i_age,i_math,c_ch;Tno de-mystude nt.Age=i_age;Tno de-mystude nt.Name=c_ch;Tno de-mystude nt.Math=i_math;void Stude nt:DeleteNode(struct*tree,i nt i)Fin d(tree,i);if(!tj)cout无点!id= i)/的正好是根节点f(no de-lchild = NULL & node-rchildN
7、ULL)/只有一个根节点的情况treeitem要删除delete node;node=NULL;coutvv已删除节点,当前树为空lchild != NULL & node-rchild =NULL)/有左孩子没右孩子treeitem *p = no de;newtreeitem*dtreeitem(Search( no de)-id);d-lchild = tree-lchild;d-rchild = NULL;node=d;delete p;coutID 值 为vvivv的 节点已 rchild != NULL & node-lchild :NULL)/有右孩子没左孩子treeitem *
8、p = no de;treeitem*dtreeitem(Search( no de)-c onten ts);d-lchild= NULL;d-rchild = tree-rchild;被删除newnode =d;tree-lchild = tree-lchild-rchild;delete p;delete p;点已被删除newdelete p;coutID值 为vvivv的 节vve ndl;else/有左、右孩子treeitem *p = no de;treeitem*dtreeitem(Search( no de)-id);d-lchild = tree-lchild;d-child
9、 = tree-rchild;node=d;coutID 值 为vvivv的节点已被删除 lchild!=NULL)左孩子,且正是要查找的if(tree-lchild-id = i)/子是要查找的if(tree-lchild-lchildtree-lchild-rchild != NULL)不是根节点,树还有若根节点的左孩= NULL &treeitem *p = tree-lchild;coutID 值 为vvivv的节点已被删除lchild-lchild tree-lchild-rchild = NULL) delete tree-lchild;tree-lchild = NULL;cou
10、tID值 为vvivv的节点已被删除lchild-lchild!= NULL &tree-lchild-rchild = NULL)treeitem *p = tree-lchild;delete tree-rchild;d-rchild = tree-lchild-rchild;tree-lchild = tree-lchild-lchild;delete p;coutID值 为vvivv的节点已被删除lchild-lchild!= NULL &tree-lchild-rchild != NULL)treeitem *p = tree-lchild;treeitem*d=newtreeite
11、m(Search( no de)-id);d-left = tree-lchildt-lchild;tree-lchild = d;delete p;return;f(tree-rchild匸NULL)/右孩子,且正是要查找的不是根节点,树还有f(tree-rchild-id = i)NULL &f(tree-rchild-lchild tree-rchild-rchild = NULL)tree-rchild = NULL;coutID值 为vvivv的节点已被删除vve ndl;elsef(tree-rchild-lchild!= NULL &tree-rchild-rchild = NU
12、LL)treeitem *p = tree-rchild;tree-rchild = tree-rchild-lchild;delete p;coutID 值为vvivv的节点已被删除 rchild-child= NULL &tree-rchild-rchild != NULL)treeitem *p = tree-rchild;tree-rchild = tree-rchild-rchild;delete p;coutID 值 为vvivv的节点已被删除rchild-lchild tree-rchild-rchild != NULL)treeitem *p = tree-rchild;treeitem*dnew
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 13.1《林教头风雪山神庙》课件统编版高一语文必修下册
- 2026中国伺服助力器行业现状规模与应用前景预测报告
- 2026中国电火锅市场竞争态势与营销趋势预测报告
- 2025-2030中国可持续金融行业市场发展现状分析及发展趋势与投资前景研究报告
- 2025新全国安全生产月知识题库(附含答案)
- 2026年肉类加工技术研发合同协议
- 2026冷链物流对葡萄干品质保障的影响评估报告
- 2026农产品质量安全检测行业市场竞争现状深度研究及食品安全发展趋势报告
- 2026农业科技园区建设运营模式资源利用率与投资效益分析研究
- 2026农业电商行业市场竞争格局分析及投资评估发展前景规划研究
- 2024-2025学年四川省成都市石室联中教育集团八年级(下)期中数学试卷
- 小学科学教学中的跨学科融合创新实践研究教学研究课题报告
- 【物理】2026郑州高三二模试题及答案
- 《AQ3067-2026化工和危险化学品重大生产安全事故隐患判定准则》解读
- 施工电梯基础监理实施细则
- 2026 年山东春考英语提分技巧全解
- 国开2026年《公共政策概论》形成性考核任务1-4答案
- 消防工程从入门到精通
- YDT 5102-2024 通信线路工程技术规范
- 沙漠掘金(经典版)-沙漠掘金攻略
- 教科版四年级科学下册3《观察土壤》优质教案(2套)
评论
0/150
提交评论