-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST Median Method.cpp
309 lines (263 loc) · 9.62 KB
/
BST Median Method.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
#include <memory>
#include <iostream>
//namespace csci2100 {
template <typename ItemType>
class BinarySearchTree {
protected:
class Node {
private:
ItemType _data;
std::unique_ptr<Node> _left{nullptr}, _right{nullptr};
Node *_parent{nullptr};
int _height{0};
int _numSubNodes{0};
public:
Node(ItemType data, std::unique_ptr<Node> left,
std::unique_ptr<Node> right,
Node *parent)
: _data(data), _left(std::move(left)), _right(std::move(right)),
_parent(parent) {}
ItemType getData() const { return _data; }
void setData(ItemType d) { _data = d; }
int getHeight() const { return _height; }
int getNumSubNodes() const {return _numSubNodes; }
void setHeight(int h) { _height = h; }
Node *getLeft() const { return _left.get(); }
Node *getRight() const { return _right.get(); }
Node *getParent() const { return _parent; }
std::unique_ptr<Node> acquireLeft() { return std::move(_left); }
std::unique_ptr<Node> acquireRight() { return std::move(_right); }
void setNumSubNodes(int newSubNodes){
_numSubNodes = newSubNodes;
}
void setLeft(std::unique_ptr<Node> newLeft) {
_left = std::move(newLeft);
}
void setRight(std::unique_ptr<Node> newRight) {
_right = std::move(newRight);
}
void setParent(Node *newParent) { _parent = newParent; }
std::unique_ptr<Node>& getLeftLink() { return _left; }
std::unique_ptr<Node>& getRightLink() { return _right; }
};
public:
class InternalIterator {
private:
Node* _position{nullptr};
BinarySearchTree& _tree;
public:
InternalIterator(Node* pos, BinarySearchTree& t) : _position(pos), _tree(t) {
}
ItemType getData() const { return _position->getData(); }
bool isNode() const { return _position != nullptr; }
bool isRoot() const { return _position->getParent() == nullptr; }
bool isLeaf() const { return _position->getLeft() == nullptr && _position->getRight() == nullptr; }
bool hasRightChild() const { return _position->getRight() != nullptr; }
bool hasLeftChild() const { return _position->getLeft() != nullptr; }
int getHeight() const { return _position->getHeight(); }
void setHeight(int h) { _position->setHeight(h); }
void up() { _position = _position->getParent(); }
void left() { _position = _position->getLeft(); }
void right() { _position = _position->getRight(); }
bool operator==(const InternalIterator other) { return _position == other._position; }
void addLeftChild(ItemType data) {
_position->setLeft(std::make_unique<Node>(data, nullptr, nullptr, _position));
_tree._size++;
_tree._updateHeights(_position);
}
void addRightChild(ItemType data) {
_position->setRight(std::make_unique<Node>(data, nullptr, nullptr, _position));
_tree._size++;
_tree._updateHeights(_position);
}
std::unique_ptr<Node>& getLeftLink() {
return _position->getLeftLink();
}
std::unique_ptr<Node>& getRightLink() {
return _position->getRightLink();
}
void rotateRight() {
if (isRoot()) _tree._rotateRight(_tree._root);
else if (_position->getParent()->getLeft() == _position)
_tree._rotateRight(_position->getParent()->getLeftLink());
else _tree._rotateRight(_position->getParent()->getRightLink());
_tree._updateHeights(_position);
}
void rotateLeft() {
if (isRoot()) _tree._rotateLeft(_tree._root);
else if (_position->getParent()->getLeft() == _position)
_tree._rotateLeft(_position->getParent()->getLeftLink());
else _tree._rotateLeft(_position->getParent()->getRightLink());
_tree._updateHeights(_position);
}
};
protected:
std::unique_ptr<Node> _root{nullptr};
int _size{0};
public:
BinarySearchTree() { }
int size() const { return _size; }
bool empty() const { return _size == 0; }
void clear() { _root = nullptr; }
InternalIterator root() { return InternalIterator(_root.get(), *this); }
void createRoot(ItemType data) {
_root = std::make_unique<Node>(data, nullptr, nullptr, nullptr);
_size = 1;
}
void insert(ItemType data) {
if (_root == nullptr) {
createRoot(data);
} else {
InternalIterator loop {root()};
while (true) {
if (loop.getData() >= data) {
if (loop.hasLeftChild()) {
loop.left();
} else {
loop.addLeftChild(data);
return;
}
} else {
if (loop.hasRightChild()) {
loop.right();
} else {
loop.addRightChild(data);
return;
}
}
}
loop.addLeftChild(data);
}
}
void _rotateRight(std::unique_ptr<Node>& link) {
std::unique_ptr<Node> original = std::move(link);
std::unique_ptr<Node> left = original->acquireLeft();
std::unique_ptr<Node> t1 = left->acquireLeft();
std::unique_ptr<Node> t2 = left->acquireRight();
std::unique_ptr<Node> t3 = original->acquireRight();
if (t1 != nullptr) t1->setParent(left.get());
left->setLeft(std::move(t1));
if (t2 != nullptr) t2->setParent(original.get());
original->setLeft(std::move(t2));
if (t3 != nullptr) t3->setParent(original.get());
original->setRight(std::move(t3));
if (original->getParent() == nullptr)
left->setParent(nullptr);
else
left->setParent(original->getParent());
original->setParent(left.get());
left->setRight(std::move(original));
link = std::move(left);
}
void _rotateLeft(std::unique_ptr<Node>& link) {
std::unique_ptr<Node> original = std::move(link);
std::unique_ptr<Node> right = original->acquireRight();
std::unique_ptr<Node> t1 = original->acquireLeft();
std::unique_ptr<Node> t2 = right->acquireLeft();
std::unique_ptr<Node> t3 = right->acquireRight();
if (t1 != nullptr) t1->setParent(original.get());
original->setLeft(std::move(t1));
if (t2 != nullptr) t2->setParent(original.get());
original->setRight(std::move(t2));
if (t3 != nullptr) t3->setParent(right.get());
right->setRight(std::move(t3));
if (original->getParent() == nullptr)
right->setParent(nullptr);
else
right->setParent(original->getParent());
original->setParent(right.get());
right->setLeft(std::move(original));
link = std::move(right);
}
void _updateHeights(Node *location) {
int h{0};
if (location->getLeft() != nullptr)
h = location->getLeft()->getHeight() + 1;
if (location->getRight() != nullptr) {
int r = location->getRight()->getHeight() + 1;
if (r > h)
h = r;
}
location->setHeight(h);
if (location->getParent() != nullptr)
_updateHeights(location->getParent());
}
int count (Node* node) const {
if (node == nullptr)
return 0;
return 1+count(node->getLeft())+count(node->getRight());
}
ItemType getMedian () const {
if (count(_root.get()) % 2 == 0) {
std::cout << "MultipleMedianError\nMedians: ";
}
if (_root == nullptr) { // no nodes to count
std::cout << "Cannot get median of empty tree\n";
return (ItemType) NULL;
} else { // count nodes on each side
int passLeft {0}; // nodes passed on left
int passRight {0}; // nodes passed on right
int countLeft, countRight;
Node* currNode {_root.get()}; // current node
while (currNode != nullptr) {
countLeft = count(currNode->getLeft()); // nodes on left
countRight = count(currNode->getRight()); // nodes on right
if (std::abs((countLeft+passLeft)-(countRight+passRight)) == 1) {
std::cout << currNode->getData() << " "; // found a median of MultipleMedianError
}
if (std::abs((countLeft+passLeft)-(countRight+passRight)) >= 1) { // still unbalanced
if (countLeft+passLeft > countRight+passRight) { // heavy on left
passRight += countRight+1; // add passed nodes + current node
currNode = currNode->getLeft();
} else { // heavy on right
passLeft += countLeft+1; // add passed nodes + current node
currNode = currNode->getRight(); // move right
}
} else {
return currNode->getData();
}
}
std::cout << "\n"; // only runs if no true median
return (ItemType) NULL; // only runs if no true median
}
}
};
int main() {
BinarySearchTree<char> bst_c;
bst_c.insert('d');
bst_c.insert('e');
bst_c.insert('b');
bst_c.insert('a');
bst_c.insert('c');
char median {bst_c.getMedian()};
std::cout << "Median: " << median << "\n" << std::endl; // c
BinarySearchTree<int> bst_i;
bst_i.insert(5);
bst_i.insert(3);
bst_i.insert(9);
bst_i.insert(1);
bst_i.insert(4);
bst_i.insert(8);
bst_i.insert(11);
bst_i.insert(-3);
bst_i.insert(2);
bst_i.insert(7);
bst_i.insert(12);
int median2 {bst_i.getMedian()};
std::cout << "Median: " << median2 << "\n" << std::endl; // 5
BinarySearchTree<int> bst_i2;
bst_i2.insert(5);
bst_i2.insert(3);
bst_i2.insert(9);
bst_i2.insert(1);
bst_i2.insert(4);
bst_i2.insert(8);
bst_i2.insert(11);
bst_i2.insert(-3);
bst_i2.insert(2);
bst_i2.insert(7);
bst_i2.insert(12);
bst_i2.insert(13);
int median3 {bst_i2.getMedian()};
std::cout << "Median: " << median3 << std::endl; // 5, 7 (No True Median)
}