二叉树简单归纳(前序,中序,后序,层序遍历,叶子数目,树深度 )
2017-02-07 16:20
766 查看
Problem Description
已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点)。请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。
Input
输入一个长度小于50个字符的字符串。
Output
输出共有7行:
第1行输出前序遍历序列;
第2行输出中序遍历序列;
第3行输出后序遍历序列;
第4行输出层序遍历序列;
第5行输出叶子节点个数;
第6行输出叶子节点(从上到下,从左到右);
第7行输出二叉树深度。
Example Input
abc,,de,g,,f,,,
Example Output
abcdegf
cbegdfa
cgefdba
abcdefg
3
cfg
5
建树
前序遍历
中序遍历
后序遍历
层序遍历
叶子个数
叶子节点(从上到下,从左到右)
由层序遍历修改而得
二叉树深度
最终代码:
已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点)。请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。
Input
输入一个长度小于50个字符的字符串。
Output
输出共有7行:
第1行输出前序遍历序列;
第2行输出中序遍历序列;
第3行输出后序遍历序列;
第4行输出层序遍历序列;
第5行输出叶子节点个数;
第6行输出叶子节点(从上到下,从左到右);
第7行输出二叉树深度。
Example Input
abc,,de,g,,f,,,
Example Output
abcdegf
cbegdfa
cgefdba
abcdefg
3
cfg
5
建树
struct node *creat(struct node *t) { char c; c = str[i ++]; if (c == ',') t = NULL; else { t = (struct node *)malloc(sizeof(struct node)); t -> data = c; t -> l = creat(t -> l); t -> r = creat(t -> r); } return t; }
前序遍历
void qianxu(struct node *t) { if (t != NULL) { printf("%c", t -> data); qianxu(t -> l); qianxu(t -> r); } }
中序遍历
void zhonxu(struct node *t) { if (t != NULL) { zhonxu(t -> l); printf("%c",t -> data); zhonxu(t -> r); } }
后序遍历
void houxu(struct node *t) { if (t != NULL) { houxu(t -> l); houxu(t -> r); printf("%c",t -> data); } }
层序遍历
void cengxu(struct node *t) { int in = 0, out = 0; struct node *a[1050]; a[in ++] = t; while(in > out) { if (a[out] != NULL) { printf("%c",a[out] -> data); a[in ++] = a[out] -> l; a[in ++] = a[out] -> r; } out ++; } }
叶子个数
void num(struct node *t) { if (t != NULL) { if (t -> l == NULL && t -> r == NULL) count ++; else { num(t -> l); num(t -> r); } } }
叶子节点(从上到下,从左到右)
由层序遍历修改而得
void cengxunum(struct node *t) { int in = 0; int out = 0; struct node *p[1050]; p[in ++] = t; while(in > out) { if (p[out] != NULL) { if (p[out] -> l == NULL &&p[out] -> r == NULL) { printf("%c",p[out] -> data); } else { p[in ++] = p[out] -> l; p[in ++] = p[out] -> r; } } out ++; } }
二叉树深度
int depth(struct node *t) { int d1, d2; if (t != NULL) { d1 = depth(t -> l); d2 = depth(t -> r); if (d1 > d2) return d1 + 1; else return d2 + 1; } return 0; }
最终代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
struct node
{
char data;
struct node *l, *r;
}tree;
struct node *creat(struct node *t);
void qianxu(struct node *t);
v
4000
oid zhonxu(struct node *t);
void houxu(struct node *t);
void cengxu(struct node *t);
void num(struct node *t);
void cengxunum(struct node *t);
int depth(struct node *t);
char str[1050];
int i;
int count = 0;
int main()
{
while(scanf("%s",str)!=EOF)
{
i = 0;
struct node *tree = NULL;
tree = creat(tree);
qianxu(tree);
printf("\n");
zhonxu(tree);
printf("\n");
houxu(tree);
printf("\n");
cengxu(tree);
printf("\n");
num(tree);
printf("%d\n",count);
cengxunum(tree);
printf("\n");
int de;
de = depth(tree);
printf("%d\n",de);
}
return 0;
}
struct node *creat(struct node *t) { char c; c = str[i ++]; if (c == ',') t = NULL; else { t = (struct node *)malloc(sizeof(struct node)); t -> data = c; t -> l = creat(t -> l); t -> r = creat(t -> r); } return t; }
void zhonxu(struct node *t)
{
if (t != NULL)
{
zhonxu(t -> l);
printf("%c",t -> data);
zhonxu(t -> r);
}
}
void houxu(struct node *t)
{
if (t != NULL)
{
houxu(t -> l);
houxu(t -> r);
printf("%c",t -> data);
}
}
void qianxu(struct node *t) { if (t != NULL) { printf("%c", t -> data); qianxu(t -> l); qianxu(t -> r); } }
void cengxu(struct node *t) { int in = 0, out = 0; struct node *a[1050]; a[in ++] = t; while(in > out) { if (a[out] != NULL) { printf("%c",a[out] -> data); a[in ++] = a[out] -> l; a[in ++] = a[out] -> r; } out ++; } }
void num(struct node *t) { if (t != NULL) { if (t -> l == NULL && t -> r == NULL) count ++; else { num(t -> l); num(t -> r); } } }
void cengxunum(struct node *t) { int in = 0; int out = 0; struct node *p[1050]; p[in ++] = t; while(in > out) { if (p[out] != NULL) { if (p[out] -> l == NULL &&p[out] -> r == NULL) { printf("%c",p[out] -> data); } else { p[in ++] = p[out] -> l; p[in ++] = p[out] -> r; } } out ++; } }
int depth(struct node *t) { int d1, d2; if (t != NULL) { d1 = depth(t -> l); d2 = depth(t -> r); if (d1 > d2) return d1 + 1; else return d2 + 1; } return 0; }
相关文章推荐
- 二叉树的遍历方法之层序-先序-中序-后序遍历的简单讲解和代码示例
- 二叉树的遍历方法之层序-先序-中序-后序遍历的简单讲解和代码示例
- 二叉树的中序、先序、后序、层序遍历 & 二叉树的深度 & 节点查找
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- c语言实现二叉树先序,中序,后序(递归),层次遍历,求叶子节点个数及树的深度,下一篇写非递归的遍历
- 非递归遍历二叉树的四种策略-先序、中序、后序和层序
- 二叉树的创建,先序、中序、后序遍历的递归实现以及层序遍历
- 由二叉树的先序和中序遍历生成二叉树的层序和后序遍历结构
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的深度优先dfs遍历(前序、中序和后序;递归与非递归)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 非递归遍历二叉树的四种策略-先序、中序、后序和层序
- 我强大的二叉树(先序、层序创建,先序、中序、后序、层序遍历)
- 用非递归实现二叉树的前序、中序、后序、层次遍历,用递归实现查找、统计个数、比较、求深度
- 二叉树先序、中序、后序、层序遍历(递归、非递归)
- 遍历二叉树(前序、中序、后序、层序)
- 非递归遍历二叉树(先序、中序、后序、层序)
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现