Advertisement
Ali-ElMasry

Untitled

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