-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.h
83 lines (72 loc) · 2.2 KB
/
display.h
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
#include<iostream>
using namespace std;
void getlevel(node * ptr, int t) {
if (t == 0) {
a.push_back(ptr);
}
else {
if (ptr != NULL) {
getlevel(ptr->leftnode, t - 1);
getlevel(ptr->rightnode, t - 1);
}
else {
getlevel(NULL, t - 1);
getlevel(NULL, t - 1);
}
}
}
void printarr() {
int k = 0, gl, ls, ms, t = 0;
int n = calheight(root), j;
cout << "in print tree\n";
for (int i = 0; i < calheight(root); i++) {
ls = pow(2, n - i) - 1;
ms = pow(2, n - i + 1) - 1;
gl = pow(2, max(0, n - i - 1));
for (j = 0; j < ls; j++)
cout << " ";
getlevel(root, i);
for (int y = t; y < a.size(); y++) {
if (a[y] == NULL)
cout << " ";
else
cout << a[y]->data;
for (j = 0; j < ms ; j++)
cout << " ";
}
for (j = 0; j<gl-1; j++) {
cout << "\n";
for (int p = 0; p < ls - j; p++)
cout << " ";
for (int l = t; l < a.size(); l++) {
if (a[l] != NULL) {
if (a[l]->leftnode != NULL)
cout << "/";
else
cout << " ";
for (int p = 0; p < (2 * j + 1); p++)
cout << " ";
if (a[l]->rightnode != NULL)
cout << "\\";
else
cout << " ";
for (int mss = 0; mss < (ms - 2 * j - 2); mss++) {
cout << " ";
}
}
else {
cout << " ";
for (int p = 0; p < (2 * j + 1); p++)
cout << " ";
cout << " ";
for (int mss = 0; mss < (ms - 2 * j - 2); mss++) {
cout << " ";
}
}
}
}
cout << "\n";
t = a.size();
}
a.erase(a.begin(),a.end());
}