-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsttree.cpp
More file actions
394 lines (338 loc) · 7.07 KB
/
Copy pathbsttree.cpp
File metadata and controls
394 lines (338 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
struct node {
int data;
struct node* l;
struct node* r;
};
typedef struct node Node;
class BST {
private:
Node* root;
vector<Node*> getPathTo(int value);
Node* insert(int data, Node* node);
void printTree(Node* node);
Node* find(Node* node, int data);
void printdfs(Node* node);
void printRevTree(Node* node) const;
void inorderSucc(const int data, Node* node);
const int minVal(Node* node) const;
void dfsPathUtil(Node* node, stack<Node*>& vPath, int value, bool& found);
public:
BST();
void dfsPath(int value);
void insert(int data);
Node * lca(int v1,int v2);
void printTree();
void printbfs();
void printlevelwise();
void printLeft();
void printzigzag();
bool isBST();
Node* find(int data) {
Node* node = find(root, data);
return node;
}
void printdfs() {
printdfs(root);
}
void printRevTree() {
printRevTree(root);
}
void inorderSucc(const int data) {
//1. find the node
Node* node = find(data);
if(!node) {
cout << "Node not found" << endl;
return;
}
inorderSucc(data, node);
}
};
void BST::dfsPathUtil(Node* node, stack<Node*>& vPath, int value, bool& found) {
if (!node || found)
return;
vPath.push(node);
if(node->data == value) {
found = true;
return;
}
dfsPathUtil(node->l, vPath, value, found);
dfsPathUtil(node->r, vPath, value, found);
if(!found) {
vPath.pop();
}
}
//function to print path to a node with some value in the tree
//assumes it is not a BST (just trying out DFS for tree)
void BST::dfsPath(int value) {
stack<Node*> vPath;
bool found = false;
dfsPathUtil(root, vPath, value, found);
if(found) {
int len = vPath.size();
cout << "Path to node" << endl;
for(int i = 0; i<len; i++) {
cout << vPath.top()->data << " ";
vPath.pop();
}
cout << endl;
}
}
vector<Node*> BST::getPathTo(int value) {
vector<Node*> path;
Node* current = root;
while(1) {
path.push_back(current);
if(current && current->data == value)
return path;
else if(current && value < current->data) {
current = current->l;
}
else if(current) {
current = current->r;
}
else {
cout << "node is NULL??" << endl;
return path;
}
}
}
Node* BST::lca(int v1,int v2)
{
vector<Node*> pathv1;
vector<Node*> pathv2;
pathv1 = getPathTo(v1);
pathv2 = getPathTo(v2);
int count = 0;
while(pathv1[count] == pathv2[count]) {
count++;
}
return pathv1[--count];
}
void BST::printzigzag() {
vector<Node*> vfor;
vector<Node*> vback;
bool isvfor = true;
vfor.push_back(root);
while(vfor.size() || vback.size()) {
if(isvfor) {
for(auto& elem:vfor) {
cout << elem->data << " ";
if(elem->l)
vback.push_back(elem->l);
if(elem->r)
vback.push_back(elem->r);
}
vfor.clear();
isvfor = false;
}
else {
int sz = vback.size() - 1;
for(int i=sz; i >= 0; i--) {
cout << vback.at(i)->data << " ";
Node* elem = vback.at(sz - i);
if(elem->l)
vfor.push_back(elem->l);
if(elem->r)
vfor.push_back(elem->r);
}
vback.clear();
isvfor = true;
}
cout << endl;
}
}
const int BST::minVal(Node* node) const {
int min = INT_MAX;
if(!node)
return min;
while(node) {
if(node->data < min)
min = node->data;
node = node->l;
}
return min;
}
void BST::inorderSucc(const int data, Node* node) {
if(node->r) {
cout << "Succ: " << minVal(node->r) << endl;
return;
}
Node* curr = root;
Node* succ = nullptr;
while(curr) {
if(curr->data > data) {
succ = curr;
curr = curr->l;
}
else if(curr->data < data) {
curr = curr->r;
}
else
break;
}
if(succ)
cout << "Succ: " << succ->data << endl;
else
cout << "No succ found" << endl;
}
void BST::printRevTree(Node* node) const {
if(!node)
return;
printRevTree(node->r);
cout << node->data << " ";
printRevTree(node->l);
}
void BST::printdfs(Node* node) {
if(!node)
return;
cout << node->data << endl;
printdfs(node->l);
printdfs(node->r);
}
Node* BST::find(Node* node, int data) {
Node* ret;
if (!node)
return nullptr;
if(data == node->data)
return node;
if(data > node->data)
ret = find(node->r, data);
else
ret = find(node->l, data);
return ret;
}
BST::BST() {
root = nullptr;
}
void BST::printLeft() {
if(!root)
return;
vector<vector<Node*>> vlevels;
int level=0;
vlevels.push_back(vector<Node*>());
vlevels[level].push_back(root);
while(1) {
int count = 0;
for(Node*& node: vlevels[level]) {
vlevels.push_back(vector<Node*>());
//print the left most node at this level
if(!node)
break;
if(!count) {
cout << node->data << endl;
}
if(node->l)
vlevels[level+1].push_back(node->l);
if(node->r)
vlevels[level+1].push_back(node->r);
count++;
}
if(!vlevels[level+1].size())
break;
vlevels[level].clear(); //previous level not required
level++;
}
}
void BST::printbfs() {
queue<Node*> q;
cout << "bfs start" << endl;
q.push(root);
while(!q.empty()) {
Node* temp = q.front();
if(temp != nullptr)
{
cout << temp->data << " ";
q.push(temp->l);
q.push(temp->r);
}
q.pop();
}
cout << "bfs done" << endl;
}
//prints inorder
void BST::printTree(Node* node) {
if(node == nullptr)
{
return;
}
printTree(node->l);
cout << node->data << endl;
printTree(node->r);
}
void BST::printTree() {
cout << "print inorder start" << endl;
printTree(root);
cout << "print inorder done" << endl;
}
Node* createNode(int data) {
Node* newnode = new Node;
newnode->data = data;
newnode->l = nullptr;
newnode->r = nullptr;
return newnode;
}
Node* BST::insert(int data, Node* node) {
if (node==nullptr) {
node = createNode(data);
return node;
}
if(data <= node->data)
node->l= insert(data, node->l);
else
node->r = insert(data, node->r);
return node;
}
void BST::insert(int data) {
root = insert(data, root);
}
void BST::printlevelwise() {
if (!root)
return;
queue<node*> level;
level.push(root);
node* temp;
while(!level.empty()) {
temp = level.front();
cout << temp->data << " ";
if(temp->l)
level.push(temp->l);
if(temp->r)
level.push(temp->r);
level.pop();
}
}
int main() {
BST tree;
tree.insert(6);
tree.insert(5);
tree.insert(7);
tree.insert(1);
tree.insert(2);
tree.insert(200);
tree.insert(205);
tree.insert(505);
tree.insert(705);
tree.printTree();
tree.printbfs();
tree.dfsPath(705);
//Node* node = tree.lca(200, 205);
//cout << "LCA is "<< node->data << endl;
//tree.printlevelwise();
//cout << "print lefts " << endl;
//tree.printLeft();
//Node* n1 = tree.find(200);
//Node* n2 = tree.find(7);
//cout << "n1 " << n1->data << " n2 " << n2->data << endl;
//tree.printdfs();
//tree.printRevTree();
//cout << endl;
//tree.inorderSucc(1);
//cout << "printing zigzag" << endl;
//tree.printzigzag();
}