-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeList.hpp
More file actions
340 lines (303 loc) · 10.3 KB
/
TreeList.hpp
File metadata and controls
340 lines (303 loc) · 10.3 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
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <set>
#include <stdexcept>
#include <utility>
#include <stack>
template <typename T>
class TreeList {
private: class Node; //forward declaration
public: class iterator; //forward declaration
private: Node* root; //head of TreeList
public: TreeList ()
{
root = &Node::NULL_NODE;
};
public: TreeList (std::size_t len, const T &val = nullptr)
{
root = &Node::NULL_NODE;
for (std::size_t i = 0; i < len; i++) push_back(val);
}
public: ~TreeList ()
{
clear();
}
public: bool empty() const
{
return (root->size == 0);
}
public: std::size_t size() const
{
return root->size;
}
public: T &operator [] (std::size_t index)
{
if (index >= size()) throw std::out_of_range("Index is out of bounds");
return root->getNodeAt(index)->data;
}
public: void push_back (const T &val)
{
insert(size(), val);
}
public: void pop_back ()
{
erase(size()-1);
}
public: void insert (std::size_t index, const T &val)
{
if (index > size()) throw std::out_of_range("Index is out of bounds");
if (size() == SIZE_MAX) throw std::length_error("Maximum size for TreeList reached");
root = root->insertAt(index, val);
}
public: void erase (std:: size_t index)
{
if (index >= size()) throw std::out_of_range("Index is out of bounds");
Node* delNode = NULL;
root = root->removeAt(index, &delNode);
delete delNode;
}
public: void clear ()
{
if (root != &Node::NULL_NODE)
{
delete root;
root = &Node::NULL_NODE;
}
}
public: iterator begin() const
{
return iterator(root);
}
public: iterator end() const
{
return iterator();
}
//unit tests
public: void checkStructure () const
{
assert (root != NULL);
std::set <const Node*> visited;
root->checkStructure (visited);
}
private: class Node {
public: static Node NULL_NODE;
public: T data;
public: int height;
public: std::size_t size;
public: Node* left;
public: Node* right;
public: Node() //empty Node
{
height = 0;
size = 0;
left = NULL;
right = NULL;
}
public: Node (const T &data)
{
this->data = data;
height = 1;
size = 1;
left = &NULL_NODE;
right = &NULL_NODE;
}
public: ~Node()
{
if (left != &NULL_NODE) delete left;
if (right != &NULL_NODE) delete right;
}
public: Node* getNodeAt (std:: size_t index)
{
assert (index < size); //to ensure that we have a valid index
std:: size_t leftSize = left->size;
if (index < leftSize) return left->getNodeAt(index);
else if (index > leftSize) return right->getNodeAt(index-leftSize-1);
else return this;
}
public: Node* insertAt (std::size_t index, const T& val)
{
assert (index <= size);
if (this == &NULL_NODE) return new Node(val); //first node in the list
std::size_t leftSize = left->size;
if (index <= leftSize) left = left->insertAt(index, val);
else right = right->insertAt(index-leftSize-1, val);
updateNode();
return balancedTree();
}
public: Node* removeAt (std::size_t index, Node **delNode)
{
assert (index < size);
std::size_t leftSize = left->size;
if (index < leftSize) left = left->removeAt(index, delNode);
else if (index > leftSize) right = right->removeAt(index-leftSize-1, delNode);
else if (left == &NULL_NODE && right == &NULL_NODE)
{
assert (*delNode == NULL);
*delNode = this;
return &NULL_NODE;
}
else if (left != &NULL_NODE && right == &NULL_NODE)
{
Node* resNode = left;
left = NULL;
assert(*delNode == NULL);
*delNode = this;
return resNode;
}
else if (left == &NULL_NODE && right != &NULL_NODE)
{
Node* resNode = right;
right = NULL;
assert(*delNode == NULL);
*delNode = this;
return resNode;
}
else
{
Node* suc = right;
while (suc->left != &NULL_NODE) suc = suc->left;
data = suc->data;
right = right->removeAt(0, delNode);
}
updateNode();
return balancedTree();
}
//balances the subtree and returns the new Head of balanced subtree(balancedNode)
private: Node* balancedTree ()
{
int bal = getBalance();
assert (std::abs(bal) <= 2);
Node* balancedNode = this;
if (bal == 2)
{
assert (std::abs(right->getBalance()) <= 1);
if (right->getBalance() == -1) right = right->rotateRight();
balancedNode = rotateLeft();
}
else if (bal == -2)
{
assert (std::abs(left->getBalance()) <= 1);
if (left->getBalance() == 1) left = left->rotateLeft();
balancedNode = rotateRight();
}
assert (std::abs(balancedNode->getBalance()) <= 1);
return balancedNode;
}
private: Node* rotateRight()
{
assert (left != &NULL_NODE);
Node* newHead = this->left;
this->left = newHead->right;
newHead->right = this;
this->updateNode();
newHead->updateNode();
return newHead;
}
private: Node* rotateLeft()
{
assert (right != &NULL_NODE);
Node *newHead = this->right;
this->right = newHead->left;
newHead->left = this;
this->updateNode();
newHead->updateNode();
return newHead;
}
//updates height and size values
private : void updateNode ()
{
assert (this != &NULL_NODE);
assert (left->height >= 0 && right->height >= 0);
assert (left->size >= 0 && right->size >= 0);
height = std::max(left->height, right->height) + 1;
size = left->size + right->size + 1;
assert (height >= 0 && size >= 0);
}
private : int getBalance () const
{
return (right->height - left->height);
}
//unit test
public: void checkStructure (std::set <const Node*> &visitedNodes) const
{
if (this == &NULL_NODE) return;
if (visitedNodes.find(this) != visitedNodes.end()) //node already in set (already visited)
{
throw std::logic_error("TreeList structure violated: Not a tree");
}
visitedNodes.insert(this);
left->checkStructure(visitedNodes);
right->checkStructure(visitedNodes);
if (height != std::max(left->height, right->height) + 1)
{
throw std::logic_error("TreeList structure violated: Incorrect height");
}
if (size != left->size + right->size + 1)
{
throw std::logic_error("TreeList structure violated: Incorrect size");
}
if (std::abs(getBalance()) > 1)
{
throw std::logic_error("TreeList structure violated: Tree Not Balanced");
}
}
};
public: class iterator {
private: std::stack <Node*> pathStack;
private: Node* root;
public: iterator () //used to invoke end()function
{
while (!pathStack.empty()) pathStack.pop();
root = &Node::NULL_NODE;
}
public: iterator (Node* node) //used to invoke start() function
{
while (!pathStack.empty()) pathStack.pop();
while (node != &Node::NULL_NODE)
{
pathStack.push(node);
node = node->left;
}
if (pathStack.empty()) throw std::underflow_error("Not a valid initialization");
this->root = pathStack.top();
pathStack.pop();
}
public: const bool operator != (iterator other)
{
return this->root != other.root;
}
public: const T &operator * ()
{
if (root == &Node::NULL_NODE) throw std::out_of_range("Iterator does not point to a valid entry");
return root->data;
}
public: const iterator &operator ++ ()
{
assert (root != &Node::NULL_NODE);
if (root->right != &Node::NULL_NODE)
{
root = root->right;
while (root != &Node::NULL_NODE)
{
pathStack.push(root);
root = root->left;
}
}
if (pathStack.empty()) //return end()
{
this->root = &Node::NULL_NODE;
return *this;
}
else //return next element
{
this->root = pathStack.top();
pathStack.pop();
return *this; //* has been overloaded
}
}
};
};
template <typename T>
typename TreeList<T>::Node TreeList<T>::Node::NULL_NODE;