
二叉排序树基本操作的实现:****姓名:****学号:****日期:一疑问描述二需求预测三概要设计四详细设计五测试预测六源程序清单七用户使用指南八经验感受一、问题表述从键盘调用一组数据创建二叉排序树并对其进行查找、遍历、格式化输出操作。二、需求分析读入给定的数据构造二叉排序树实现初始化。给定数据的格式为第一行为元素个数遇退出程序。提供菜单功能选项包含查找、插入、删除和打印等。三、概要设计数据结构:structnode{intnumnode*chl二叉排序树 数据结构,*chr}分别定义了指向左右子树的指针。函数介绍voidInsert(constinttemp,node**root)voidDelete(constintkey,node**p)boolFind(constintkey,node*p,node*net,intdepth)voidPrint(constnode*p)boolMenu(node**root)函数如其名功能也亦如其名。另外为了避免边界状况如空树以及没有指定元素的非法删除或者查找但是会在变量内部直接进行判定或者状态返回判别等。函数变量部分字段使用引用的方法进行释放也有些使用的是指针的指针这样保传入的指针指向的内容可以被设置以及变量返回然后可以再次指向原有内容。

四、详细设计voidInsert(constinttemp,node**root){if(*root==){*root=newnode(*root)>chl=(*root)>chr=(*root)>num=temp}elseif((*root)>numchr)elseInsert(temp,(*root)>chl)}插入数组使用泛型的方法进行插入并动态建立对象。voidDelete(constintkey,node**p){if(*p==){coutchr==){temp=*p*p=(*p)>chldeletetemp}else{temp=(*p)>chlnode*front=while(temp>chr){front=temptemp=temp>chr}front>num=temp>numif(front!=temp)front>chr=temp>chlelsefront>chl=temp>chldeletetemp}}elseif((*p)>num>key)Delete(key,(*p)>chl)elseDelete(key,(*p)>chr)}删除数组。按照规则分三种状况进行删除最后都会销毁指针指向的对象。

如果某个元素不在其中如此最终指向的指针必然为空。另外之前没考虑到删除成功的状况返回情况其实上面使用了一个全局变量来作为补救标记。boolFind(constintkey,node*p,node*net,intdepth){if(p==)returnfalsedepthnet=pif(p>num==key)returntrueelseif(p>num>key)returnFind(key,p>chl,net,depth)elsereturnFind(key,p>chr,net,depth)}查找变量参数*net用以指向当前非常的指针但却没有具体推动输出其指向的信息认为只要输出对本程序意义不大所以没有具体关注。depth为查找应该的次数。voidPrint(constnode*p){if(p==)returncout

numchr)}只提供了中序遍历输出的状况其他情况没考量。五、测试预测测试环境:Code::Blocks输入过程:课本提供数据:两种查找情况删除操作插入操作六、源程序清单#include#include#include#include#includeusingnamespacestdstructnode{intnumnode*chl,*chr}boolflagdelifstreamin("sorttxt")voidInsert(constinttemp,node**root){if(*root==){*root=newnode(*root)>chl=(*root)>chr=(*root)>num=temp}elseif((*root)>numchr)elseInsert(temp,(*root)>chl)}voidDelete(constintkey,node**p){if(*p==){coutchr==){temp=*p*p=(*p)>chldeletetemp}else{temp=(*p)>chlnode*front=while(temp>chr){front=temptemp=temp>chr}front>num=temp>numif(front!=temp)front>chr=temp>chlelsefront>chl=temp>chldeletetemp}}elseif((*p)>num>key)Delete(key,(*p)>chl)elseDelete(key,(*p)>chr)}boolFind(constintkey,node*p,node*net,intdepth){if(p==)returnfalsedepthnet=pif(p>num==key)returntrueelseif(p>num>key)returnFind(key,p>chl,net二叉排序树 数据结构,depth)elsereturnFind(key,p>chr,net,depth)}voidPrint(constnode*p){if(p==)returncout

numchr)}boolMenu(node**root){intchoice,num,depthnode*netboolsuccoutchoiceif(choice==)returnfalsecoutnumInsert(num,(*root))cout
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-121390-1.html
美选择退出
人不犯我
不会再收美帝的剥削
感觉是美国品牌评选