-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree.cpp
330 lines (299 loc) · 8.79 KB
/
binary_tree.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* binary_tree.cpp --
*
* binary_tree data structures and misc routines
*
* 2010/01/12: initial release by Todd Xue
*/
#include "binary_tree.h"
#include "draw.h"
/*
*-------------------------------------------------------------------------
*
* Binary_Tree::print --
* print binary_tree, 2 questions to resolve
* 1. binary_tree height
* 2. binary_tree width
* after that, we can assign each node a coordinate (x,y)
* y is the node's height
* x is the node's right pos on same level
*
* here
* y represented by level
* x represented by level_no
* add these as member of Binary_Tree
*
* draw is done through Draw class
*
* So the hardness is how to calculate the level_no:
* -- simply done throught the level_no_curr[MAX_LEVEL] array
*
*-------------------------------------------------------------------------
*/
void binary_tree_calculate_level(Binary_Tree* t, int curr_level) {
t->level = curr_level++;
if (t->left)
binary_tree_calculate_level(t->left, curr_level);
if (t->right)
binary_tree_calculate_level(t->right, curr_level);
}
void binary_tree_calculate_level_no(Binary_Tree* t, int& level_max) {
if (t->level > level_max)
level_max = t->level;
if (t->left) {
t->left->level_no = t->level_no * 2;
binary_tree_calculate_level_no(t->left, level_max);
}
if (t->right) {
t->right->level_no = t->level_no * 2 + 1;
binary_tree_calculate_level_no(t->right, level_max);
}
}
void binary_tree_calculate_x(Binary_Tree* t) {
int level_nodes = 1 << t->level;
double unit = Draw::eX_MAX * 1.0/(level_nodes+1);
t->x = int(round(unit * (1+t->level_no))); // [unit, ..., level_nodes*unit]
if (t->left)
binary_tree_calculate_x(t->left);
if (t->right)
binary_tree_calculate_x(t->right);
}
void draw_binary_tree(Binary_Tree* t, Draw& d, double yunit) {
Binary_Tree* s;
if ((s = t->left)) {
d.move_to(t->x, int(round(yunit*(1+t->level))));
d.line_to(s->x, int(round(yunit*(1+s->level))));
}
if ((s = t->right)) {
d.move_to(t->x, int(round(yunit*(1+t->level))));
d.line_to(s->x, int(round(yunit*(1+s->level))));
}
if ((s = t->left))
draw_binary_tree(s, d, yunit);
if ((s = t->right))
draw_binary_tree(s, d, yunit);
d.out(t->x, int(round(yunit*(1+t->level))), t->no);
}
void Binary_Tree::print() {
binary_tree_calculate_level(this, 0);
// calculate level_no 1st
int level_max = 0;
this->level_no = 0;
binary_tree_calculate_level_no(this, level_max);
// calculate the x by the level_no
binary_tree_calculate_x(this);
// now it's ready to print out
Draw d;
double yunit = Draw::eY_MAX * 1.0 / (level_max+2);
draw_binary_tree(this, d, yunit);
d.print();
}
/*
*-------------------------------------------------------------------------
*
* next_binary_tree --
* binary_tree generation algorithm, this is an interesting algorithm,
* even if it's not fast, there are a lot of papers decribing
* binary_tree generation aglorithm in the last 40 years
*
* RETURN:
* true - re adjust binary_tree to next one
* false - it's already the last binary_tree
*
* PRE CONDTION:
* t != 0
*
*-------------------------------------------------------------------------
*/
void reset_binary_tree(Binary_Tree* t);
bool next_binary_tree(Binary_Tree* t) {
if (t->left) {
if (next_binary_tree(t->left))
return true;
if (t->right) {
if (next_binary_tree(t->right)) {
if (t->left)
reset_binary_tree(t->left);
return true;
}
else {
reset_binary_tree(t->left);
Binary_Tree* extract = t->left;
t->left = extract->left;
reset_binary_tree(t->right);
extract->left = t->right;
t->right = extract;
return true;
}
}
else {
reset_binary_tree(t->left);
Binary_Tree* extract = t->left;
t->left = extract->left;
extract->left = t->right;
t->right = extract;
return true;
}
}
else {
if (!t->right)
return false;
else
return next_binary_tree(t->right);
}
}
/*
* reset t to a single path, actually it's DFS sequence path
*/
void reset_binary_tree(Binary_Tree* t) {
if (t->left) {
reset_binary_tree(t->left);
if (t->right) {
reset_binary_tree(t->right);
Binary_Tree* s = t->left;
while (s->left)
s = s->left;
s->left = t->right;
t->right = 0;
}
}
else {
if (t->right) {
reset_binary_tree(t->right);
t->left = t->right;
t->right = 0;
}
}
}
/*
*-------------------------------------------------------------------------
*
* binary_tree_preorder --
* preorder tranverse
*
* PRE CONDTION:
* suppose n = number of vertices of binary_tree t
* o must at least contain n integers
*
* NOTE:
* the codes becomes obvious when we use the path concept
* every path of binary_tree is visited from leftest path to rigthtest
*
*-------------------------------------------------------------------------
*/
void binary_tree_preorder(Binary_Tree* t, int* o) {
int o_fill = 0;
Binary_Tree* nodes[1024];
int c = 0;
goto inner;
while (c > 0) {
t = nodes[--c]->right;
inner:
for (; t; t = t->left) {
o[o_fill++] = t->no;
if (t->right)
nodes[c++] = t;
}
}
}
void binary_tree_preorder_recursive_internal(Binary_Tree* t, int*& o) {
*o++ = t->no;
if (t->left)
binary_tree_preorder_recursive_internal(t->left, o);
if (t->right)
binary_tree_preorder_recursive_internal(t->right, o);
}
void binary_tree_preorder_recursive(Binary_Tree* t, int* o) {
binary_tree_preorder_recursive_internal(t, o);
}
/*
*-------------------------------------------------------------------------
*
* binary_tree_postorder --
* postorder tranverse
*
* POST CONDTION:
* suppose n = number of vertices of binary_tree t
* o must at least contain n integers
*
* NOTE:
* the codes becomes obvious when we use the path concept
* every path of binary_tree is visited from leftest path to rigthtest
*
* The interesting part of this compared to the non-binary-tree version,
* we see the benifits of son-sib representation
*
*-------------------------------------------------------------------------
*/
void binary_tree_postorder(Binary_Tree* t, int* o) {
int o_fill = 0;
Binary_Tree* nodes[1024];
int c = 0;
Binary_Tree* prev_pop = 0;
goto inner;
while (c > 0) {
t = nodes[c-1];
if (t->right == prev_pop || t->right == 0) {
o[o_fill++] = t->no;
--c;
prev_pop = t;
continue;
}
t = t->right;
inner:
for (; t; t = t->left)
nodes[c++] = t;
}
}
void binary_tree_postorder_recursive_internal(Binary_Tree* t, int*& o) {
Binary_Tree* saved = t;
if (t->left)
binary_tree_postorder_recursive_internal(t->left, o);
if (t->right)
binary_tree_postorder_recursive_internal(t->right, o);
*o++ = saved->no;
}
void binary_tree_postorder_recursive(Binary_Tree* t, int* o) {
binary_tree_postorder_recursive_internal(t, o);
}
/*
*-------------------------------------------------------------------------
*
* binary_tree_inorder --
* inorder tranverse
*
* IN CONDTION:
* suppose n = number of vertices of binary_tree t
* o must at least contain n integers
*
* NOTE:
* the codes becomes obvious when we use the path concept
* every path of binary_tree is visited from leftest path to rigthtest
*
*-------------------------------------------------------------------------
*/
void binary_tree_inorder(Binary_Tree* t, int* o) {
int o_fill = 0;
Binary_Tree* nodes[1024];
int c = 0;
goto inner;
while (c > 0) {
t = nodes[--c];
o[o_fill++] = t->no;
t = t->right;
inner:
for (; t; t = t->left)
nodes[c++] = t;
}
}
void binary_tree_inorder_recursive_internal(Binary_Tree* t, int*& o) {
Binary_Tree* saved = t;
if (t->left)
binary_tree_inorder_recursive_internal(t->left, o);
*o++ = saved->no;
if (t->right)
binary_tree_inorder_recursive_internal(t->right, o);
}
void binary_tree_inorder_recursive(Binary_Tree* t, int* o) {
binary_tree_inorder_recursive_internal(t, o);
}