数据结构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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using namespace std;


typedef struct btreenode
{
char data;
struct btreenode *lchild;
struct btreenode *rchild;
}BTreeNode, *BTree;

typedef struct
{
BTree link;
int flag;
}Stack;


void InitBTree(BTree &BT)
{
BT = NULL;
}


void CreatBTree(BTree &BT)
{
char ch;
ch = getchar();
if (ch == ' ')
BT = NULL;
else
{
BT = new BTreeNode;
BT->data = ch;
CreatBTree(BT->lchild);
CreatBTree(BT->rchild);
}
}
void PreOrder(BTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}
else
{
cout << BT->data;
if (BT->lchild != NULL)
PreOrder(BT->lchild);
if (BT->rchild != NULL)
PreOrder(BT->rchild);
}
}


void InOrder(BTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}



if (BT->lchild != NULL)
InOrder(BT->lchild);


cout << BT->data;


if (BT->rchild != NULL)
InOrder(BT->rchild);

}


void PostOrder(BTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}


if (BT->lchild != NULL)
PostOrder(BT->lchild);


if (BT->rchild != NULL)
PostOrder(BT->rchild);


cout << BT->data;

}


void LevelOrder(BTree BT)
{
BTree Queue[30];
int front, rear;
if (BT == NULL) return;

front = -1;
rear = 0;
Queue[rear] = BT;


while (front != rear)
{
front++;
cout << Queue[front]->data;
if (Queue[front]->lchild != NULL)
{
rear++;
Queue[rear] = Queue[front]->lchild;
}


if (Queue[front]->rchild != NULL)
{
rear++;
Queue[rear] = Queue[front]->rchild;
}
}
}


void PreOrder_2(BTree BT)
{
BTree stack[30], p;
int top;
if (BT == NULL) return;
top = -1;
p = BT;
while (!(p == NULL && top == -1))
{
while (p != NULL)
{
cout << p->data;
if (top < 30)
{
stack[top + 1] = p;
top++;
}
else
{
cout << "Stack is full!" << endl;
return;
}
p = p->lchild;
}

if (top < 0) return;
else
{
top--;
p = stack[top + 1];
p = p->rchild;
}
}
}


void PostOrder_2(BTree BT)
{
Stack s[30];
BTree p;
int top, sign;
if (BT == NULL) return;
top = -1;
p = BT;
while (!(p == NULL && top == -1))
{
if (p != NULL)
{
s[++top].link = p;
s[top].flag = 1;
p = p->lchild;
}
else
{
p = s[top].link;
sign = s[top].flag;
top--;
if (sign == 1)
{
s[++top].link = p;
s[top].flag = 2;
p = p->rchild;
}
else
{
cout << p->data;
p = NULL;
}
}
}
}


void main()
{
BTree T;
InitBTree(T);
cout << "InitTree completed!" << endl;
cout << "Create Tree:" << endl;
CreatBTree(T);
cout << "/nPreOrder:" << endl;
PreOrder(T);
cout << "/nInOrder:" << endl;
InOrder(T);
cout << "/nPosetOrder:" << endl;
PostOrder(T);
cout << "/nLevelOrder:" << endl;
LevelOrder(T);
cout << "/nPreOrder_2:" << endl;
PreOrder_2(T);
cout << "/nPostOrder_2:" << endl;
PostOrder_2(T);
system("pause");
}
作者

Wu Rang

发布于

2010-05-21

更新于

2024-06-13

许可协议

评论