-
Notifications
You must be signed in to change notification settings - Fork 0
/
二叉树的建.c
78 lines (72 loc) · 1.38 KB
/
二叉树的建.c
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
71
72
73
74
75
76
77
78
/*************************************************************************
> File Name: 二叉树的建.c
> Author:
> Mail:
> Created Time: 2015年11月04日 星期三 14时52分29秒
************************************************************************/
#include<stdio.h>
#include "string.h"
#include "malloc.h"
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
printf("Error!");
T->data=ch;
T->lchild=Create(T->lchild);
T->rchild=Create(T->rchild);
}
return T;
}
void Preorder(BiTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void zhongxu(BiTree T)
{
if(T)
{
zhongxu(T->lchild);
printf("%c",T->data);
zhongxu(T->rchild);
}
}
void houxu(BiTree T)
{
if(T)
{
houxu(T->lchild);
houxu(T->rchild);
printf("%c",T->data);
}
}
void main()
{
BiTree T;
int sum,dep;
printf("please input data: ");
T=Create(T);
printf("xianxu: ");
Preorder(T);
printf("\n");
printf("zhongxu: ");
zhongxu(T);
printf("\n");
printf("houxu: ");
houxu(T);
printf("\n");
}