Advertisement
Ali-ElMasry

Untitled

Dec 7th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. enum Color {BLACK, RED};
  6. bool colorD;
  7.  
  8. struct Node
  9. {
  10.     int from;
  11.     int to;
  12.     int mx;
  13.     Color color;
  14.     Node *left, *right, *parent;
  15.  
  16.     // Constructor
  17.     Node(int from , int to, Node* l, Node* r, Node* p)
  18.     {
  19.        this->from = from;
  20.        this->to = to;
  21.        this->left = l;
  22.        this->right = r;
  23.        this->parent = p;
  24.        this->color = RED;
  25.        this->mx = to;
  26.     }
  27.     Node()
  28.     {
  29.  
  30.     }
  31. };
  32.  
  33. struct RBT
  34. {
  35.     Node* root;
  36.     Node* NIL;
  37.  
  38.     RBT()
  39.     {
  40.         NIL = new Node(0, 0, 0, 0, 0);
  41.         NIL->color = BLACK;
  42.         root = NIL;
  43.     }
  44.  
  45.     void insert(int from , int to)
  46.     {
  47.         //cout<<value<<endl;
  48.         Node* pt = new Node(from , to, NIL, NIL, NIL);
  49.         if(root == NIL)
  50.         {
  51.             root = pt;
  52.             root->color = BLACK;
  53.             return;
  54.         }
  55.         bool dir;
  56.         Node* loc = root;
  57.         Node* locP;
  58.         while(loc != NIL)
  59.         {
  60.             locP = loc;
  61.  
  62.             if(pt->from < loc->from)
  63.             {
  64.                 loc = locP->left;
  65.                 dir = false;
  66.             }
  67.             else
  68.             {
  69.                 loc = locP->right;
  70.                 dir = true;
  71.             }
  72.         }
  73.         pt->parent = locP;
  74.         if(dir)
  75.             locP->right = pt;
  76.         else locP->left = pt;
  77.  
  78.         insertFix(pt);
  79.     }
  80.  
  81.     void insertFix(Node* x)
  82.     {
  83.         Node* original = x;
  84.  
  85.         while(x != root && x->parent->color == RED)
  86.         {
  87.             if(x->parent->parent->left == x->parent)
  88.             {
  89.                 Node* y = x->parent->parent->right;
  90.                 if(y != NIL && y->color == RED)
  91.                 {
  92.                     x->parent->color = BLACK;
  93.                     y->color = BLACK;
  94.                     x->parent->parent->color = RED;
  95.                     x = x->parent->parent;
  96.                 }
  97.                 else
  98.                 {
  99.                     if(x->parent->right == x)
  100.                     {
  101.                         x = x->parent;
  102.                         leftRotate(x);
  103.                     }
  104.                     x->parent->color = BLACK;
  105.                     x->parent->parent->color = RED;
  106.                     rightRotate(x->parent->parent);
  107.                 }
  108.             }
  109.             else
  110.             {
  111.                 Node* y = x->parent->parent->left;
  112.                 if(y != NIL && y->color == RED)
  113.                 {
  114.                     x->parent->color = BLACK;
  115.                     y->color = BLACK;
  116.                     x->parent->parent->color = RED;
  117.                     x = x->parent->parent;
  118.                 }
  119.                 else
  120.                 {
  121.                     if(x->parent->left == x)
  122.                     {
  123.                         x = x->parent;
  124.                         rightRotate(x);
  125.                     }
  126.                     x->parent->color = BLACK;
  127.                     x->parent->parent->color = RED;
  128.                     leftRotate(x->parent->parent);
  129.                 }
  130.             }
  131.         }
  132.         if(x == root)
  133.             x->color = BLACK;
  134.  
  135.         fixMax(original);
  136.     }
  137.  
  138.     void leftRotate(Node* x)
  139.     {
  140.         Node* y = x->right;
  141.         int dir = -1;
  142.         if(x != root && x->parent->left == x)
  143.             dir = 0;
  144.         else if(x != root)
  145.             dir = 1;
  146.  
  147.         x->right = y->left;
  148.         if(y->left != NIL)
  149.             y->left->parent = x;
  150.  
  151.         if(dir != -1)
  152.         {
  153.             if(dir == 0)
  154.                 x->parent->left = y;
  155.             else x->parent->right = y;
  156.  
  157.             y->parent = x->parent;
  158.         }
  159.         if(x == root)
  160.         {
  161.             y->parent = NIL;
  162.             root = y;
  163.         }
  164.  
  165.         y->left = x;
  166.         x->parent = y;
  167.  
  168.         fixMax(x);
  169.     }
  170.  
  171.     void rightRotate(Node* y)
  172.     {
  173.         Node* x = y->left;
  174.         int dir = -1;
  175.         if(y != root && y->parent->left == y)
  176.             dir = 0;
  177.         else if(y != root)
  178.             dir = 1;
  179.  
  180.         y->left = x->right;
  181.         if(x->right != NIL)
  182.             x->right->parent = y;
  183.  
  184.         if(dir != -1)
  185.         {
  186.             if(dir == 0)
  187.                 y->parent->left = x;
  188.             else y->parent->right = x;
  189.  
  190.             x->parent = y->parent;
  191.         }
  192.         if(y == root)
  193.         {
  194.             x->parent = NIL;
  195.             root = x;
  196.         }
  197.  
  198.         x->right = y;
  199.         y->parent = x;
  200.  
  201.         fixMax(y);
  202.     }
  203.  
  204.     void fixMax(Node* x)
  205.     {
  206.         //cout<<x->from<<endl;
  207.         x->mx = x->to;
  208.  
  209.         if(x->left != NIL)
  210.             x->mx = max(x->mx , x->left->mx);
  211.         if(x->right != NIL)
  212.             x->mx = max(x->mx , x->right->mx);
  213.  
  214.         if(x != root)
  215.             fixMax(x->parent);
  216.     }
  217.  
  218.     bool overlaps(Node* x , Node* y)
  219.     {
  220.         if((y->from >= x->from && y->from <= x->to)||
  221.            (y->to <= x->to && y->to >= x->from)||
  222.            (y->from <= x->from && y->to >= x->to))
  223.             return true;
  224.         return false;
  225.     }
  226.  
  227.     void findOverlap(int from , int to)
  228.     {
  229.         Node* temp = this->root;
  230.         Node* key = new Node(from , to, NIL, NIL, NIL);
  231.  
  232.         while(temp != NIL)
  233.         {
  234.             if(overlaps(key , temp))
  235.             {
  236.                 cout<<"\nOverlaps with [" << temp->from << " , " << temp->to << "]\n";
  237.                 return;
  238.             }
  239.             if(temp->left !=NIL && key->from <= temp->left->mx)
  240.                 temp = temp->left;
  241.             else
  242.                 temp = temp->right;
  243.         }
  244.         cout<<"\nNo Overlaps\n";
  245.     }
  246.  
  247.     void print()
  248.     {
  249.         cout<<"\nInorder Traversal :\n";
  250.         inorder(root);
  251.         cout<<"\nPreorder Traversal :\n";
  252.         preorder(root);
  253.     }
  254.     void inorder(Node* root)
  255.     {
  256.         if(root == NIL)
  257.             return;
  258.         inorder(root->left);
  259.         cout<<root->from<<" "<<root->to<<endl;
  260.         inorder(root->right);
  261.     }
  262.     void preorder(Node* root)
  263.     {
  264.         if(root == NIL)
  265.             return;
  266.         cout<<root->from<<" "<<root->to<<endl;
  267.         preorder(root->left);
  268.         preorder(root->right);
  269.     }
  270.  
  271.     void deleteRBT(Node* z)
  272.     {
  273.         if (z == NIL)
  274.             return;
  275.         Node* y = z;
  276.         Node* x = NIL;
  277.         Color yColor = y->color;
  278.         if (z->left == NIL) {
  279.             x = z->right;
  280.             tranplant(z, z->right);
  281.         } else if (z->right == NIL) {
  282.             x = z->left;
  283.             tranplant(z, z->left);
  284.         } else {
  285.             y = treeMin(z->right);
  286.             yColor  = y->color;
  287.             x = y->right;
  288.             if (y->parent == z)
  289.                 x->parent = y;
  290.             else {
  291.                 tranplant(y, y->right);
  292.                 y->right = z->right;
  293.                 y->right->parent = y;
  294.             }
  295.             tranplant(z, y);
  296.             y->left = z->left;
  297.             y->left->parent = y;
  298.             y->color = z->color;
  299.         }
  300.         delete z;
  301.         if (yColor == BLACK)
  302.             deleteFix(x);
  303.     }
  304.  
  305.     void deleteRBT(int from, int to) {
  306.         Node* x = root;
  307.         while (x != NIL) {
  308.             if (x->from == from && x->to == to) {
  309.                 deleteRBT(x);
  310.                 return;
  311.             }
  312.             if (x->from < from)
  313.                 x = x->right;
  314.             else
  315.                 x = x->left;
  316.         }
  317.     }
  318.  
  319.     void deleteFix(Node* x)
  320.     {
  321.         while (x != root && x->color != BLACK) {
  322.             if (x == x->parent->left) {
  323.                 Node* w = x->parent->right;
  324.                 if (w->color == RED) { // case 1
  325.                     w->color = BLACK;
  326.                     x->parent->color = RED;
  327.                     leftRotate(x->parent);
  328.                     w = x->parent->right;
  329.                 }
  330.                 if (w->left->color == BLACK &&
  331.                         w->right->color == BLACK) { // case 2
  332.                     w->color = RED;
  333.                     x = x->parent;
  334.                 } else {
  335.                     if (w->right->color == BLACK) { // case 3
  336.                         w->left->color = BLACK;
  337.                         w->color = RED;
  338.                         rightRotate(w);
  339.                         w = w->parent->right;
  340.                     }
  341.                     // case 4
  342.                     w->color = x->parent->color;
  343.                     x->parent->color = BLACK;
  344.                     w->right->color = BLACK;
  345.                     leftRotate(x->parent);
  346.                     x = root;
  347.                 }
  348.             } else {
  349.                 Node* w = x->parent->left;
  350.                 if (w->color == RED) { // case 1
  351.                     w->color = BLACK;
  352.                     x->parent->color = RED;
  353.                     rightRotate(x->parent);
  354.                     w = x->parent->left;
  355.                 }
  356.                 if (w->left->color == BLACK &&
  357.                         w->right->color == BLACK) { // case 2
  358.                     w->color = RED;
  359.                     x = x->parent;
  360.                 } else {
  361.                     if (w->left->color == BLACK) { // case 3
  362.                         w->right->color = BLACK;
  363.                         w->color = RED;
  364.                         leftRotate(w);
  365.                         w = w->parent->left;
  366.                     }
  367.                     // case 4
  368.                     w->color = x->parent->color;
  369.                     x->parent->color = BLACK;
  370.                     w->left->color = BLACK;
  371.                     rightRotate(x->parent);
  372.                     x = root;
  373.                 }
  374.             }
  375.         }
  376.         x->color = BLACK;
  377.     }
  378.  
  379.     Node* treeMin(Node* node)
  380.     {
  381.         while (node->left != NIL)
  382.             node = node->left;
  383.         return node;
  384.     }
  385.  
  386.     void tranplant(Node* u, Node* v)
  387.     {
  388.         if (u->parent == NIL)
  389.             root = v;
  390.         else if (u == u->parent->left)
  391.             u->parent->left = v;
  392.         else
  393.             u->parent->right = v;
  394.         v->parent = u->parent;
  395.     }
  396. };
  397.  
  398. int main()
  399. {
  400.     RBT x;
  401.     int from , to;
  402.  
  403.     x.insert(20, 30);
  404.     x.insert(30, 30);
  405.     x.insert(35, 30);
  406.     x.insert(25, 30);
  407.     x.insert(10, 30);
  408.  
  409.     x.print();
  410.     x.deleteRBT(10, 30);
  411.     x.deleteRBT(20, 30);
  412.     x.print();
  413.  
  414.     // cout<<"Number of Nodes :\n";
  415.     // int n = 0;
  416.     // // cin>>n;
  417.     // cout<<"Intervals :\n";
  418. //
  419.     // for(int i=0; i<n; i++)
  420.     // {
  421.         // //cin>>from>>to;
  422.         // x.insert(from , to);
  423.     // }
  424. //
  425.     // x.print();
  426. //
  427.     // cout<<"Search for Interval :\n";
  428.     // // cin>>from>>to;
  429.     // //x.findOverlap(from , to);
  430. //
  431.     // cout<<"Delete Interval :\n";
  432. //
  433.  
  434.     return 0;
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement