-
Notifications
You must be signed in to change notification settings - Fork 0
/
tll.cpp
405 lines (372 loc) · 11.2 KB
/
tll.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
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
#include <iostream>
#include <string>
#include "tll.h"
using namespace std;
//=========================================================================
// constructor
// Parameters: none
// Return: none
//=========================================================================
template <class D, class T>
triplinkedlist<D,T>::triplinkedlist()
//preconditions: none
//postconditions: triply linked list object is created
{
head = nullptr;
}
//=========================================================================
// deconstructor
// Parameters: none
// Return: none
//=========================================================================
template <class D, class T>
triplinkedlist<D,T>::~triplinkedlist()
//preconditions: triply linked list object exists
//postconditions: triply linked list object is deleted
{
Node *x = this->head;
if (x)
{
deleteTree(x->left);
deleteTree(x->right);
delete x;
}
}
//=========================================================================
// deleteTree
// Parameters: none
// Return: none
//=========================================================================
template <class D, class T>
void triplinkedlist<D,T>::deleteTree(Node* node)
//preconditions: triply linked list object exists
//postconditions: triply linked list object is deleted
{
if (node != nullptr){
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
}
//=========================================================================
// insert
// Parameters:
// elem - element to insert into the tree
// Return: none
//=========================================================================
template <class D, class T>
void triplinkedlist<D,T>::insert( const Element<D,T> &elem)
//preconditions: triply linked list exists
//postconditions: triply linked list now includes the element to insert in the correct spot
{
//if empty, construct
Node *newNode = nullptr;
newNode -> item = elem;
Node *y = nullptr;
Node *x = head;
while ( x != nullptr){
y = x;
if (elem.get_key() < x->item.get_key())
x = x->left;
else
x = x->right;
}
newNode->parent = y;
if (y == nullptr) {
head = newNode;
} else if (elem.get_key() < y->item.get_key()) {
y->left = newNode;
} else {
y->right = newNode;
}
}
/*
//=========================================================================
// remove
// Parameters:
// k - key of node to remove from the list
// Return: none
//=========================================================================
template <class T, class D>
void triplinkedlist<T,D>::remove(const Node<T,D> &node )
//preconditions:
//postconditions:
{
if (elem.left == nullptr){
transplant(elem.right);
} else if (elem.right == nullptr)
}
*/
//=========================================================================
// empty
// Parameters: none
// Return:
// true - if the tree is empty
// false - if the tree has at least one node
//=========================================================================
template <class D, class T>
bool triplinkedlist<D,T>::empty( void )
//preconditions: the tree object exists
//postconditions: the boolean value returned correctly describes the tree
{
return head == nullptr;
}
//=========================================================================
// max_key
// Parameters: none
// Return:
// the key associated with the max key in the tree
//=========================================================================
template <class D, class T>
T triplinkedlist<D,T>::max_key()
//preconditions: the tree object exists and is not empty
//postconditions: the largest key in the tree is returned
{
Node *x = this->head;
Node *max = this->head;
while (x != NULL){
max = x;
x = x->left;
}
return max->item.get_key();
}
//=========================================================================
// max_data
// Parameters: none
// Return:
// the data value associated with the largest key in the tree
//=========================================================================
template <class D, class T>
D triplinkedlist<D,T>::max_data()
//preconditions: the tree object exists and is not empty
//postconditions: the data value associated with the largest key in the tree is correctly returned
{
Node *x = this->head;
Node *max = this->head;
while (x != NULL){
max = x;
x = x->left;
}
return max->item.get_data();
}
//=========================================================================
// min_key
// Parameters: none
// Return:
// the key associated with the minimum key in the tree
//=========================================================================
template <class D, class T>
T triplinkedlist<D,T>::min_key()
//preconditions: the tree object exists and is not empty
//postconditions: the key associated with the minimum key in the tree is correctly returned
{
Node *x = this->head;
Node *min = this->head;
while (x != NULL){
min = x;
x = x->right;
}
return min->item.get_key();
}
//=========================================================================
// min_data
// Parameters: none
// Return:
// the data value associated with the minimum key in the tree
//=========================================================================
template <class D, class T>
D triplinkedlist<D,T>::min_data()
//preconditions: the tree object exists and is not empty
//postconditions: the data value associated with the minimum key in the tree is correctly returned
{
Node *x = this->head;
Node *min = this->head;
while (x != NULL){
min = x;
x = x->right;
}
return min->item.get_data();
}
//=========================================================================
// successor
// Parameters:
// k - the key of the node to find successor for
// Return:
// the node that has the next largest key after k in the tree
//=========================================================================
template <class D, class T>
T triplinkedlist<D,T>::successor(const T k)
//preconditions: the tree object exists and contains the key k
//postconditions: the correct successor of the node with key k is returned
{
Node *x = findNode(this, k);
if ( x == nullptr )
return nullptr;
else{
Node y = x->parent;
while ( y != NULL && x == y->right){
x = y;
y = y->parent;
}
return y;
}
}
/*
//=========================================================================
// findNode
// Parameters:
// tree - tree object to search for node
// k - key of the node to search for
// Return:
// node with key k
//=========================================================================
template <class T, class D>
Node* triplinkedlist<T,D>::findNode(const triplinkedlist& tree, const T k)
//preconditions: the tree object tree exists
//postconditions: the node with key k is correctly returned or NULL is returned if the k is not in the tree
{
Node *x = tree->head;
while (x != nullptr && x->item.get_key() != k) {
if (k < x->item.get_key())
x = x->left;
else
x = x->right;
}
return x;
}
*/
//=========================================================================
// in_order
// Parameters: none
// Return:
// the string of all keys of the tree in ascending order
//=========================================================================
template <class D, class T>
string triplinkedlist<D,T>::in_order()
//preconditions:
//postconditions:
{
string s = "";
in_order(head, s);
return s;
}
/*
//=========================================================================
// trim
// Parameters:
// Return:
//=========================================================================
template <class T, class D>
void triplinkedlist<T,D>::trim(T low, T high)
//preconditions:
//postconditions:
{
{
if (root == nullptr) return nullptr;
// Trim the left and right subtrees
root->left = trimRecursively(root->left, low, high);
if (root->left){
root->left->parent = root;
}
root->right = trimRecursively(root->right, low, high);
if (root->right) {
root->right->parent = root;
}
// If the current node's key is less than low, trim it and return the right subtree
if (root->item.key < low) {
Node* rightSubtree = root->right;
delete root; // Assume delete deallocates memory
return rightSubtree;
}
// If the current node's key is greater than high, trim it and return the left subtree
else if (root->item.key > high) {
Node* leftSubtree = root->left;
delete root; // Assume delete deallocates memory
return leftSubtree;
}
// If the current node is within the range, return it
return root;
}
Node *x = this.head;
Node *y = x.parent;
//check RHS for high
if (x.key < high){
while ( x != NULL && x.key < high ){
y = x;
x = x.right;
}
if (x != NULL ){
x.head = x
triplylinkedlist<T> yt;
yt(this)
}
}
//check LHS for high
if (x.key > high){
while ( x != NULL && x.key > high ){
y = x;
x = x.left;
}
}
if (x != NULL ){
}
//check LHS for low
if (x.key > low){
while ( x != NULL && x.key > low ){
y = x;
x = x.left;
}
}
//check RHS for low
if (x.key < low){
while ( x != NULL && x.key < low ){
y = x;
x = x.right;
}
}
}
*/
//=========================================================================
// transplant
// Parameters:
// u - node of the higher node to transplant
// v - node of the lower node to transplant
// Return: none
//=========================================================================
template <class D, class T>
void triplinkedlist<D,T>::transplant( Node u, Node v)
//preconditions: the tree object exists and
//postconditions: node ’s parent be- comes node ’s parent, and ’s parent ends up having as its appropriate child
{
if ( u.parent == nullptr )
this->head = v;
else if (u == u->parent->left )
u->parent->left = v;
else (u->parent->right = v);
if ( v != NULL )
v->parent = u->parent;
}
//=========================================================================
// get
// Parameters:
// k - key of a node to find its data
// Return:
// the data associated with the key k
//=========================================================================
template <class D, class T>
D triplinkedlist<D,T>::get(const T k)
//preconditions: the tree object exists and contains the key k
//postconditions: the data associated with the key k is correctly returned and NULL if k is not in the tree
{
Node *x = this->head;
while (x != nullptr && x->key != k) {
if (k < x->item.key)
x = x->left;
else
x = x->right;
}
if (x != nullptr && x->item.key == k)
return x->item.get_data();
else
return NULL;
}