insertfix(
node*);
25 voidleftrotate(
node*);
26 voidrightrotate(
node*);
31 voiddisplay(
node*);
37cout <<
"\nEnter key of the node to be inserted: ";
70voidRBtree::insertfix(
node*t)
78 while(t->parent != NULL && t->parent->color ==
'r')
80 node*
g= t->parent->parent;
81 if(
g->left == t->parent)
83 if(
g->right != NULL)
88t->parent->color =
'b';
96 if(t->parent->right == t)
101t->parent->color =
'b';
108 if(
g->left != NULL)
111 if(u->color ==
'r')
113t->parent->color =
'b';
121 if(t->parent->left == t)
126t->parent->color =
'b';
139cout <<
"\nEmpty Tree.";
143cout <<
"\nEnter the key of the node to be deleted: ";
150 while(p != NULL && found == 0)
164cout <<
"\nElement Not Found.";
169cout <<
"\nDeleted Element: "<< p->key;
170cout <<
"\nColour: ";
171 if(p->color ==
'b')
176 if(p->parent != NULL)
177cout <<
"\nParent: "<< p->parent->key;
179cout <<
"\nThere is no parent of the node. ";
180 if(p->right != NULL)
181cout <<
"\nRight Child: "<< p->right->key;
183cout <<
"\nThere is no right child of the node. ";
185cout <<
"\nLeft Child: "<< p->left->key;
187cout <<
"\nThere is no left child of the node. ";
188cout <<
"\nNode Deleted.";
189 if(p->left == NULL || p->right == NULL)
197 if(y->right != NULL)
203q->parent = y->parent;
204 if(y->parent == NULL)
208 if(y == y->parent->left)
211y->parent->right = q;
218 if(y->color ==
'b')
223voidRBtree::delfix(
node*p)
226 while(p != root && p->color ==
'b')
228 if(p->parent->left == p)
230s = p->parent->right;
231 if(s->color ==
'r')
234p->parent->color =
'r';
235leftrotate(p->parent);
236s = p->parent->right;
238 if(s->right->color ==
'b'&&s->left->color ==
'b')
245 if(s->right->color ==
'b')
247s->left->color =
'b';
250s = p->parent->right;
252s->color = p->parent->color;
253p->parent->color =
'b';
254s->right->color =
'b';
255leftrotate(p->parent);
262 if(s->color ==
'r')
265p->parent->color =
'r';
266rightrotate(p->parent);
269 if(s->left->color ==
'b'&&s->right->color ==
'b')
276 if(s->left->color ==
'b')
278s->right->color =
'b';
283s->color = p->parent->color;
284p->parent->color =
'b';
285s->left->color =
'b';
286rightrotate(p->parent);
295voidRBtree::leftrotate(
node*p)
297 if(p->right == NULL)
309 if(p->parent != NULL)
310y->parent = p->parent;
311 if(p->parent == NULL)
315 if(p == p->parent->left)
318p->parent->right = y;
324voidRBtree::rightrotate(
node*p)
331 if(y->right != NULL)
334y->right->parent = p;
338 if(p->parent != NULL)
339y->parent = p->parent;
340 if(p->parent == NULL)
344 if(p == p->parent->left)
347p->parent->right = y;
360 while(y->right != NULL)
366 while(y->left != NULL)
376voidRBtree::display(
node*p)
380cout <<
"\nEmpty Tree.";
385cout <<
"\n\t NODE: ";
386cout <<
"\n Key: "<< p->key;
387cout <<
"\n Colour: ";
388 if(p->color ==
'b')
392 if(p->parent != NULL)
393cout <<
"\n Parent: "<< p->parent->key;
395cout <<
"\n There is no parent of the node. ";
396 if(p->right != NULL)
397cout <<
"\n Right Child: "<< p->right->key;
399cout <<
"\n There is no right child of the node. ";
401cout <<
"\n Left Child: "<< p->left->key;
403cout <<
"\n There is no left child of the node. ";
407cout <<
"\n\nLeft:\n";
414cout <<
"\n\nRight:\n";
425cout <<
"\nEmpty Tree\n";
429cout <<
"\n Enter key of the node to be searched: ";
433 while(p != NULL && found == 0)
446cout <<
"\nElement Not Found.";
449cout <<
"\n\t FOUND NODE: ";
450cout <<
"\n Key: "<< p->key;
451cout <<
"\n Colour: ";
452 if(p->color ==
'b')
456 if(p->parent != NULL)
457cout <<
"\n Parent: "<< p->parent->key;
459cout <<
"\n There is no parent of the node. ";
460 if(p->right != NULL)
461cout <<
"\n Right Child: "<< p->right->key;
463cout <<
"\n There is no right child of the node. ";
465cout <<
"\n Left Child: "<< p->left->key;
467cout <<
"\n There is no left child of the node. ";
478cout <<
"\n\t RED BLACK TREE ";
479cout <<
"\n 1. Insert in the tree ";
480cout <<
"\n 2. Delete a node from the tree";
481cout <<
"\n 3. Search for an element in the tree";
482cout <<
"\n 4. Display the tree ";
483cout <<
"\n 5. Exit ";
484cout <<
"\nEnter Your Choice: ";
488 case1: obj.insert();
489cout <<
"\nNode Inserted.\n";
493 case3: obj.search();
499 default: cout <<
"\nEnter a Valid Choice.";
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
double g(double x)
Another test function.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4