-
Notifications
You must be signed in to change notification settings - Fork 0
/
trees.h
341 lines (295 loc) · 8.91 KB
/
trees.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
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
#ifndef __TREES_H__
#define __TREES_H__
#include <iostream>
#include <memory>
#include <vector>
#include <cmath>
#include <stack>
#include <memory>
using std::cout;
using std::endl;
using std::shared_ptr;
using std::vector;
using std::stack;
using std::make_shared;
/*******************************************************************************
* class BinaryTreeNode
*/
template <typename T>
struct BinaryTreeNode {
public:
T data;
shared_ptr<BinaryTreeNode<T>> parent;
shared_ptr<BinaryTreeNode<T>> left, right;
int size;
/*****************************************************************************
* BinaryTreeNode()
*/
BinaryTreeNode(T value) {
data = value;
size = 0;
}
/*****************************************************************************
* getters
*/
const T & getData() { return data; }
int getSize() { return size; }
shared_ptr<BinaryTreeNode<T>> getParent() { return parent; }
shared_ptr<BinaryTreeNode<T>> getLeft() { return left; }
shared_ptr<BinaryTreeNode<T>> getRight() { return right; }
/*****************************************************************************
* compare()
*/
int compare(shared_ptr<BinaryTreeNode<T>> that) {
iLog(0, "Compare %d and %d", this->getData(), that->getData());
int diff = this->getData() - that->getData();
if (diff != 0) {
return diff;
}
if (diff == 0) {
if (this->getLeft() != nullptr && that->getLeft() == nullptr) {
return -1;
} else if (this->getLeft() == nullptr && that->getLeft() != nullptr) {
return 1;
} else if( this->getLeft() != nullptr && that->getLeft() != nullptr) {
diff = this->getLeft()->compare(that->getLeft());
}
}
if (diff == 0) {
if (this->getRight() != nullptr && that->getRight() == nullptr) {
return -1;
} else if (this->getRight() == nullptr && that->getRight() != nullptr) {
return 1;
} else if( this->getRight() != nullptr && that->getRight() != nullptr) {
diff = this->getRight()->compare(that->getRight());
}
}
return diff;
}
/*****************************************************************************
* flatten()
*/
shared_ptr<BinaryTreeNode<T>> flatten(shared_ptr<BinaryTreeNode<T>> node) {
shared_ptr<BinaryTreeNode<T>> lroot = nullptr, rroot = nullptr;
if (node->left) {
lroot = flatten(node->left);
node->left = nullptr;
}
if (node->right) {
rroot = flatten(node->right);
node->right = nullptr;
}
if (lroot != nullptr) {
pushRight(lroot, node);
} else {
lroot = node;
}
if (rroot != nullptr) {
pushRight(lroot, rroot);
}
return lroot;
}
/*****************************************************************************
* pushRight()
*/
void pushRight(shared_ptr<BinaryTreeNode<T>> node,
shared_ptr<BinaryTreeNode<T>> right) {
if (node->right) {
pushRight(node->right, right);
} else {
node->right = right;
}
}
};
/*******************************************************************************
* class BinaryTree
*/
template <typename T>
class BinaryTree {
public:
shared_ptr<BinaryTreeNode<T>> root;
/*****************************************************************************
* BinaryTree()
*/
BinaryTree() : root(nullptr) {}
BinaryTree(vector<T> &v) {
root = buildBinaryTree(v);
}
/*****************************************************************************
* buildBinaryTree()
*/
shared_ptr<BinaryTreeNode<T>> buildBinaryTree(vector<T> &v) {
int idx = 0;
auto root = createBinaryTreeNode(v, idx);
return root;
}
/*****************************************************************************
* print()
*/
void print() {
int level = 0;
printBinaryTree(root, level);
}
/*****************************************************************************
* compare()
*/
int compare(BinaryTree<T> &that) {
int diff = 0;
diff = this->root->compare(that.getHead());
return diff;
}
/*****************************************************************************
* flatten()
*/
void flatten() {
root = root->flatten(root);
}
/*****************************************************************************
* isBalancedTree()
*/
bool isBalancedTree() {
NodeDepth depth = { true, 0 };
int treeDepth = getNodeDepth(root, depth);
iLog(0, "Tree is balanced = %d and has maximum depth %d",
depth.balanced, depth.maxDepth);
(void)treeDepth;
return depth.balanced;
}
/*****************************************************************************
* iterativeInOrderTraversal()()
*/
void iterativeInOrderTraversal() {
shared_ptr<BinaryTreeNode<T>> curr = root, prev = nullptr, next = nullptr;
int level = 0;
while(curr) {
if(!prev || prev->left == curr || prev->right == curr) {
if(curr->left) {
next = curr->left;
} else {
iLog(level, "node %d", curr->data);
next = curr->right ? curr->right : curr->parent;
}
} else if(curr->left == prev) {
iLog(level, "node %d", curr->data);
next = curr->right ? curr->right : curr->parent;
} else if(curr->right == prev) {
next = curr->parent;
}
prev = curr;
curr = next;
}
}
/*****************************************************************************
* findKthNode()()
*/
shared_ptr<BinaryTreeNode<T>> findKthNode(int k) {
shared_ptr<BinaryTreeNode<T>> curr = root;
while(curr) {
int leftSize = curr->left ? curr->left->size : 0;
if(leftSize + 1 < k) {
k -= (leftSize + 1);
curr = curr->right;
} else if(k == leftSize + 1) {
return curr;
} else {
curr = curr->left;
}
}
return curr;
}
/*****************************************************************************
* reconstructFromPreorder()()
*/
void reconstructFromPreorder(vector<T> v) {
stack<shared_ptr<BinaryTreeNode<T>>> s;
for(auto it = v.crbegin(); it != v.crend(); it++) {
if(*it == '0') {
s.push(nullptr);
} else {
shared_ptr<BinaryTreeNode<T>> l = s.top(); s.pop();
shared_ptr<BinaryTreeNode<T>> r = s.top(); s.pop();
/*
shared_ptr<BinaryTreeNode<T>> n =
make_shared<BinaryTreeNode<T>>(
BinaryTreeNode<T>{*it, nullptr, l, r, 0});
*/
shared_ptr<BinaryTreeNode<T>> n =
make_shared<BinaryTreeNode<T>>(BinaryTreeNode<T>(*it));
n->left = l;
n->right = r;
s.emplace(n);
}
}
root = s.top();
}
protected:
struct NodeDepth {
bool balanced;
int maxDepth;
};
/*****************************************************************************
* getHead()
*/
const shared_ptr<BinaryTreeNode<T>> getHead() {
return root;
}
/*****************************************************************************
* createBinaryTreeNode()
*/
shared_ptr<BinaryTreeNode<T>> createBinaryTreeNode(
vector<T> &v, int &idx, shared_ptr<BinaryTreeNode<T>> parent = nullptr) {
if (idx >= (int) v.size()) {
return nullptr;
}
/*
*/
int level = floor(log2(idx+1));
iLog(level, "Create node [%d]=%d", idx, v[idx]);
shared_ptr<BinaryTreeNode<T>> node =
shared_ptr<BinaryTreeNode<T>>(new BinaryTreeNode<T>(v[idx]));
int leftIdx = idx*2 + 1;
int rightIdx = idx*2 + 2;
node->size = 1;
node->parent = parent;
node->left = createBinaryTreeNode(v, leftIdx, node);
node->right = createBinaryTreeNode(v, rightIdx, node);
if(node->left)
node->size += node->left->size;
if(node->right)
node->size += node->right->size;
/*
cout << node->getData() << "-Left:" << node->left << endl;
cout << node->getData() << "-Right:" << node->right << endl;
*/
return node;
}
/*****************************************************************************
* printBinaryTree()
*/
void printBinaryTree(shared_ptr<BinaryTreeNode<T>> node, int level) {
if(node == nullptr) {
return;
}
iLog(level, "Node: %d [size=%d]", node->getData(), node->getSize());
printBinaryTree(node->left, level+1);
printBinaryTree(node->right, level+1);
}
/*****************************************************************************
* getNodeDepth()()
*/
int getNodeDepth(shared_ptr<BinaryTreeNode<T>> node, NodeDepth &depth) {
if (node == nullptr) {
return 0;
}
int nodeDepth = 1;
int leftDepth = getNodeDepth(node->left, depth);
int rightDepth = getNodeDepth(node->right, depth);
if (abs(leftDepth - rightDepth) > 1) {
depth.balanced = false;
}
nodeDepth += std::max(leftDepth, rightDepth);
depth.maxDepth = nodeDepth > depth.maxDepth ? nodeDepth : depth.maxDepth;
return nodeDepth;
}
};
#endif /* __TREES_H__ */