-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.h
315 lines (267 loc) · 7.17 KB
/
bst.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
/**
* bst.h
* Implements a(n unbalanced) BST storing Key,Value pairs
*/
#include <iostream>
#include <exception>
#include <cstdlib>
#include <utility>
/* -----------------------------------------------------
* Regular Binary Tree Node_s
------------------------------------------------------*/
template <class KeyType, class ValueType>
class Node_ {
public:
Node_ (const KeyType & k, const ValueType & v, Node_<KeyType, ValueType> *p)
: _item(k, v)
// the default is to create new node_s as leaves
{ _parent = p; _left = _right = NULL; }
virtual ~Node_()
{ }
std::pair<const KeyType, ValueType> const & getItem () const
{ return _item; }
std::pair<const KeyType, ValueType> & getItem ()
{ return _item; }
const KeyType & getKey () const
{ return _item.first; }
const ValueType & getValue () const
{ return _item.second; }
/* the next three functions are virtual because for Red-Black-Trees,
we'll want to use Red-Black node_s, and for those, the
getParent, getLeft, and getRight functions should return
Red-Black node_s, not just ordinary node_s.
That's an advantage of using getters/setters rather than a struct. */
virtual Node_<KeyType, ValueType> *getParent () const
{ return _parent; }
virtual Node_<KeyType, ValueType> *getLeft () const
{ return _left; }
virtual Node_<KeyType, ValueType> *getRight () const
{ return _right; }
void setParent (Node_<KeyType, ValueType> *p)
{ _parent = p; }
void setLeft (Node_<KeyType, ValueType> *l)
{ _left = l; }
void setRight (Node_<KeyType, ValueType> *r)
{ _right = r; }
void setValue (const ValueType &v)
{ _item.second = v; }
protected:
std::pair<const KeyType, ValueType> _item;
Node_ <KeyType, ValueType> *_left, *_right, *_parent;
};
/* -----------------------------------------------------
* Regular Binary Search Tree
------------------------------------------------------*/
template <class KeyType, class ValueType>
class BinarySearchTree {
protected:
// Main data member of the class
Node_<KeyType, ValueType> *root;
public:
/**
* Constructor
*/
BinarySearchTree () {root = NULL; }
/**
* Destructor
*/
~BinarySearchTree () { deleteAll (root); }
void insert(Node_<KeyType,ValueType>* n){
insert_(n, root);
}
/**
* Prints the entire tree structure in a nice format
*
* It will denote subtrees in [] brackets.
* This could be helpful if you want to debug your functions.
*/
void print () const
{
printRoot (root);
std::cout << "\n";
}
/**
* An In-Order iterator
* !!! You must implement this !!!
*/
class iterator {
public:
/**
* Initialize the internal members of the iterator
*/
iterator(Node_<KeyType,ValueType>* ptr){
curr = ptr;
}
std::pair<const KeyType,ValueType>& operator*()
{ return curr->getItem(); }
std::pair<const KeyType,ValueType>* operator->()
{ return &(curr->getItem()); }
/**
* Checks if 'this' iterator's internals have the same value
* as 'rhs'
*/
bool operator==(const iterator& rhs) const{
return (rhs.curr == (*this).curr);
}
/**
* Checks if 'this' iterator's internals have a different value
* as 'rhs'
*/
bool operator!=(const iterator& rhs) const{
return !(*this == rhs);
}
/*
* Advances the iterator's location using an in-order sequencing
*/
iterator& operator++(){
if(curr == NULL){return *this;}
if(AmIlastnode_(curr)){
curr = NULL;
return *this;
}
else if(curr->getRight() != NULL){
curr = curr->getRight();
while(curr->getLeft() != NULL){curr = curr->getLeft();}
return *this;
}
else{
while(1){
if(curr->getParent() == NULL){
curr = NULL;
return *this;
}
if(curr->getParent()->getLeft() == curr){
curr = curr->getParent();
return *this;
}
curr = curr->getParent();
}
}
}
Node_<KeyType, ValueType>* curr;
protected:
//you are welcome to add any necessary variables and helper functions here.
bool AmIlastnode_(Node_<KeyType,ValueType>* n){
if(n->getRight() != NULL){
return 0;
}
while(n->getParent() != NULL){
if(n->getParent()->getKey() < n->getKey()){
n = n->getParent();
continue;
}
return 0;
}
return 1;
}
};
/**
* Returns an iterator to the "smallest" item in the tree
*/
iterator begin(){
Node_<KeyType, ValueType>* temp = root;
while(temp->getLeft() != NULL){
temp = temp->getLeft();
}
iterator ite(temp);
return ite;
}
/**
* Returns an iterator whose value means INVALID
*/
iterator end(){
iterator ite(NULL);
return ite;
}
/**
* Returns an iterator to the item with the given key, k
* or the end iterator if k does not exist in the tree
*/
iterator find (const KeyType & k) const
{
Node_<KeyType, ValueType> *curr = internalFind(k);
iterator it(curr);
return it;
}
protected:
void insert_(Node_<KeyType,ValueType>* tobefixed, Node_<KeyType, ValueType>* temp){
if(temp->getKey() == tobefixed->getItem().first){
return;
}
else if(temp->getKey() > tobefixed->getItem().first){
if(temp->getLeft() == NULL){
tobefixed->setParent(temp);
temp->setLeft(tobefixed);
temp->getLeft()->setLeft(NULL);
temp->getLeft()->setRight(NULL);
return;
}
else{
insert_(tobefixed, temp->getLeft());
}
}
else{
if(temp->getRight() == NULL){
tobefixed->setParent(temp);
temp->setRight(tobefixed);
temp->getRight()->setRight(NULL);
temp->getRight()->setLeft(NULL);
return;
}
else{
insert_(tobefixed, temp->getRight());
}
}
}
/**
* Helper function to find a node_ with given key, k and
* return a pointer to it or NULL if no item with that key
* exists
*/
Node_<KeyType, ValueType>* internalFind(const KeyType& k) const
{
Node_<KeyType, ValueType> *curr = root;
Node_<KeyType,ValueType>* last_visited = root;
while (curr) {
if (curr->getKey() == k) {
return curr;
}
else if (k < curr->getKey()) {
last_visited = curr;
curr = curr->getLeft();
}
else {
last_visited = curr;
curr = curr->getRight();
}
}
return last_visited;
}
/**
* Helper function to print the tree's contents
*/
void printRoot (Node_<KeyType, ValueType> *r) const
{
if (r != NULL)
{
std::cout << "[";
printRoot (r->getLeft());
std::cout << " (" << r->getKey() << ", " << r->getValue() << ") ";
printRoot (r->getRight());
std::cout << "]";
}
}
/**
* Helper function to delete all the items
*/
void deleteAll (Node_<KeyType, ValueType> *r)
{
if (r != NULL)
{
deleteAll (r->getLeft());
deleteAll (r->getRight());
delete r;
}
}
};
/* Feel free to add member function definitions here if you need */