-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTree.cpp
More file actions
268 lines (249 loc) · 7.07 KB
/
BSTree.cpp
File metadata and controls
268 lines (249 loc) · 7.07 KB
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
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
// Binary search tree structure
struct BSTree {
string data;
BSTree *left, *right;
BSTree(string number) {
this->data = number;
this->left = NULL;
this->right = NULL;
}
};
struct Trunk {
Trunk *prev;
string str;
Trunk(Trunk *prev, string str) {
this->prev = prev;
this->str = str;
}
};
void showTrunks(Trunk *p) {
if (p == NULL)
return;
showTrunks(p->prev);
cout << p->str;
}
/**
* The function takes a pointer to a tree node, a pointer to a Trunk object, and a boolean value. It
* prints the tree in a horizontal fashion
*
* @param root The root of the tree
* @param prev the previous trunk
* @param isLeft true if the node is a left child of its parent
*/
void horizontal_printer(BSTree *root, Trunk *prev = NULL, bool isLeft = false) {
if (root == NULL)
return;
string prev_str = " ";
Trunk *trunk = new Trunk(prev, prev_str);
horizontal_printer(root->right, trunk, true);
if (!prev)
trunk->str = "----";
else if (isLeft) {
trunk->str = ".----";
prev_str = " |";
}
else {
trunk->str = "\'----";
prev->str = prev_str;
}
showTrunks(trunk);
cout << " " << root->data << endl;
if (prev)
prev->str = prev_str;
trunk->str = " |";
horizontal_printer(root->left, trunk, false);
}
/**
* It reads in a line of input, and returns a queue of BSTree nodes
*
* @return A queue of BSTree pointers.
*/
queue<BSTree*> getNodes() {
queue<BSTree*> res;
string node;
while (cin >> node) {
BSTree *root = new BSTree(node);
res.push(root);
if (cin.peek() == '\n') break;
}
cin.ignore(1);
return res;
}
/**
* The function takes in the inorder and postorder/preorder traversals of a binary tree, and returns the root of
* the constructed tree
* This is the core part and the main algorithm used to construct the tree
* by giving it the inorder traversal and either postorder or preorder traversal of the tree
*
* @param in_order the in-order traversal of the tree
* @param post_pre the post-order/pre-order traversal of the tree
* @param root the root of the tree
*
* @return The root of the constructed tree.
*/
BSTree* build(queue<BSTree*> &in_order, queue<BSTree*> &post_pre, BSTree *&root) {
if (in_order.size() == 0)
return NULL;
if (in_order.size() == 1)
return root;
if (in_order.size() == 2) {
if (in_order.back()->data == root->data)
root->left = in_order.front();
else
root->right = in_order.back();
return root;
}
if (in_order.size() == 3) {
root->left = in_order.front();
root->right = in_order.back();
return root;
}
int sign;
if (post_pre.front()->data == root->data) {
post_pre.pop();
sign = 2;
}
else sign = 1;
queue<BSTree*> left_in;
while (in_order.front()->data != root->data) {
left_in.push(in_order.front());
in_order.pop();
}
in_order.pop();
queue<BSTree*> right_in;
while (!in_order.empty()) {
right_in.push(in_order.front());
in_order.pop();
}
queue<BSTree*> left_p;
for (int i = 0; i < left_in.size(); i++) {
left_p.push(post_pre.front());
post_pre.pop();
}
queue<BSTree*> right_p;
for (int i = 0; i < right_in.size(); i++) {
right_p.push(post_pre.front());
post_pre.pop();
}
if (sign == 1) {
root->left = build(left_in, left_p, left_p.back());
root->right = build(right_in, right_p, right_p.back());
}
else {
root->left = build(left_in, left_p, left_p.front());
root->right = build(right_in, right_p, right_p.front());
}
return root;
}
BSTree* buildTree(queue<BSTree*> &in, queue<BSTree*> &post_pre, int num) {
if (num == 1)
return build(in, post_pre, post_pre.back());
else if (num == 2)
return build(in, post_pre, post_pre.front());
else cout << "something went wrong!" << endl;
return NULL;
}
int height(BSTree *root) {
if (root == NULL)
return 0;
else {
int left_height = height(root->left);
int right_height = height(root->right);
if (left_height >= right_height)
return left_height + 1;
else
return right_height + 1;
}
}
/**
* It takes a tree, a level, and a queue, then adds the data of the nodes at that level to the queue
*
* @param root the root of the tree
* @param level the level of the tree you're currently on
* @param q queue of strings
*
* @return the width of the tree.
*/
void width(BSTree *root, int level, queue<string> &q){
if (root == NULL) {
if (level>1){
width(NULL, level - 1, q);
width(NULL, level - 1, q);
}
else q.push("null");
return;
}
if (level == 1)
q.push(root->data);
else if (level > 1) {
width(root->left, level - 1, q);
width(root->right, level - 1, q);
}
}
/**
* The function takes a binary search tree and prints it out in a vertical fashion
*
* @param root the root of the tree
* @param parse the string that is printed between each node
*/
void vertical_printer(BSTree *root, string parse = " ") {
int nodes = pow(2, height(root)) - 1;
string *space = new string[nodes];
for (int i = 0; i < nodes; i++)
space[i] = "#";
for (int l = 1; l <= height(root); l++) {
queue<string> q;
width(root, l, q);
int jumps = pow(2, height(root) - l);
int helper = jumps;
for (int i = 1; helper <= nodes; i++) {
if (i != helper)
cout << parse;
else if (i == helper && space[i-1] != "#") {
cout << parse;
helper += jumps;
}
else if (i == helper && space[i-1] == "#" && q.front() == "null") {
space[i-1] = q.front();
q.pop();
helper += jumps;
cout << parse;
}
else {
space[i-1] = q.front();
q.pop();
helper += jumps;
cout << space[i-1];
}
}
for (int i = 1; i <= jumps/2; i++)
cout << endl;
}
delete[] space;
}
int main() {
cout << "Enter in-order traversal of the tree with space in between(x x x ...)" << endl;
queue<BSTree*> in = getNodes();
cout << "Which one do you want to enter now: (1 or 2)" << endl
<< "1. post" << endl
<< "2. pre" << endl
<< "Your choice: ";
int num;
cin >> num; cin.ignore(1);
cout << "Enter the traversal" << endl;
queue<BSTree*> post_pre = getNodes();
BSTree *root = buildTree(in, post_pre, num);
cout << endl;
horizontal_printer(root);
cout << endl;
for (int i = 0; i < 50; i++) cout << "-";
cout << endl;
vertical_printer(root);
cout << "\n\nPress enter to exit...";
cin.get();
return 0;
}