-
Notifications
You must be signed in to change notification settings - Fork 3
/
redYellowTree.h
689 lines (578 loc) · 14.6 KB
/
redYellowTree.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#pragma once
#include<iostream>
#include<algorithm>
#include<thread>
#include<chrono>
#include <SDL.h>
#include <SDL_ttf.h>
#include <queue>
#include<Windows.h>
#include<conio.h>
#include "redYellowNode.h"
#include "redYellowVisualizer.h"
using namespace std;
class redYellowTree {
RYNode* root;
RYNode* TNULL;
// Preorder
void preOrderHelper(RYNode* node) {
if (node != TNULL) {
std::cout << node->data << " ";
preOrderHelper(node->left);
preOrderHelper(node->right);
}
}
// Inorder
void inOrderHelper(RYNode* node) {
if (node != TNULL) {
inOrderHelper(node->left);
std::cout << node->data << " ";
inOrderHelper(node->right);
}
}
// Post order
void postOrderHelper(RYNode* node) {
if (node != TNULL) {
postOrderHelper(node->left);
postOrderHelper(node->right);
std::cout << node->data << " ";
}
}
// Left Rotate
void leftRotate(RYNode* x) {
RYNode* y = x->right;
x->right = y->left;
if (y->left != TNULL) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
this->root = y;
}
else if (x == x->parent->left) {
x->parent->left = y;
}
else {
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
// Right Rotate
void rightRotate(RYNode* y) {
RYNode* x = y->left;
y->left = x->right;
if (x->right != TNULL) {
x->right->parent = y;
}
x->parent = y->parent;
if (y->parent == nullptr) {
this->root = x;
}
else if (y == y->parent->right) {
y->parent->right = x;
}
else {
y->parent->left = x;
}
x->right = y;
y->parent = x;
}
// Balance the tree after insertion
void fixInsert(RYNode* k) {
RYNode* u;
while (k->parent != nullptr && k->parent->color == Color::RED) {
if (k->parent == k->parent->parent->right) {
u = k->parent->parent->left; // Uncle
if (u->color == Color::RED) {
u->color = Color::YELLOW;
k->parent->color = Color::YELLOW;
k->parent->parent->color = Color::RED;
k = k->parent->parent;
}
else {
if (k->parent == k->parent->parent->left && k == k->parent->left) {
Color temp = k->parent->parent->color;
k->parent->parent->color = k->parent->color;
k->parent->color = temp;
rightRotate(k->parent->parent);
}
else if (k->parent == k->parent->parent->right && k == k->parent->right)
{
Color temp = k->parent->parent->color;
k->parent->parent->color = k->parent->color;
k->parent->color = temp;
leftRotate(k->parent->parent);
}
else if (k->parent == k->parent->parent->left && k == k->parent->right)
{
Color temp = k->parent->parent->color;
k->parent->parent->color = k->color;
k->color = temp;
leftRotate(k->parent);
rightRotate(k->parent);
}
else
{
Color temp = k->parent->parent->color;
k->parent->parent->color = k->color;
k->color = temp;
rightRotate(k->parent);
leftRotate(k->parent);
}
}
}
else {
u = k->parent->parent->right; // Uncle
if (u->color == Color::RED) {
u->color = Color::YELLOW;
k->parent->color = Color::YELLOW;
k->parent->parent->color = Color::RED;
k = k->parent->parent; // Move to the grandparent
}
else {
if (k == k->parent->right) {
k = k->parent;
leftRotate(k);
}
k->parent->color = Color::YELLOW;
k->parent->parent->color = Color::RED;
rightRotate(k->parent->parent);
}
}
if (k == root) {
break;
}
}
root->color = Color::YELLOW;
}
void rbTransplant(RYNode* u, RYNode* v) {
if (u->parent == nullptr) {
root = v;
}
else if (u == u->parent->left) {
u->parent->left = v;
}
else {
u->parent->right = v;
}
v->parent = u->parent;
}
// Delete a node from the tree
void fixDelete(RYNode* x) {
RYNode* s;
while (x != root && x->color == Color::YELLOW) {
if (x == x->parent->left) {
s = x->parent->right;
if (s->color == Color::RED) {
s->color = Color::YELLOW;
x->parent->color = Color::RED;
leftRotate(x->parent);
s = x->parent->right;
}
if (s->left->color == Color::YELLOW && s->right->color == Color::YELLOW) {
s->color = Color::RED;
x = x->parent;
}
else {
if (s->right->color == Color::YELLOW) {
s->left->color = Color::YELLOW;
s->color = Color::RED;
rightRotate(s);
s = x->parent->right;
}
s->color = x->parent->color;
x->parent->color = Color::YELLOW;
s->right->color = Color::YELLOW;
leftRotate(x->parent);
x = root;
}
}
else {
s = x->parent->left;
if (s->color == Color::RED) {
s->color = Color::YELLOW;
x->parent->color = Color::RED;
rightRotate(x->parent);
s = x->parent->left;
}
if (s->right->color == Color::YELLOW && s->left->color == Color::YELLOW) {
s->color = Color::RED;
x = x->parent;
}
else {
if (s->left->color == Color::YELLOW) {
s->right->color = Color::YELLOW;
s->color = Color::RED;
leftRotate(s);
s = x->parent->left;
}
s->color = x->parent->color;
x->parent->color = Color::YELLOW;
s->left->color = Color::YELLOW;
rightRotate(x->parent);
x = root;
}
}
}
x->color = Color::YELLOW;
}
RYNode* searchTreeHelper(RYNode* node, int key) {
if (node == TNULL || key == node->data) {
return node;
}
if (key < node->data) {
return searchTreeHelper(node->left, key);
}
return searchTreeHelper(node->right, key);
}
// Balance the tree after a node deletion
void deleteNodeHelper(RYNode* node, int key) {
RYNode* z = TNULL;
RYNode* x;
RYNode* y;
while (node != TNULL) {
if (node->data == key) {
z = node;
}
if (node->data <= key) {
node = node->right;
}
else {
node = node->left;
}
}
if (z == TNULL) {
std::cout << "Couldn't find key in the tree" << std::endl;
return;
}
y = z;
Color yOriginalColor = y->color;
if (z->left == TNULL) {
x = z->right;
rbTransplant(z, z->right);
}
else if (z->right == TNULL) {
x = z->left;
rbTransplant(z, z->left);
}
else {
y = minimum(z->right);
yOriginalColor = y->color;
x = y->right;
if (y->parent == z) {
x->parent = y;
}
else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
}
rbTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
}
if (yOriginalColor == Color::YELLOW) {
fixDelete(x);
}
}
// Find the minimum node in the tree
RYNode* minimum(RYNode* node) {
while (node->left != TNULL) {
node = node->left;
}
return node;
}
// Find the maximum node in the tree
RYNode* maximum(RYNode* node) {
while (node->right != TNULL) {
node = node->right;
}
return node;
}
// Find the successor of a given node
RYNode* successor(RYNode* x) {
if (x->right != TNULL) {
return minimum(x->right);
}
RYNode* y = x->parent;
while (y != TNULL && x == y->right) {
x = y;
y = y->parent;
}
return y;
}
// Find the predecessor of a given node
RYNode* predecessor(RYNode* x) {
if (x->left != TNULL) {
return maximum(x->left);
}
RYNode* y = x->parent;
while (y != TNULL && x == y->left) {
x = y;
y = y->parent;
}
return y;
}
public:
redYellowTree() :root(NULL), TNULL(new RYNode()) {
int Set[] = { 10,10,10,10,10,10 };
int counter = 1;
char key;
int i = 0;
while (1) {
system("cls");
color(10);
cout << "\n\n\t\t\t\t[ RED YELLOW TREE MENU ]" << endl;
cout << "\t\t\t-------------------------------------" << endl;
gotoxy(7, 8);
color(Set[0]);
cout << "1. INSERTION" << endl;
gotoxy(7, 9);
color(Set[1]);
cout << "2. DELETION" << endl;
gotoxy(7, 10);
color(Set[2]);
cout << "3. SEARCHING" << endl;
gotoxy(7, 11);
color(Set[3]);
cout << "4. TRAVERSAL" << endl;
gotoxy(7, 12);
color(Set[4]);
cout << "5. 2D RED YELLOW TREE" << endl;
gotoxy(7, 13);
color(Set[5]);
cout << "0. RETURN TO THE PREVIOUS MENU" << endl;
key = _getch();
if (key == 72 && (counter > 1 && counter <= 6)) {
counter--;
i--;
}
if (key == 80 && (counter >= 1 && counter < 6)) {
counter++;
i++;
}
for (int j = 0; j < 6; j++) {
Set[j] = 10;
}
Set[i] = 12;
if (key == '\r')
{
if (counter == 6)
{
system("cls");
cout << "\n\n\t\t\tEXITING RED YELLOW TREE.." << endl;
this_thread::sleep_for(std::chrono::seconds(1));
break;
}
if (counter == 1)
{
system("cls");
cout << "\n\n\t\t\t\t[ DATA INSERTION ]" << endl;
cout << "\t\t\t--------------------------------" << endl;
int no;
cout << "How many nodes to be inserted?" << endl;
cout << "N = ";
cin >> no;
cout << "Enter the data to be inserted." << endl;
for (int i = 0; i < no; i++)
{
int a;
cout << "Node " << i + 1 << ": ";
cin >> a;
insert(a);
if (!root)
{
cout << "Data couldn't be inserted :(" << endl;
}
cout << endl;
}
cout << "Returning to the menu..." << endl;
this_thread::sleep_for(std::chrono::seconds(2));
}
if (counter == 2)
{
system("cls");
cout << "\n\n\t\t\t\t[ DATA DELETION ]" << endl;
cout << "\t\t\t--------------------------------" << endl;
int a;
cout << "Enter the data to be deleted: ";
cin >> a;
if (searchTree(a))
{
deleteNode(a);
cout << "\n\n";
cout << "Data " << a << " is deleted from binary tree" << endl;
}
else
{
cout << "\n\n";
cout << "Data couldn't be found to be deleted" << endl;
cout << "Please enter valid data" << endl;
}
cout << "Returning to the menu..." << endl;
this_thread::sleep_for(std::chrono::seconds(2));
continue;
}
if (counter == 3)
{
system("cls");
cout << "\n\n\t\t\t\t[ DATA SEARCHING ]" << endl;
cout << "\t\t\t----------------------------------" << endl;
int a;
cout << "Enter the data to be searched: ";
cin >> a;
if (!searchTree(a))
{
cout << "\n\n";
cout << "Data couldn't be found in the binary tree " << endl;
}
else
{
cout << "\n\n";
cout << "Data " << a << " present in the binary tree" << endl;
}
cout << "Returning to the menu..." << endl;
this_thread::sleep_for(std::chrono::seconds(2));
}
if (counter == 4)
{
system("cls");
cout << "\n\n\t\t\t\t[ DATA TRAVERSAL ]" << endl;
cout << "\t\t\t--------------------------------" << endl;
cout << "\n\n";
cout << "In-Order Traversal : ";
printInorder();
cout << endl;
cout << "\n\n";
cout << "Pre-Order Traversal :";
printPreorder();
cout << endl;
cout << "\n\n";
cout << "Post-Order Traversal :";
printPostorder();
cout << endl;
this_thread::sleep_for(std::chrono::seconds(2));
cout << "Returning to the menu..." << endl;
this_thread::sleep_for(std::chrono::seconds(3));
}
if (counter == 5)
{
system("cls");
color(10);
cout << "\n\n\t\t\t\t[ DATA VISUALIZER ]" << endl;
cout << "\t\t\t----------------------------------" << endl;
RYVisualizer a4;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
TTF_Font* font = TTF_OpenFont("arial.ttf", 20); // Use the path to your TTF font file
if (!font) {
std::cerr << "Failed to load font: " << TTF_GetError() << std::endl;
continue;
}
if (!root)
{
cout << "\n\n\t\t\tOOPSIE! KINDLY ENTER THE DATA FIRST :)" << endl;
this_thread::sleep_for(std::chrono::seconds(2));
continue;
}
else
{
cout << "\n\n\t\t\tENTERING GRAPHICS MODE..." << endl;
this_thread::sleep_for(std::chrono::seconds(1));
}
SDL_Window* window = SDL_CreateWindow("RED YELLOW Tree Visualization", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event e;
bool quit = false;
// Define the initial horizontal spacing for the root level
int initialSpacing = SCREEN_WIDTH / 4;
// Set the green line color
SDL_Color lineColor = { 0, 255, 0, 255 };
// Render the entire ternary tree with animations
a4.renderRYT(renderer, font, this->root, SCREEN_WIDTH / 2, 50,0, initialSpacing, lineColor);
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
cout << "\n\n\t\t\tEXITING GRAPHICS MODE.." << endl;
this_thread::sleep_for(std::chrono::seconds(1));
}
}
}
}
// Inorder
void inOrder() {
inOrderHelper(this->root);
}
// Post order
void postOrder() {
postOrderHelper(this->root);
}
// Search the tree for a given key
RYNode* searchTree(int k) {
return searchTreeHelper(this->root, k);
}
// Balance the tree after insertion of a node
void insert(int key) {
RYNode* node = new RYNode(key);
node->parent = nullptr;
node->left = TNULL;
node->right = TNULL;
node->color = Color::RED; // New node is always red
RYNode* y = nullptr;
RYNode* x = this->root;
while (x != TNULL && x!=nullptr) {
y = x;
if (node->data < x->data) {
x = x->left;
}
else {
x = x->right;
}
}
node->parent = y;
if (y == nullptr) {
root = node;
}
else if (node->data < y->data) {
y->left = node;
}
else {
y->right = node;
}
if (node->parent == nullptr) {
node->color = Color::YELLOW;
return;
}
if (node->parent->parent == nullptr) {
return;
}
fixInsert(node);
}
// Delete a node from the tree
void deleteNode(int data) {
deleteNodeHelper(this->root, data);
}
// Preorder
void printPreorder() {
preOrderHelper(this->root);
}
// Inorder
void printInorder() {
inOrderHelper(this->root);
}
// Post order
void printPostorder() {
postOrderHelper(this->root);
}
};