-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search_tree1.h
465 lines (389 loc) · 11.7 KB
/
Search_tree1.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*****************************************
* UW User ID: uwuserid
* Submitted for ECE 250
* Semester of Submission: (Winter|Spring|Fall) 20NN
*
* By submitting this file, I affirm that
* I am the author of all modifications to
* the provided code.
*****************************************/
#ifndef RPOVDGBQN9TIEO3P
#define RPOVDGBQN9TIEO3P
#include "Exception.h"
#include "ece250.h"
#include <cassert>
template <typename Type>
class Search_tree {
public:
class Iterator;
private:
class Node {
public:
Type node_value;
int tree_height;
// The left and right sub-trees
Node *left_tree;
Node *right_tree;
// Hint as to how you can create your iterator
// Point to the previous and next nodes in linear order
Node *previous_node;
Node *next_node;
// Member functions
Node( Type const & = Type() );
void update_height();
int height() const;
bool is_leaf() const;
Node *front();
Node *back();
Node *find( Type const &obj );
void clear();
bool insert( Type const &obj, Node *&to_this );
bool erase( Type const &obj, Node *&to_this );
};
Node *root_node;
int tree_size;
// Hint as to how to start your linked list of the nodes in order
Node *front_sentinel;
Node *back_sentinel;
public:
class Iterator {
private:
Search_tree *containing_tree;
Node *current_node;
bool is_end;
// The constructor is private so that only the search tree can create an iterator
Iterator( Search_tree *tree, Node *starting_node );
public:
// DO NOT CHANGE THE SIGNATURES FOR ANY OF THESE
Type operator*() const;
Iterator &operator++();
Iterator &operator--();
bool operator==( Iterator const &rhs ) const;
bool operator!=( Iterator const &rhs ) const;
// Make the search tree a friend so that it can call the constructor
friend class Search_tree;
};
// DO NOT CHANGE THE SIGNATURES FOR ANY OF THESE
Search_tree();
~Search_tree();
bool empty() const;
int size() const;
int height() const;
Type front() const;
Type back() const;
Iterator begin();
Iterator end();
Iterator rbegin();
Iterator rend();
Iterator find( Type const & );
void clear();
bool insert( Type const & );
bool erase( Type const & );
// Friends
template <typename T>
friend std::ostream &operator<<( std::ostream &, Search_tree<T> const & );
};
//////////////////////////////////////////////////////////////////////
// Search Tree Public Member Functions //
//////////////////////////////////////////////////////////////////////
// The initialization of the front and back sentinels is a hint
template <typename Type>
Search_tree<Type>::Search_tree():
root_node( nullptr ),
tree_size( 0 ),
front_sentinel( new Search_tree::Node( Type() ) ),
back_sentinel( new Search_tree::Node( Type() ) ) {
front_sentinel->next_node = back_sentinel;
back_sentinel->previous_node = front_sentinel;
}
template <typename Type>
Search_tree<Type>::~Search_tree() {
clear(); // might as well use it...
delete front_sentinel;
delete back_sentinel;
}
template <typename Type>
bool Search_tree<Type>::empty() const {
return ( root_node == nullptr );
}
template <typename Type>
int Search_tree<Type>::size() const {
return tree_size;
}
template <typename Type>
int Search_tree<Type>::height() const {
return root_node->height();
}
template <typename Type>
Type Search_tree<Type>::front() const {
if ( empty() ) {
throw underflow();
}
return root_node->front()->node_value;
}
template <typename Type>
Type Search_tree<Type>::back() const {
if ( empty() ) {
throw underflow();
}
return root_node->back()->node_value;
}
template <typename Type>
typename Search_tree<Type>::Iterator Search_tree<Type>::begin() {
return empty() ? Iterator( this, back_sentinel ) : Iterator( this, root_node->front() );
}
template <typename Type>
typename Search_tree<Type>::Iterator Search_tree<Type>::end() {
return Iterator( this, back_sentinel );
}
template <typename Type>
typename Search_tree<Type>::Iterator Search_tree<Type>::rbegin() {
return empty() ? Iterator( this, front_sentinel ) : Iterator( this, root_node->back() );
}
template <typename Type>
typename Search_tree<Type>::Iterator Search_tree<Type>::rend() {
return Iterator( this, front_sentinel );
}
template <typename Type>
typename Search_tree<Type>::Iterator Search_tree<Type>::find( Type const &obj ) {
if ( empty() ) {
return Iterator( this, back_sentinel );
}
typename Search_tree<Type>::Node *search_result = root_node->find( obj );
if ( search_result == nullptr ) {
return Iterator( this, back_sentinel );
} else {
return Iterator( this, search_result );
}
}
template <typename Type>
void Search_tree<Type>::clear() {
if ( !empty() ) {
root_node->clear();
root_node = nullptr;
tree_size = 0;
}
// Reinitialize the sentinels
front_sentinel->next_node = back_sentinel;
back_sentinel->previous_node = front_sentinel;
}
template <typename Type>
bool Search_tree<Type>::insert( Type const &obj ) {
if ( empty() ) {
root_node = new Search_tree::Node( obj );
tree_size = 1;
// Connect root and front/back together
front_sentinel->next_node = root_node;
back_sentinel->previous_node = root_node;
root_node->next_node = back_sentinel;
root_node->previous_node = front_sentinel;
return true;
} else if ( root_node->insert( obj, root_node ) ) {
++tree_size;
return true;
} else {
return false;
}
}
template <typename Type>
bool Search_tree<Type>::erase( Type const &obj ) {
if ( !empty() && root_node->erase( obj, root_node ) ) {
--tree_size;
return true;
} else {
return false;
}
}
//////////////////////////////////////////////////////////////////////
// Node Public Member Functions //
//////////////////////////////////////////////////////////////////////
template <typename Type>
Search_tree<Type>::Node::Node( Type const &obj ):
node_value( obj ),
left_tree( nullptr ),
right_tree( nullptr ),
next_node( nullptr ),
previous_node( nullptr ),
tree_height( 0 ) {
// does nothing
}
template <typename Type>
void Search_tree<Type>::Node::update_height() {
tree_height = std::max( left_tree->height(), right_tree->height() ) + 1;
}
template <typename Type>
int Search_tree<Type>::Node::height() const {
return ( this == nullptr ) ? -1 : tree_height;
}
// Return true if the current node is a leaf node, false otherwise
template <typename Type>
bool Search_tree<Type>::Node::is_leaf() const {
return ( (left_tree == nullptr) && (right_tree == nullptr) );
}
// Return a pointer to the front node
template <typename Type>
typename Search_tree<Type>::Node *Search_tree<Type>::Node::front() {
return ( left_tree == nullptr ) ? this : left_tree->front();
}
// Return a pointer to the back node
template <typename Type>
typename Search_tree<Type>::Node *Search_tree<Type>::Node::back() {
return ( right_tree == nullptr ) ? this : right_tree->back();
}
template <typename Type>
typename Search_tree<Type>::Node *Search_tree<Type>::Node::find( Type const &obj ) {
if ( obj == node_value ) {
return this;
} else if ( obj < node_value ) {
return (left_tree == nullptr) ? nullptr : left_tree->find( obj );
} else {
return ( right_tree == nullptr ) ? nullptr : right_tree->find( obj );
}
}
// Recursively clear the tree
template <typename Type>
void Search_tree<Type>::Node::clear() {
if ( left_tree != nullptr ) {
left_tree->clear();
}
if ( right_tree != nullptr ) {
right_tree->clear();
}
delete this;
}
template <typename Type>
bool Search_tree<Type>::Node::insert( Type const &obj, Search_tree<Type>::Node *&to_this ) {
if ( obj < node_value ) {
if ( left_tree == nullptr ) {
left_tree = new Search_tree<Type>::Node( obj );
update_height();
//connecting the nodes for iterator
left_tree->next_node = to_this;
left_tree->previous_node = to_this->previous_node;
to_this->previous_node->next_node = left_tree;
to_this->previous_node = left_tree;
return true;
} else {
if ( left_tree->insert( obj, left_tree ) ) {
update_height();
return true;
} else {
return false;
}
}
} else if ( obj > node_value ) {
if ( right_tree == nullptr ) {
right_tree = new Search_tree<Type>::Node( obj );
update_height();
right_tree->next_node = to_this->next_node;
right_tree->previous_node = to_this;
to_this->next_node->previous_node = right_tree;
to_this->next_node = right_tree;
return true;
} else {
if ( right_tree->insert( obj, right_tree ) ) {
update_height();
return true;
} else {
return false;
}
}
} else {
return false;
}
}
template <typename Type>
bool Search_tree<Type>::Node::erase( Type const &obj, Search_tree<Type>::Node *&to_this ) {
if ( obj < node_value ) {
if ( left_tree == nullptr ) {
return false;
} else {
if ( left_tree->erase( obj, left_tree ) ) {
update_height();
return true;
}
return false;
}
} else if ( obj > node_value ) {
if ( right_tree == nullptr ) {
return false;
} else {
if ( right_tree->erase( obj, right_tree ) ) {
update_height();
return true;
}
return false;
}
} else {
assert( obj == node_value );
if ( is_leaf() ) {
to_this = nullptr;
delete this;
} else if ( left_tree == nullptr ) {
to_this = right_tree;
delete this;
} else if ( right_tree == nullptr ) {
to_this = left_tree;
delete this;
} else {
node_value = right_tree->front()->node_value;
right_tree->erase( node_value, right_tree );
update_height();
}
return true;
}
}
//////////////////////////////////////////////////////////////////////
// Iterator Private Constructor //
//////////////////////////////////////////////////////////////////////
template <typename Type>
Search_tree<Type>::Iterator::Iterator( Search_tree<Type> *tree, typename Search_tree<Type>::Node *starting_node ):
containing_tree( tree ),
current_node( starting_node ) {
// This is done for you...
// Does nothing...
}
//////////////////////////////////////////////////////////////////////
// Iterator Public Member Functions //
//////////////////////////////////////////////////////////////////////
template <typename Type>
Type Search_tree<Type>::Iterator::operator*() const {
// This is done for you...
return current_node->node_value;
}
template <typename Type>
typename Search_tree<Type>::Iterator &Search_tree<Type>::Iterator::operator++() {
// Update the current node to the node containing the next higher value
// If we are already at end do nothing
// Your implementation here, do not change the return value
current_node = current_node->next_node;
return *this;
}
template <typename Type>
typename Search_tree<Type>::Iterator &Search_tree<Type>::Iterator::operator--() {
// Update the current node to the node containing the next smaller value
// If we are already at either rend, do nothing
// Your implementation here, do not change the return value
current_node = current_node->previous_node;
return *this;
}
template <typename Type>
bool Search_tree<Type>::Iterator::operator==( typename Search_tree<Type>::Iterator const &rhs ) const {
// This is done for you...
return ( current_node == rhs.current_node );
}
template <typename Type>
bool Search_tree<Type>::Iterator::operator!=( typename Search_tree<Type>::Iterator const &rhs ) const {
// This is done for you...
return ( current_node != rhs.current_node );
}
//////////////////////////////////////////////////////////////////////
// Friends //
//////////////////////////////////////////////////////////////////////
// You can modify this function however you want: it will not be tested
template <typename T>
std::ostream &operator<<( std::ostream &out, Search_tree<T> const &list ) {
out << "not yet implemented";
return out;
}
#endif