1
#include
<
stdio.h
>
2
#include
<
malloc.h
>
3
4
typedef
struct
node
{
5
int
data;
6
struct
node
*
lchild,
*
rchild;
7
}
*
treetp,tree;
8
treetp create (treetp t,
int
c);
9
void
print1(treetp);
10
void
print2(treetp);
11
void
print3(treetp);
12
int
number
=
0
;
13
void
main()
14
{
15
treetp t
=
0
,r;
16
r
=
create (t,
0
);
17
printf(
"
前序排列 :
"
);
18
print1 (r);
19
printf(
"
\n中序排列 :
"
);
20
print2 (r);
21
printf(
"
\n后序排列 :
"
);
22
print3 (r);
23
}
24
25
treetp create(treetp t,
int
c)
26
{
27
treetp p,di;
28
do
{
29
scanf(
"
%d
"
,
&
c);
30
if
(t
==
0
)
31
{
32
t
=
(treetp)malloc(
sizeof
(tree));
33
t
->
lchild
=
t
->
rchild
=
0
;
34
t
->
data
=
c;
35
}
36
else
37
{ p
=
t;
38
while
(p
!=
0
)
39
{
40
di
=
p;
41
if
(c
<
(p
->
data))
42
p
=
p
->
lchild;
43
else
44
p
=
p
->
rchild;
45
}
46
if
(c
<
(di
->
data))
47
{
48
treetp NEWdi
=
(treetp) malloc(
sizeof
(tree));
49
NEWdi
->
lchild
=
NEWdi
->
rchild
=
0
;
50
NEWdi
->
data
=
c;
51
di
->
lchild
=
NEWdi;
52
}
53
else
54
{
55
treetp NEWdi
=
(treetp) malloc(
sizeof
(tree));
56
NEWdi
->
lchild
=
NEWdi
->
rchild
=
0
;
57
NEWdi
->
data
=
c;
58
di
->
rchild
=
NEWdi;
59
}
60
}
61
++
number;
62
}
while
(c
!=
0
);
63
printf(
"
叶子的数量:%d
"
,number);
64
return
t;
65
}
66
void
print1(treetp t)
67
{
68
if
(t
!=
0
)
69
{
70
printf(
"
%d
"
,t
->
data);
71
print1(t
->
lchild);
72
print1(t
->
rchild);
73
}
74
}
75
void
print2(treetp t)
76
{
77
if
(t
!=
0
)
78
{
79
print2(t
->
lchild);
80
printf(
"
%d
"
,t
->
data);
81
print2(t
->
rchild);
82
}
83
}
84
void
print3(treetp t)
85
{
86
if
(t
!=
0
)
87
{
88
print3(t
->
lchild);
89
print3(t
->
rchild);
90
printf(
"
%d
"
,t
->
data);
91
}
92
}
93
地震让大伙知道:居安思危,才是生存之道。