-
Notifications
You must be signed in to change notification settings - Fork 1
/
建立树.cpp
58 lines (50 loc) · 833 Bytes
/
建立树.cpp
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
#include <iostream>
#include<cstdlib>
using namespace std;
typedef struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
BiTree CreateBiTree(){
char ch;
BiTree T;
cin>>ch;
if (ch == '#')
T = NULL;
else
{
T = new(BiTNode);
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;
}
void PreOrderTraverse(BiTree T){
if (T){
cout<<T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
void InOrderTraverse(BiTree T){
if (T){
InOrderTraverse(T->lchild);
cout<<T->data;
InOrderTraverse(T->rchild);
}
}
void PostOrderTraverse(BiTree T){
if (T){
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout<<T->data;
}
}
int main(){
BiTree T;
T = CreateBiTree();
InOrderTraverse(T);
system("PAUSE");
return 0;
}