-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.hh
553 lines (441 loc) · 11.9 KB
/
binary_tree.hh
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/**
* @file binary_tree.hh
* @brief Declaratia pentru binary search tree
* @version 0.1
* @date 2021-04-23
*
* @copyright Copyright (c) 2021
*
*/
#ifndef BINARY_TREE_HH
#define BINARY_TREE_HH
#include "tree_node.hh"
#include <ostream>
#include <iostream>
#define PARENT(z) ((TreeNode<T> *)z->get_parent())
#define GRANDPARENT(z) ((TreeNode<T> *)(z->get_parent()->get_parent()))
template <class T>
class BinarySearchTree
{
// printeaza nodurile arborelui in ordine crescatoare
// pornind de la un anumit nod;
std::ostream &in_order_tree_print(std::ostream &out, const TreeNode<T> *x, bool &c) const;
void clear(const TreeNode<T> *x);
protected:
// radacina arborelui;
TreeNode<T> *root;
// pentru compatibilitate, in cazul unui arbore simplu va fi nullptr,
// iar pt rb-tree, pointeaza catre un tip special de nod, de culoare BLACK;
TreeNode<T> *nil;
// numarul de elemente;
size_t count;
void tree_transplant(TreeNode<T> *u, TreeNode<T> *v);
// muta nodul v in locul nodului u
// (deci tot subarborele cu radacina in v este "transportat" in locul nodului u)
virtual void transplant(TreeNode<T> *u, TreeNode<T> *v);
// returneaza radacina arborelui;
const TreeNode<T> *get_root() const;
// cauta nodul cu cheia k din subarborele cu radacina x;
const TreeNode<T> *search(const TreeNode<T> *x, TreeNode<T> k) const;
// insereaza nodul x in arbore;
const TreeNode<T> *insert(TreeNode<T> *x);
virtual void remove_node(const TreeNode<T> *z);
// returneaza nodul cu cheia minima din subarborele cu radacina in nodul x;
const TreeNode<T> *minimum(const TreeNode<T> *x) const;
// returneaza nodul cu cheia maxima din subarborele cu radacina in nodul x;
const TreeNode<T> *maximum(const TreeNode<T> *x) const;
// returneaza nodul succesor al nodului x;
const TreeNode<T> *successor(const TreeNode<T> *x) const;
// returneaza nodul predecesor nodului x;
const TreeNode<T> *predecessor(const TreeNode<T> *x) const;
void kth_element(const TreeNode<T> *x, TreeNode<T> **target, size_t k, size_t &c) const;
public:
BinarySearchTree();
virtual ~BinarySearchTree();
/*
* Metodele din cerinta proiectului;
*/
// insereaza o noua cheie in arbore;
virtual void insert(T x);
// sterge o cheie din arbore;
void remove(T x);
// returneaza minimul din intregul arbore;
T minimum() const;
// returneaza maximul din intregul arbore;
T maximum() const;
// returneaza succesorul lui x;
T successor(T x) const;
// returneaza predecesorului lui x;
T predecessor(T x) const;
// returneaza elementul cu indexul k in ordine crescatoare;
T kth_element(size_t k) const;
// returneaza numarul de noduri din arbore;
size_t size() const;
// returneaza true daca nodul x exista in arbore, altfel false;
bool exists(T x) const;
// elibereaza recursiv memoria pentru toate nodurile arborelui;
void clear();
template <class t>
friend std::ostream &operator<<(std::ostream &out, const BinarySearchTree<t> &tree);
};
template <class T>
BinarySearchTree<T>::BinarySearchTree()
{
this->root = nullptr;
this->nil = nullptr;
this->count = 0;
}
template <class T>
BinarySearchTree<T>::~BinarySearchTree()
{
this->clear(this->root);
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::get_root() const
{
return this->root;
}
template <class T>
void BinarySearchTree<T>::tree_transplant(TreeNode<T> *u, TreeNode<T> *v)
{
if (PARENT(u) == this->nil)
{
this->root = v;
}
else if (u == PARENT(u)->get_left())
{
PARENT(u)->set_left(v);
}
else
{
PARENT(u)->set_right(v);
}
}
template <class T>
void BinarySearchTree<T>::transplant(TreeNode<T> *u, TreeNode<T> *v)
{
this->tree_transplant(u, v);
// daca v e nenul, atunci parintele lui u devine parintele lui v;
if (v != this->nil)
{
v->set_parent(u->get_parent());
}
}
template <class T>
std::ostream &BinarySearchTree<T>::in_order_tree_print(std::ostream &out, const TreeNode<T> *x, bool &c) const
{
if (x != this->nil)
{
// se parcurge recursiv arborele pana la nodul
// cu cheia cea mai mica;
this->in_order_tree_print(out, x->get_left(), c);
// initial c = 0, deci nu se printeaza virgula
if (c)
{
out << ", ";
} // dupa ce se printeza primul nod, c devine 1
// deci la urmatorul apel va printa si virgula
else
{
c = 1;
}
size_t ct = x->get_count() - 1;
while (ct)
{
out << *x << ", ";
ct--;
}
out << *x; /* << "[" << *x->get_left() << " " << *x->get_right() << "]"*/
;
this->in_order_tree_print(out, x->get_right(), c);
}
return out;
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::search(const TreeNode<T> *x, TreeNode<T> k) const
{
while (x != this->nil and k != x->get_key())
{
if (k < x->get_key())
{
x = x->get_left();
}
else
{
x = x->get_right();
}
}
return x;
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::insert(TreeNode<T> *z)
{
// pointer la radacina arborelui;
TreeNode<T> *x = this->root;
// auxiliar;
TreeNode<T> *y = this->nil;
// traverseaza arborele:
// la fiecare pas se decide daca merge la stanga sau dreapata
// comparand cheile, pana cand se ajunge la o frunza;
while (x != this->nil)
{
y = x;
if (z->get_key() < x->get_key())
{
x = (TreeNode<T> *)x->get_left();
}
else if (z->get_key() > x->get_key())
{
x = (TreeNode<T> *)x->get_right();
}
else
{
x->inc_count();
this->count++;
return this->nil;
}
}
// seteaza parintele lui z;
z->set_parent(y);
// daca arborele nu a avut radacina
if (y == this->nil)
{
// atunci z devine radacina;
this->root = z;
}
// altfel se decide daca z este copilul drept sau stang al lui y,
// comparand cheile;
else if (z->get_key() < y->get_key())
{
y->set_left(z);
}
else
{
y->set_right(z);
}
this->count++;
return z;
}
template <class T>
void BinarySearchTree<T>::remove_node(const TreeNode<T> *z)
{
if (z->get_left() == this->nil)
{
this->transplant((TreeNode<T> *)z, (TreeNode<T> *)z->get_right());
}
else if (z->get_right() == this->nil)
{
this->transplant((TreeNode<T> *)z, (TreeNode<T> *)z->get_left());
}
else
{
TreeNode<T> *y = (TreeNode<T> *)this->minimum(z->get_right());
if (y->get_parent() != z)
{
this->transplant(y, (TreeNode<T> *)y->get_right());
y->set_right(z->get_right());
((TreeNode<T> *)y->get_right())->set_parent(y);
}
this->transplant((TreeNode<T> *)z, y);
y->set_left(z->get_left());
((TreeNode<T> *)y->get_left())->set_parent(y);
}
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::minimum(const TreeNode<T> *x) const
{
if (x == this->nil)
{
return x;
}
while (x->get_left() != this->nil)
x = x->get_left();
return x;
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::maximum(const TreeNode<T> *x) const
{
if (x == this->nil)
{
return x;
}
while (x->get_right() != this->nil)
x = x->get_right();
return x;
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::successor(const TreeNode<T> *x) const
{
// daca copilul drept este nenul
if (x->get_right() != this->nil)
{
return this->minimum(x->get_right());
}
const TreeNode<T> *y = x->get_parent();
// cat timp x e copil drept se parcurge arborele spre radacina
while (y != this->nil and x == y->get_right())
{
x = y;
y = y->get_parent();
}
return y;
}
template <class T>
const TreeNode<T> *BinarySearchTree<T>::predecessor(const TreeNode<T> *x) const
{
if (x->get_left() != this->nil)
{
return this->maximum(x->get_left());
}
const TreeNode<T> *y = x->get_parent();
while (y != this->nil and x == y->get_left())
{
x = y;
y = y->get_parent();
}
return y;
}
template <class T>
void BinarySearchTree<T>::kth_element(const TreeNode<T> *x, TreeNode<T> **target, size_t k, size_t &c) const
{
if (x != this->nil and c < k)
{
this->kth_element(x->get_left(), target, k, c);
if (c < k)
{
*target = (TreeNode<T> *)x;
}
c += x->get_count();
this->kth_element(x->get_right(), target, k, c);
}
}
template <class T>
void BinarySearchTree<T>::clear(const TreeNode<T> *x)
{
// elibereaza recursiv memoria alocata pe fiecare nod;
if (x != this->nil)
{
this->clear(x->get_left());
this->clear(x->get_right());
delete x;
}
}
template <class T>
void BinarySearchTree<T>::insert(T n)
{
// noul nod care va fi inserat;
TreeNode<T> *z = new TreeNode<T>(n);
this->insert(z);
}
template <class T>
void BinarySearchTree<T>::remove(T x)
{
TreeNode<T> *z = (TreeNode<T> *)this->search(this->root, x);
if (z == this->nil)
{
throw "Key not found";
}
z->dec_count();
this->count--;
if (not z->get_count())
{
this->remove_node(z);
}
}
template <class T>
T BinarySearchTree<T>::minimum() const
{
if (this->root == this->nil)
{
throw "Tree is empty";
}
return *this->minimum(this->root);
}
template <class T>
T BinarySearchTree<T>::maximum() const
{
if (this->root == this->nil)
{
throw "Tree is empty";
}
return *this->maximum(this->root);
}
template <class T>
T BinarySearchTree<T>::successor(T x) const
{
if (this->root == this->nil)
throw "Tree is empty";
// cauta nodul cu cheia lui x;
const TreeNode<T> *z = this->search(this->root, x);
// std::cout << "\nleft of " << x << " is " << *z->get_left() << "\n";
// std::cout << "\nright of " << x << " is " << *z->get_right() << "\n";
// eroare, daca nu s-a gasit cheia
if (z == this->nil)
throw "Key does not exist";
const TreeNode<T> *x_successor = this->successor(z);
if (x_successor == this->nil)
{
throw "Successor does not exist";
}
return *x_successor;
}
template <class T>
T BinarySearchTree<T>::predecessor(T x) const
{
if (this->root == this->nil)
throw "Tree is empty";
// cauta nodul cu cheia lui x;
const TreeNode<T> *z = this->search(this->root, x);
if (z == this->nil)
throw "Key does not exist";
if (z->get_left() != this->nil)
{
return *this->maximum(z->get_left());
}
const TreeNode<T> *x_predecessor = this->predecessor(z);
if (x_predecessor == this->nil)
{
throw "Predecessor does not exists";
}
return *x_predecessor;
}
template <class T>
size_t BinarySearchTree<T>::size() const
{
return this->count;
}
template <class T>
bool BinarySearchTree<T>::exists(T x) const
{
return this->search(this->root, x) != this->nil ? 1 : 0;
}
template <class T>
T BinarySearchTree<T>::kth_element(size_t k) const
{
if (not k or k > this->count)
{
throw "Requested element does not exist";
}
size_t c = 0;
TreeNode<T> *target = this->nil;
this->kth_element(this->root, &target, k, c);
return *target;
}
template <class T>
void BinarySearchTree<T>::clear()
{
this->clear(this->root);
this->root = this->nil;
}
template <class t>
std::ostream &operator<<(std::ostream &out, const BinarySearchTree<t> &tree)
{
const TreeNode<t> *root = tree.root;
// la primul apel nu se printeaza virgula
bool c = 0;
out << "{";
tree.in_order_tree_print(out, root, c);
out << "}";
return out;
}
#endif