24 void insertfix(
node *);
25 void leftrotate(
node *);
26 void rightrotate(
node *);
37 cout <<
"\nEnter key of the node to be inserted: ";
70void RBtree::insertfix(
node *t)
78 while (t->parent != NULL && t->parent->color ==
'r')
80 node *
g = t->parent->parent;
81 if (
g->left == t->parent)
88 t->parent->color =
'b';
96 if (t->parent->right == t)
101 t->parent->color =
'b';
113 t->parent->color =
'b';
121 if (t->parent->left == t)
126 t->parent->color =
'b';
139 cout <<
"\nEmpty Tree.";
143 cout <<
"\nEnter the key of the node to be deleted: ";
150 while (p != NULL && found == 0)
164 cout <<
"\nElement Not Found.";
169 cout <<
"\nDeleted Element: " << p->key;
170 cout <<
"\nColour: ";
176 if (p->parent != NULL)
177 cout <<
"\nParent: " << p->parent->key;
179 cout <<
"\nThere is no parent of the node. ";
180 if (p->right != NULL)
181 cout <<
"\nRight Child: " << p->right->key;
183 cout <<
"\nThere is no right child of the node. ";
185 cout <<
"\nLeft Child: " << p->left->key;
187 cout <<
"\nThere is no left child of the node. ";
188 cout <<
"\nNode Deleted.";
189 if (p->left == NULL || p->right == NULL)
197 if (y->right != NULL)
203 q->parent = y->parent;
204 if (y->parent == NULL)
208 if (y == y->parent->left)
211 y->parent->right = q;
223void RBtree::delfix(
node *p)
226 while (p != root && p->color ==
'b')
228 if (p->parent->left == p)
230 s = p->parent->right;
234 p->parent->color =
'r';
235 leftrotate(p->parent);
236 s = p->parent->right;
238 if (s->right->color ==
'b'&&s->left->color ==
'b')
245 if (s->right->color ==
'b')
247 s->left->color =
'b';
250 s = p->parent->right;
252 s->color = p->parent->color;
253 p->parent->color =
'b';
254 s->right->color =
'b';
255 leftrotate(p->parent);
265 p->parent->color =
'r';
266 rightrotate(p->parent);
269 if (s->left->color ==
'b'&&s->right->color ==
'b')
276 if (s->left->color ==
'b')
278 s->right->color =
'b';
283 s->color = p->parent->color;
284 p->parent->color =
'b';
285 s->left->color =
'b';
286 rightrotate(p->parent);
295void RBtree::leftrotate(
node *p)
297 if (p->right == NULL)
309 if (p->parent != NULL)
310 y->parent = p->parent;
311 if (p->parent == NULL)
315 if (p == p->parent->left)
318 p->parent->right = y;
324void RBtree::rightrotate(
node *p)
331 if (y->right != NULL)
334 y->right->parent = p;
338 if (p->parent != NULL)
339 y->parent = p->parent;
340 if (p->parent == NULL)
344 if (p == p->parent->left)
347 p->parent->right = y;
360 while (y->right != NULL)
366 while (y->left != NULL)
376void RBtree::display(
node *p)
380 cout <<
"\nEmpty Tree.";
385 cout <<
"\n\t NODE: ";
386 cout <<
"\n Key: " << p->key;
387 cout <<
"\n Colour: ";
392 if (p->parent != NULL)
393 cout <<
"\n Parent: " << p->parent->key;
395 cout <<
"\n There is no parent of the node. ";
396 if (p->right != NULL)
397 cout <<
"\n Right Child: " << p->right->key;
399 cout <<
"\n There is no right child of the node. ";
401 cout <<
"\n Left Child: " << p->left->key;
403 cout <<
"\n There is no left child of the node. ";
407 cout <<
"\n\nLeft:\n";
414 cout <<
"\n\nRight:\n";
425 cout <<
"\nEmpty Tree\n";
429 cout <<
"\n Enter key of the node to be searched: ";
433 while (p != NULL && found == 0)
446 cout <<
"\nElement Not Found.";
449 cout <<
"\n\t FOUND NODE: ";
450 cout <<
"\n Key: " << p->key;
451 cout <<
"\n Colour: ";
456 if (p->parent != NULL)
457 cout <<
"\n Parent: " << p->parent->key;
459 cout <<
"\n There is no parent of the node. ";
460 if (p->right != NULL)
461 cout <<
"\n Right Child: " << p->right->key;
463 cout <<
"\n There is no right child of the node. ";
465 cout <<
"\n Left Child: " << p->left->key;
467 cout <<
"\n There is no left child of the node. ";
478 cout <<
"\n\t RED BLACK TREE ";
479 cout <<
"\n 1. Insert in the tree ";
480 cout <<
"\n 2. Delete a node from the tree";
481 cout <<
"\n 3. Search for an element in the tree";
482 cout <<
"\n 4. Display the tree ";
483 cout <<
"\n 5. Exit ";
484 cout <<
"\nEnter Your Choice: ";
488 case 1: obj.insert();
489 cout <<
"\nNode Inserted.\n";
493 case 3: 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.