二叉树——数据结构课堂作业
↖(^ω^)↗
![](/assets/blank.gif)
![](/assets/blank.gif)
1 /* 2 3 编写算法函数void preorder1(bintree t)实现二叉树t的非递归前序遍历。 4 5 */ 6 7 #include "bintree.h" 8 char *a="ABC##D#E##F##"; /*扩充二叉树序树t的前序序列*/ 9 10 /*函数preorder1()的功能是非递归前序遍历二叉树t,请将函数补充完整并调试运行*/ 11 void preorder1(bintree t) 12 { 13 bintree p = t; 14 15 seqstack s; 16 init(&s); 17 while(p!=NULL||!empty(&s)) 18 { 19 20 while(p!=NULL) 21 { 22 printf("%c",p->data); 23 push(&s,p); 24 p = p->lchild; 25 } 26 27 if(!empty(&s)) 28 { 29 p=pop(&s); 30 p=p->rchild; 31 } 32 } 33 } 34 35 int main() 36 { 37 bintree t; 38 t=creatbintree(); /*建立二叉树t的存储结构*/ 39 printf("二叉树的前序序列为:\n"); 40 //preorder(t); 41 preorder1(t); /*前序非递归遍历二叉树*/ 42 return 0; 43 }
实现二叉树t的非递归前序遍历
![](/assets/blank.gif)
![](/assets/blank.gif)
1 /* 2 编写算法函数void levelbintree(bintree t),实现二叉树的层次遍历。 3 */ 4 5 #include "bintree.h" 6 #include "queue" 7 8 using namespace std; 9 10 char *a="ABC##D#E##F##"; /*扩充二叉树序树t的前序序列*/ 11 void levelbintree(bintree t) 12 { 13 queue<bintree> q; 14 q.push(t); 15 16 while(!q.empty()) { 17 bintree s = q.front(); 18 q.pop(); 19 printf("%c",s->data); 20 if(s->lchild!=NULL) { 21 q.push(s->lchild); 22 } 23 if(s->rchild!=NULL) { 24 q.push(s->rchild); 25 } 26 27 } 28 } 29 30 int main() 31 { bintree t; 32 t=creatbintree(); /*建立二叉树t的存储结构*/ 33 printf("二叉树的层次序列为:\n"); 34 levelbintree(t); /*层次遍历二叉树*/ 35 return 0; 36 }
实现二叉树的层次遍历
![](/assets/blank.gif)
![](/assets/blank.gif)
1 /* 2 编写函数bintree prelist(bintree t),bintree postfirst(bintree t), 3 分别返回二叉树t在前序遍历下的最后一个结点地址和后序遍历下的第一个结点地址。 4 */ 5 6 #include "bintree.h" 7 char *a="ABC##D##EF#G###"; /*扩充二叉树序树t的前序序列*/ 8 9 10 bintree prelast(bintree t) 11 { 12 while(t->lchild!=NULL||t->rchild!=NULL) 13 { 14 if(t->rchild!=NULL) 15 while(t->rchild!=NULL) 16 t = t->rchild; 17 if(t->lchild!=NULL) 18 t= t->lchild; 19 } 20 return t; 21 22 } 23 24 bintree postfirst(bintree t) 25 { 26 while(t->lchild!=NULL||t->rchild!=NULL) 27 { 28 if(t->lchild!=NULL) 29 while(t->lchild!=NULL) 30 t = t->lchild; 31 32 if(t->rchild!=NULL) 33 t= t->rchild; 34 } 35 return t; 36 37 } 38 39 int main() 40 { 41 bintree t,p,q; 42 t=creatbintree(); /*建立二叉树t的存储结构*/ 43 // preorder(t); 44 p=prelast(t); 45 q=postfirst(t); 46 if (t!=NULL) 47 { 48 printf("前序遍历最后一个结点为:%c\n",p->data); 49 printf("后序遍历第一个结点为:%c\n",q->data); 50 } 51 else printf("二叉树为空!"); 52 return 0; 53 }
分别返回二叉树t在前序遍历下的最后一个结点地址和后序遍历下的第一个结点地址
![](/assets/blank.gif)
![](/assets/blank.gif)
1 /* 2 假设二叉树采用链式方式存储,t为其根结点,编写一个函数int Depth(bintree t, char x),求值为x的结点在二叉树中的层次。 3 */ 4 #include "bintree.h" 5 char *a="ABC##D##EF#G###"; /*扩充二叉树序树t的前序序列*/ 6 7 /* 8 函数Depth,功能:求结点x所在的层次 9 */ 10 11 int d; 12 int dfs(bintree t,char x,int level) 13 { 14 if(t->data==x) 15 { 16 d = level; 17 } 18 if(t->lchild!=NULL) 19 { 20 dfs(t->lchild,x,level+1); 21 } 22 if(t->rchild!=NULL) 23 { 24 dfs(t->rchild,x,level+1); 25 } 26 } 27 28 int Depth(bintree t,char x) 29 { 30 dfs(t,x,0); 31 return d; 32 } 33 34 int main() 35 { 36 bintree root; 37 char x; 38 int k=0; 39 root=creatbintree(); 40 printf("请输入树中的1个结点值:\n"); 41 scanf("%c",&x); 42 k=Depth(root,x); 43 printf("%c结点的层次为%d\n",x,k); 44 }
求值为x的结点在二叉树中的层次
![](/assets/blank.gif)
![](/assets/blank.gif)
1 /* 2 试编写一个函数,将一棵给定二叉树中所有结点的左、右子女互换。 3 */ 4 #include "bintree.h" 5 #include "algorithm" 6 7 using namespace std; 8 9 char *a="ABC##D##EF#G###"; /*扩充二叉树序树t的前序序列*/ 10 /*请将本函数补充完整,并进行测试*/ 11 12 void change(bintree t) 13 { 14 if(t->lchild!=NULL&&t->rchild!=NULL) { 15 swap(t->lchild,t->rchild); 16 change(t->lchild); 17 change(t->rchild); 18 } 19 20 } 21 22 int main() 23 { 24 bintree root; 25 root=creatbintree(); 26 change(root); 27 preorder(root); 28 }
给定二叉树中所有结点的左、右子女互换
![](/assets/blank.gif)
![](/assets/blank.gif)
1 /* 2 试编写一个递归函数bintree buildBintree(char *pre, char *mid, int length), 3 根据二叉树的前序序列pre、中序序列mid和前序序列长度length,构造二叉树的二叉链表存储结构, 4 函数返回二叉树的树根地址。 5 */ 6 7 #include "bintree.h" 8 #include <string.h> 9 char *a=""; 10 /*请将本函数补充完整,并进行测试*/ 11 12 char pre[100],in[100]; 13 14 int find(int l,int r,char ch) 15 { 16 for(int i=l;i<r;i++) 17 if(in[i] == ch) 18 return i; 19 return -1; 20 } 21 22 void make(int prel,int prer,int inl,int inr) 23 { 24 if(prel>=prer) 25 return ; 26 if(prel==prer-1) 27 { 28 printf("%c",pre[prel]); 29 return ; 30 } 31 32 int pos = find(inl,inr,pre[prel]); 33 34 make(prel+1,prel+pos-inl+1,inl,pos); 35 make(prel+pos-inl+1,prer,pos+1,inr); 36 printf("%c",pre[prel]); 37 } 38 39 40 bintree buildBintree(char *pre, char *mid,int length) 41 { 42 make(0,length,0,length); 43 puts(""); 44 } 45 46 int main() 47 { 48 bintree root; 49 50 puts("请输入前序序列:"); 51 gets(pre); 52 puts("请输入中序序列:"); 53 gets(in); 54 puts("后序序列是:"); 55 //root=buildBintree(pre,mid,strlen(pre)); 56 buildBintree(pre,in,strlen(pre)); 57 58 //postorder(root); 59 }
根据二叉树的前序序列pre、中序序列mid和前序序列长度length,构造二叉树的二叉链表存储结构
转载于:https://www.cnblogs.com/TreeDream/p/6209279.html
二叉树——数据结构课堂作业相关推荐
- 软件测试课堂作业+期末整理【超实用】
疫情期间在家结课,所用教材(第一本),辅助自学教材(第二本,MOOC上武剑洁老师的配套教材). 虽说MOOC是国家精品课,但个人觉得"内容太少.太理论"了. 我们大学老师和MOOC ...
- 数据结构大作业--迷宫问题
数据结构大作业–迷宫问题 上图: 代码有详细注解 迷宫自动生成问题单独讨论 当然由于出口随机的原因右小概率会出现bug(这无伤大雅) 首先我们都知道,迷宫只有一条正确的道路. 这个时候请把自己想象成一 ...
- C/C++《数据结构大作业》
C/C++<数据结构大作业> 要求 一.作业目的 1. 掌握用数据结构的知识进行程序设计. 2. 应用所学的数据结构完成一个具有一定实际意义的应用程序的设计.编码.调试,锻炼实践动手能力, ...
- CC++《数据结构大作业》
C/C++<数据结构大作业> 要求 一.作业目的 1. 掌握用数据结构的知识进行程序设计. 2. 应用所学的数据结构完成一个具有一定实际意义的应用程序的设计.编码.调试,锻炼实践动手能力, ...
- latex 小于_一份菜鸡的Latex课堂作业works--(ii)
开学了,每天的课堂作业接踵而来.这是一份包含插入代码块和图片的作业. 先来一波图 documentclass{article} usepackage[UTF8]{ctex} usepackage{fa ...
- 软件工程概论课堂作业3
题目:返回一个整数数组中最大子数组的和 要求: 输入一个一维整形数组,数组里有正数也有负数. 一维数组首尾相接,象个一条首尾相接带子一样. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个 ...
- 二叉树----数据结构:二叉树的三种遍历及习题
二叉树----数据结构:二叉树的三种遍历,利用递归算法. 关于二叉树的遍历,应用非常广泛,不单单是访问打印结点,还可以进行一系列的操作,如赋值.删除.查找.求二叉树的深度等等. 有递归和非递归两种算法 ...
- 堆栈在DNA计算机中的应用,堆栈和二叉树数据结构在DNA计算机中的设计与实现
摘要: 自从1994年Adleman用DNA链解决一个简单有向哈密顿图问题以来,DNA计算已经形成国际科学前沿领域内研究的一个新的热点,引起许多不同学科学者们的兴趣.DNA计算机的研制需要诸如生物工程 ...
- 高级软件测试技术17秋第1次课堂作业小结
第1次课堂作业小结 1. 作业要求 2017年11月13日,周一,在课堂上布置了两个作业,其中,针对每班1-3组以及7班学号前15名同学安排的是编程作业,要求现场限时(15分钟内)根据指定的 ...
最新文章
- R语言ggplot2可视化发散型条形图、发散条形图(Diverging Bars)是一种可以同时处理负值和正值的条形图、并按照大小排序区分数据(Diverging Bars)
- linux下的PHP+Mysql+PHPWind实现
- 关于truncate与checkident
- JavaScript面试时候的坑洼沟洄——表达式与运算符
- go定时器 每天重复_Go 简单性的价值:来自对 Go 倍加青睐的谷歌软件工程师的自述...
- iOS开发之oc(五)--成员变量(实例变量)
- 突发!甲骨文严查Java授权,很多公司连夜卸载了JDK,启用OpenJDK 替代....
- swagger如何屏蔽某些接口,不对外公开--使用@ApiIgnore
- mysql node 可视化_使用Prometheus进行Substrate节点可视化监控
- Ubuntu下QT的安装配置
- Edge浏览器支持IE内核 / 增加Edge兼容性
- 关于new pad利用iPad12的资源缩放的问题 contentScaleFactor设置
- 转载:Java导出数据到Excel
- 设计模式之简单化_Mediator中介者模式_只有一个仲裁者
- 从 smali 接入第三方 sdk
- android 气泡.9,Android 三种方式实现三角形气泡效果、自定义View、shape、点9图
- 应用spss可靠性分析软件
- 电脑双屏变单屏后,界面显示问题
- 搭建高可用的nacos集群
- 【搜索与回溯算法】保卫农场(DFS)
热门文章
- GitHub标星7700:Python从新手到大师,只要100天
- 那个20多万“不可描述”照片的数据集,有人用它做了鉴黄模型 | Demo
- AI又输了!中国传奇Dota2冠军联手,OpenAI快速进化然并卵
- 高性能 socket 框架
- Linu的sftp环境搭建
- unity, destroy gameObject destroy all children
- Spring MVC 接收json自动转换JSONObject
- openssl搭建私有CA
- File类与FileInfo类
- WORD中插入的公式与文字对不齐——公式比文字高——文字比公式低