数据结构->二叉树的基本操作
来源:互联网 发布:练六级英语听力的软件 编辑:程序博客网 时间:2024/05/15 02:30
1、 按先序次序输入二叉树中结点的值(一个字符),`@`表示空树,生成二叉树的二叉链表存储结构。
2、 按先、中、后序递归遍历二叉树,之后结合栈的应用,将中序遍历算法改为非递归算法。
3、利用二叉树的递归算法求二叉树的高度 。
4、利用二叉树的递归算法求二叉树的叶子个数。
5、编写一个主函数,调试上述算法。
#include <stdio.h>#include <stdlib.h>typedef char ElemType;typedef struct Node{ ElemType data; struct Node *lchild,*rchild;} BTNode,*BiTree;int m=0,n=0;BiTree creat(){ BiTree T; ElemType data; scanf("%c",&data); if(data=='@') T=NULL ; else { T=(BiTree)malloc(sizeof(BTNode)); T->data=data; T->lchild=creat(); T->rchild=creat(); } return T;}void bianli1(BiTree T){ if(T) { printf("%c",T->data); bianli1(T->lchild); bianli1(T->rchild); }}void bianli2(BiTree T){ if(T) { bianli2(T->lchild); printf("%c",T->data); bianli2(T->rchild); }}void bianli3(BiTree T){ if(T) { bianli3(T->lchild); bianli3(T->rchild); printf("%c",T->data); }}void fdg(BiTree T){ BTNode tt[1000]; int top=-1; BTNode *p=T; BTNode *q; int i=0; while(p||top!=-1) { if(p) { tt[++top]=(*p); p=p->lchild; } else { q=&(tt[top--]); printf("%c",q->data); p=q->rchild; } }}int yezi(BiTree T){ if(T==NULL) return 0; if(T->lchild==NULL&&T->rchild==NULL) return 1; return (yezi(T->lchild)+yezi(T->rchild));}int height(BiTree T){ if(T==NULL) return 0; else { m=height(T->lchild); n=height(T->rchild); if(m>n) return m+1; else return n+1; }}int main(){ BiTree T=creat(); //creat(&T); puts("创建成功"); puts("\n先序遍历"); bianli1(T); puts("\n中序遍历"); bianli2(T); puts("\n后序遍历"); bianli3(T); puts("\n非递归中序遍历"); fdg(T); puts(""); printf("树的高度为%d\n",height(T)); printf("树的叶子数为%d\n",yezi(T)); return 0;}
阅读全文
0 0
- 数据结构之二叉树的基本操作
- 【数据结构】二叉树的基本操作
- (数据结构)二叉树的基本操作
- 【数据结构笔记】二叉树的基本操作
- 数据结构-二叉树的基本操作
- 数据结构->二叉树的基本操作
- 【数据结构】二叉树基本操作的程序实现
- 数据结构——二叉树的基本操作
- 数据结构.二叉树的基本操作(C语言实现)
- [数据结构] 二叉树的建立及其基本操作
- 南邮数据结构实验6.1二叉树的基本操作
- 数据结构:二叉树的基本操作(JAVA实现)
- 数据结构实验2——二叉树的基本操作
- 数据结构:二叉搜索树(BST)的基本操作
- 数据结构复习——二叉树的几个基本操作
- 【数据结构】二叉树的简单遍历及基本操作
- 数据结构 — 二叉树的基本操作(递归实现)
- 数据结构——二叉树的基本操作
- spring配置bean的细节二
- Tensorflow实例:实现GAN(生成对抗网)
- [区间DP] POJ 3186
- AES过程
- Python之旅
- 数据结构->二叉树的基本操作
- 斯坦福大学Tensorflow与深度学习实战课程
- 51nod 1172 Partial Sums V2 任意模数FFT
- 高级用法
- 程序员偷偷深爱的9个不良编程习惯
- python随机数
- SQL Server 返回结果集的几种方式
- 数据结构->图的运算
- 补充:webpack入门