二叉树的创建与三种遍历(先序,中序,后序)
来源:互联网 发布:2017程序员猝死 编辑:程序博客网 时间:2024/06/06 03:50
[问题描述] 建立一棵二叉树,并对其进行遍历(先序、中序、后序),打印输出遍历结果。
[基本要求] 从键盘接受输入(先序),以二叉链表作为存储结构,建立二叉树(以先序来建立),并采用递归算法对其进行遍历(先序、中序、后序),将遍历结果打印输出。
[测试数据] 1 2 3 0 0 4 5 0 7 0 0 6 0 0 0
1
2
3
4
5
建立二叉树:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
先序遍历:
1
2
3
4
5
6
7
8
9
中序遍历:
1
2
3
4
5
6
7
8
9
10
后序遍历:
1
2
3
4
5
6
7
8
9
10
总代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[基本要求] 从键盘接受输入(先序),以二叉链表作为存储结构,建立二叉树(以先序来建立),并采用递归算法对其进行遍历(先序、中序、后序),将遍历结果打印输出。
[测试数据] 1 2 3 0 0 4 5 0 7 0 0 6 0 0 0
则输出结果为 先序:1 2 3 4 5 7 6 中序:3 2 5 7 4 6 1 后序:3 7 5 6 4 2 1
二叉树节点的建立:
C++ Code
2
3
4
5
typedef struct BiTNode
{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode ,*BiTree;
{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode ,*BiTree;
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BiTree creatbitree()
{
BiTree T;
int p;
scanf("%d",&p);
if(p==0)
T=NULL;
else
{
T=(BiTNode * )malloc(sizeof(BiTNode));
T->data=p;
T->lchild=creatbitree();
T->rchild=creatbitree();
}
return T;
}
{
BiTree T;
int p;
scanf("%d",&p);
if(p==0)
T=NULL;
else
{
T=(BiTNode * )malloc(sizeof(BiTNode));
T->data=p;
T->lchild=creatbitree();
T->rchild=creatbitree();
}
return T;
}
C++ Code
2
3
4
5
6
7
8
9
void preOrder(BiTree T)
{
if(T!=NULL)
{
printf("%d",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
{
if(T!=NULL)
{
printf("%d",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
C++ Code
2
3
4
5
6
7
8
9
10
void inOrder(BiTree T)
{
if(T!=NULL)
{
inOrder(T->lchild);
printf("%d",T->data);
inOrder(T->rchild);
}
}
{
if(T!=NULL)
{
inOrder(T->lchild);
printf("%d",T->data);
inOrder(T->rchild);
}
}
C++ Code
2
3
4
5
6
7
8
9
10
void postOrder(BiTree T)
{
if(T!=NULL)
{
postOrder(T->lchild);
postOrder(T->rchild);
printf("%d",T->data);
}
}
{
if(T!=NULL)
{
postOrder(T->lchild);
postOrder(T->rchild);
printf("%d",T->data);
}
}
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;
typedef struct BiTNode
{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode ,*BiTree;
BiTree creatbitree()
{
BiTree T;
int p;
scanf("%d",&p);
if(p==0)
T=NULL;
else
{
T=(BiTNode * )malloc(sizeof(BiTNode));
T->data=p;
T->lchild=creatbitree();
T->rchild=creatbitree();
}
return T;
}
void preOrder(BiTree T)
{
if(T!=NULL)
{
printf("%d ",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
void inOrder(BiTree T)
{
if(T!=NULL)
{
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
}
void postOrder(BiTree T)
{
if(T!=NULL)
{
postOrder(T->lchild);
postOrder(T->rchild);
printf("%d ",T->data);
}
}
int main()
{
BiTree ta;
ta=creatbitree();
printf("xian xu bian li\n");
preOrder(ta);
printf("\n");
printf("zhong xu bian li\n");
inOrder(ta);
printf("\n");
printf("hou xu bian li \n");
postOrder(ta);
return 0;
}
using namespace std;
typedef struct BiTNode
{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode ,*BiTree;
BiTree creatbitree()
{
BiTree T;
int p;
scanf("%d",&p);
if(p==0)
T=NULL;
else
{
T=(BiTNode * )malloc(sizeof(BiTNode));
T->data=p;
T->lchild=creatbitree();
T->rchild=creatbitree();
}
return T;
}
void preOrder(BiTree T)
{
if(T!=NULL)
{
printf("%d ",T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
void inOrder(BiTree T)
{
if(T!=NULL)
{
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
}
void postOrder(BiTree T)
{
if(T!=NULL)
{
postOrder(T->lchild);
postOrder(T->rchild);
printf("%d ",T->data);
}
}
int main()
{
BiTree ta;
ta=creatbitree();
printf("xian xu bian li\n");
preOrder(ta);
printf("\n");
printf("zhong xu bian li\n");
inOrder(ta);
printf("\n");
printf("hou xu bian li \n");
postOrder(ta);
return 0;
}
阅读全文
0 0
- 二叉树的创建与三种遍历(先序,中序,后序)
- 二叉树递归的创建及三种遍历(先序、中序、后序)
- 二叉树的操作(二叉树的创建、先序遍历--->先根、中序遍历---->先左、后续遍历--->后根)
- 二叉树的创建与先、中、后序遍历递归实现
- 二叉树的创建,先序遍历,中序遍历,后序遍历
- 创建一个二叉树(先序遍历、中序遍历、后序遍历)
- 用Java语言实现二叉树的三种遍历(先序、中序、后序遍历)
- 二叉树(先序创建,递归先序,中序,后序遍历)-----1
- 有关二叉树的三种遍历方式------先序,中序,后序
- 二叉树的递归创建,先序(中序、后序)递归遍历二叉树
- 我强大的二叉树(先序、层序创建,先序、中序、后序、层序遍历)
- 先序、中序、后序遍历(三种情况访问二叉树)
- 二叉树的遍历(1)--先序遍历,中序遍历,后序遍历
- 二叉树的遍历代码(先序遍历,中序遍历,后序遍历)
- 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度
- 二叉树的遍历(先序/中序/后序,递归/迭代)与搜索
- Java二叉树(三)--非递归的中序_先序_后序遍历二叉树
- 先序遍历/后序遍历创建线索二叉树
- Android下网络通信机制
- 关于软硬结合时的思考
- 毛发及眼球的渲染技术
- 7:---(3)CSS3中的变形--缩放 scale()
- static静态变量
- 二叉树的创建与三种遍历(先序,中序,后序)
- Androidの矢量图形之VectorDrawable研究
- 数据库概述(发展历程,关系数据库/非关系数据库/SQL)
- PEMReader最新的使用方法
- linux 下安装jdk8和Jetty9.4.6
- svn: Aborting commit: 'E:\myeclipse\workplaces\……“ remains in conflict错误的解决方法
- 解决 Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)
- android 实现淘宝授权功能
- apache性能测试工具ab使用详解