头文件和定义

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<ctime>
#include<cassert>
#include<string>
using namespace std;

struct BitreeNode{
char data;
BitreeNode *lc,*rc;
};

typedef BitreeNode* Bitree;

创建树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//create
void BitreeCreate(Bitree &t,string define,int &i)
{
if(define[i]=='*'){
t=0;
i++;
return;
}
t=new BitreeNode;
t->data=define[i];
i++;
BitreeCreate(t->lc,define,i);
BitreeCreate(t->rc,define,i);
}

前序遍历

1
2
3
4
5
6
7
8
9
10
//pre
void BitreePreOrder(Bitree t)
{
if(t)
{
cout<<t->data;
BitreePreOrder(t->lc);
BitreePreOrder(t->rc);
}
}

中序遍历

1
2
3
4
5
6
7
8
9
10
//CenterOrder
void BitreeCenterOrderOrder(Bitree t)
{
if(t)
{
BitreePreOrder(t->lc);
cout<<t->data;
BitreePreOrder(t->rc);
}
}

后续遍历

1
2
3
4
5
6
7
8
9
10
//back
void BitreeBackOrder(Bitree t)
{
if(t)
{
cout<<t->data;
BitreeBackOrder(t->rc);
BitreeBackOrder(t->lc);
}
}

获取节点数

1
2
3
4
5
6
7
//get node number
int BitreeCount(Bitree t)
{
if(t)
return BitreeCount(t->lc)+BitreeCount(t->rc)+1;
return 0;
}

获取高度

1
2
3
4
5
6
7
8
9
//get height
int BitreeHeight(Bitree t)
{
if(!t)
return 0;
if(BitreeHeight(t->lc)>BitreeHeight(t->rc))
return BitreeHeight(t->lc)+1;
return BitreeHeight(t->rc)+1;
}

查找结点

1
2
3
4
5
6
7
8
9
10
11
12
//Search
Bitree BitreeSearch(Bitree t,char e)
{
if(t==0)
return 0;
if(t->data==e)
return t;
if(BitreeSearch(t->lc,e))
return BitreeSearch(t->lc,e);
return BitreeSearch(t->rc,e);
}

测试代码

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
//test
void BitreeTest()
{
Bitree t;
int i = 0;
BitreeCreate(t, "ab**c**", i);
cout << "PreOrder: ";
BitreePreOrder(t);
cout << "\n";
cout << "BackOrder: ";
BitreeBackOrder(t);
cout << "\n";
cout << "CenterOrder: ";
BitreeCenterOrderOrder(t);

cout << "\n";
cout << "Node number: ";
cout << BitreeCount(t) << "\n";

BitreeCount(t);
cout << "Height: ";
BitreeHeight(t);
cout << BitreeHeight(t) << "\n";
}

完整代码

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#include<ctime>
#include<cassert>
#include<string>
using namespace std;

struct BitreeNode{
char data;
BitreeNode *lc,*rc;
};

typedef BitreeNode* Bitree;

//create
void BitreeCreate(Bitree &t,string define,int &i)
{
if(define[i]=='*'){
t=0;
i++;
return;
}
t=new BitreeNode;
t->data=define[i];
i++;
BitreeCreate(t->lc,define,i);
BitreeCreate(t->rc,define,i);
}

//pre
void BitreePreOrder(Bitree t)
{
if(t)
{
cout<<t->data;
BitreePreOrder(t->lc);
BitreePreOrder(t->rc);
}
}

//CenterOrder
void BitreeCenterOrderOrder(Bitree t)
{
if(t)
{
BitreePreOrder(t->lc);
cout<<t->data;
BitreePreOrder(t->rc);
}
}

//back
void BitreeBackOrder(Bitree t)
{
if(t)
{
cout<<t->data;
BitreeBackOrder(t->rc);
BitreeBackOrder(t->lc);
}
}

//get node number
int BitreeCount(Bitree t)
{
if(t)
return BitreeCount(t->lc)+BitreeCount(t->rc)+1;
return 0;
}

//get height
int BitreeHeight(Bitree t)
{
if(!t)
return 0;
if(BitreeHeight(t->lc)>BitreeHeight(t->rc))
return BitreeHeight(t->lc)+1;
return BitreeHeight(t->rc)+1;
}

//Search
Bitree BitreeSearch(Bitree t,char e)
{
if(t==0)
return 0;
if(t->data==e)
return t;
if(BitreeSearch(t->lc,e))
return BitreeSearch(t->lc,e);
return BitreeSearch(t->rc,e);
}

//test
void BitreeTest()
{
Bitree t;
int i = 0;
BitreeCreate(t, "ab**c**", i);
cout << "PreOrder: ";
BitreePreOrder(t);
cout << "\n";
cout << "BackOrder: ";
BitreeBackOrder(t);
cout << "\n";
cout << "CenterOrder: ";
BitreeCenterOrderOrder(t);

cout << "\n";
cout << "Node number: ";
cout << BitreeCount(t) << "\n";

BitreeCount(t);
cout << "Height: ";
BitreeHeight(t);
cout << BitreeHeight(t) << "\n";
}

int main()
{
BitreeTest();
}