Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <utility>
  5. #include <list>
  6. #include <set>
  7. #include <random>
  8.  
  9. using std::cin;
  10. using std::cout;
  11. using std::max;
  12. using std::reverse;
  13. using std::vector;
  14. using std::pair;
  15.  
  16. template<class T>
  17. struct RBTNode {
  18.     RBTNode(T v, bool c = 0, RBTNode<T> *P = nullptr) {
  19.         value = v;
  20.         color = c;
  21.         Left = Right = nullptr;
  22.         Parent = P;
  23.     }
  24.  
  25.     RBTNode<T> *Left;
  26.     RBTNode<T> *Right;
  27.     RBTNode<T> *Parent;
  28.     T value;
  29.     bool color;
  30. };
  31.  
  32. template<class T>
  33. class Iterator {
  34. public:
  35.     explicit Iterator(RBTNode<T> *tree) {
  36.         current = tree;
  37.     }
  38.  
  39.     Iterator<T> &operator=(const Iterator &other) {
  40.         if (this != &other) {
  41.             current = other.current;
  42.         }
  43.         return *this;
  44.     }
  45.  
  46.     Iterator(const Iterator &other) {
  47.         current = other.current;
  48.     }
  49.  
  50.     bool operator!=(const Iterator &other) {
  51.         return current != other.current;
  52.     }
  53.  
  54.     bool operator==(const Iterator &other) {
  55.         return current == other.current;
  56.     }
  57.  
  58.     Iterator<T> &operator++() {
  59.         if (current->Right) {
  60.             current = current->Right;
  61.             while (current->Left)
  62.                 current = current->Left;
  63.         } else {
  64.             while (current->Parent != nullptr && current->Parent->Right == current)
  65.                 current = current->Parent;
  66.             current = current->Parent;
  67.         }
  68.         return *this;
  69.     }
  70.  
  71.     Iterator<T> &operator--() {
  72.         if (current->Left) {
  73.             current = current->Left;
  74.             while (current->Right)
  75.                 current = current->Right;
  76.         } else {
  77.             while (current->Parent->Left == current)
  78.                 current = current->Parent;
  79.             current = current->Parent;
  80.         }
  81.         return *this;
  82.     }
  83.  
  84.     Iterator<T> operator++(int such_a_rule) {
  85.         Iterator<T> t = *this;
  86.         if (current->Right) {
  87.             current = current->Right;
  88.             while (current->Left)
  89.                 current = current->Left;
  90.         } else {
  91.             while (current->Parent != nullptr && current->Parent->Right == current)
  92.                 current = current->Parent;
  93.             if (current->Parent == nullptr) return Iterator(nullptr);
  94.             current = current->Parent;
  95.         }
  96.         return t;
  97.     }
  98.  
  99.     Iterator<T> operator--(int such_a_rule) {
  100.         Iterator<T> t = *this;
  101.         if (current->Left) {
  102.             current = current->Left;
  103.             while (current->Right)
  104.                 current = current->Right;
  105.         } else {
  106.             while (current->Parent != nullptr && current->Parent->Left == current)
  107.                 current = current->Parent;
  108.             if (current->Parent == nullptr) return Iterator(nullptr);
  109.             current = current->Parent;
  110.         }
  111.         return t;
  112.     }
  113.  
  114.     T operator*() {
  115.         return current->value;
  116.     }
  117.  
  118.     RBTNode<T> *operator->() {
  119.         return current;
  120.     }
  121.  
  122. private:
  123.     RBTNode<T> *current = nullptr;
  124. };
  125.  
  126. template<class T>
  127. class RBT {
  128.     int size = 0;
  129. public:
  130.     typedef Iterator<T> iterator;
  131.  
  132.     Iterator<T> begin();
  133.  
  134.     Iterator<T> end();
  135.  
  136.     RBTNode<T> *Head;
  137.  
  138.     void insert(T v);
  139.  
  140.     RBTNode<T> *search(T v);
  141.  
  142.     void deleteVal(T v);
  143.  
  144.     bool Empty();
  145.  
  146. private:
  147.     void RotateRight(RBTNode<T> *);
  148.  
  149.     void deleteNode(RBTNode<T> *v);
  150.  
  151.     void RotateLeft(RBTNode<T> *root);
  152.  
  153.     void ReColor(RBTNode<T> *node);
  154.  
  155.     void ReBalance(RBTNode<T> *root);
  156.  
  157.     void Insert(RBTNode<T> *root, T v);
  158.  
  159.     RBTNode<T> *MinReplacedVal(RBTNode<T> *x);
  160.  
  161.     RBTNode<T> *sibling(RBTNode<T> *node);
  162.  
  163.     void fixDoubleBlack(RBTNode<T> *x);
  164. };
  165.  
  166. template<class T>
  167. Iterator<T> RBT<T>::begin() {
  168.     RBTNode<T> *t = Head;
  169.     while (t->Left != nullptr) t = t->Left;
  170.     return Iterator(t);
  171. }
  172.  
  173. template<class T>
  174. Iterator<T> RBT<T>::end() {
  175.     RBTNode<T> *t = Head;
  176.     while (t->Right != nullptr) t = t->Right;
  177.     return Iterator(t);
  178. }
  179.  
  180. template<class T>
  181. void RBT<T>::RotateRight(RBTNode<T> *root) {
  182.     if (root->Parent != nullptr) {
  183.         if (root->Parent->Left != nullptr && root->Parent->Left == root)
  184.             root->Parent->Left = root->Left;
  185.         else
  186.             root->Parent->Right = root->Left;
  187.     }
  188.  
  189.     root->Left->Parent = root->Parent;
  190.     root->Parent = root->Left;
  191.     root->Left = root->Parent->Right;
  192.     if (root->Parent->Right != nullptr) root->Parent->Right->Parent = root;
  193.     if (root->Parent->Parent == nullptr) Head = root->Parent;
  194.     root->Parent->Right = root;
  195. }
  196.  
  197. template<class T>
  198. void RBT<T>::RotateLeft(RBTNode<T> *root) {
  199.     if (root->Parent != nullptr) {
  200.         if (root->Parent->Left != nullptr && root->Parent->Left == root)
  201.             root->Parent->Left = root->Right;
  202.         else
  203.             root->Parent->Right = root->Right;
  204.     }
  205.     root->Right->Parent = root->Parent;
  206.     root->Parent = root->Right;
  207.     root->Right = root->Parent->Left;
  208.     if (root->Parent->Left != nullptr) root->Parent->Left->Parent = root;
  209.     if (root->Parent->Parent == nullptr) Head = root->Parent;
  210.     root->Parent->Left = root;
  211. }
  212.  
  213. template<class T>
  214. void RBT<T>::ReColor(RBTNode<T> *node) {
  215.     if (node->Parent->Parent->Right != nullptr) node->Parent->Parent->Right->color = 0;
  216.     if (node->Parent->Parent->Left != nullptr) node->Parent->Parent->Left->color = 0;
  217.     if (node->Parent->Parent->Parent == nullptr) node->Parent->Parent->color = 0;
  218.     else
  219.         node->Parent->Parent->color = 1;
  220. }
  221.  
  222. template<class T>
  223. void RBT<T>::ReBalance(RBTNode<T> *root) {
  224.     if (root->Parent == nullptr || root->Parent->color == 0) return;
  225.  
  226.     if (root->Parent->Left == root && root->Parent->Parent->Left == root->Parent) {
  227.         if (root->Parent->Parent->Right == nullptr || root->Parent->Parent->Right->color == 0) {
  228.             RotateRight(root->Parent->Parent);
  229.             root->Parent->color = 0;
  230.             root->Parent->Right->color = 1;
  231.         } else {
  232.             ReColor(root);
  233.             ReBalance(root->Parent->Parent);
  234.         }
  235.     } else if (root->Parent->Right == root && root->Parent->Parent->Left == root->Parent) {
  236.         if (root->Parent->Parent->Right == nullptr || root->Parent->Parent->Right->color == 0) {
  237.             RotateLeft(root->Parent);
  238.             RotateRight(root->Parent);
  239.             root->color = 0;
  240.             root->Right->color = 1;
  241.         } else {
  242.             ReColor(root);
  243.             ReBalance(root->Parent->Parent);
  244.         }
  245.     } else if (root->Parent->Right == root && root->Parent->Parent->Right == root->Parent) {
  246.         if (root->Parent->Parent->Left == nullptr || root->Parent->Parent->Left->color == 0) {
  247.             RotateLeft(root->Parent->Parent);
  248.             root->Parent->color = 0;
  249.             root->Parent->Left->color = 1;
  250.         } else {
  251.             ReColor(root);
  252.             ReBalance(root->Parent->Parent);
  253.         }
  254.     } else if (root->Parent->Left == root && root->Parent->Parent->Right == root->Parent) {
  255.         if (root->Parent->Parent->Left == nullptr ||
  256.             root->Parent->Parent->Left->color == 0) {
  257.             RotateRight(root->Parent);
  258.             RotateLeft(root->Parent);
  259.             root->color = 0;
  260.             root->Left->color = 1;
  261.         } else {
  262.             ReColor(root);
  263.             ReBalance(root->Parent->Parent);
  264.         }
  265.     }
  266. }
  267.  
  268. template<class T>
  269. void RBT<T>::Insert(RBTNode<T> *root, T v) {
  270.     RBTNode<T> *P = nullptr;
  271.     while (root != nullptr) {
  272.         P = root;
  273.         if (root->value < v)
  274.             root = root->Right;
  275.         else
  276.             root = root->Left;
  277.     }
  278.  
  279.     root = new RBTNode<T>(v, 1, P);
  280.     if (P->value >= root->value) P->Left = root;
  281.     else
  282.         P->Right = root;
  283.     ReBalance(root);
  284. }
  285.  
  286. template<class T>
  287. RBTNode<T> *RBT<T>::MinReplacedVal(RBTNode<T> *x) {
  288.  
  289.     if (x->Left != nullptr and x->Right != nullptr) {
  290.         RBTNode<T> *temp_variable = x->Right;
  291.         while (temp_variable->Left != nullptr)
  292.             temp_variable = temp_variable->Left;
  293.         return temp_variable;
  294.     }
  295.  
  296.     if (x->Left == nullptr and x->Right == nullptr)
  297.         return nullptr;
  298.  
  299.     if (x->Left != nullptr)
  300.         return x->Left;
  301.     else
  302.         return x->Right;
  303. }
  304.  
  305. template<class T>
  306. RBTNode<T> *RBT<T>::sibling(RBTNode<T> *node) {
  307.     if (node->Parent == nullptr)
  308.         return nullptr;
  309.  
  310.     if (node == node->Parent->Left)
  311.         return node->Parent->Right;
  312.  
  313.     return node->Parent->Left;
  314. }
  315.  
  316. template<class T>
  317. void RBT<T>::fixDoubleBlack(RBTNode<T> *x) {
  318.     if (x == Head)
  319.         return;
  320.  
  321.     RBTNode<T> *sib = sibling(x), *parent = x->Parent;
  322.     if (sib == nullptr)
  323.         fixDoubleBlack(parent);
  324.     else {
  325.         if (sib->color == 1) {
  326.             parent->color = 1;
  327.             sib->color = 0;
  328.  
  329.             if (sib->Parent->Left == sib)
  330.                 RotateRight(parent);
  331.             else
  332.                 RotateLeft(parent);
  333.  
  334.             fixDoubleBlack(x);
  335.         } else {
  336.             if ((sib->Left != nullptr && sib->Left->color == 1) ||
  337.                 (sib->Right != nullptr && sib->Right->color == 1)) {
  338.  
  339.                 if (sib->Left != nullptr && sib->Left->color == 1) {
  340.                     if (sib->Parent->Left == sib) {
  341.                         sib->Left->color = sib->color;
  342.                         sib->color = parent->color;
  343.                         RotateRight(parent);
  344.                     } else {
  345.                         sib->Left->color = parent->color;
  346.                         RotateRight(sib);
  347.                         RotateLeft(parent);
  348.                     }
  349.                 } else {
  350.                     if (sib->Parent->Left == sib) {
  351.                         sib->Right->color = parent->color;
  352.                         RotateLeft(sib);
  353.                         RotateRight(parent);
  354.                     } else {
  355.                         sib->Right->color = sib->color;
  356.                         sib->color = parent->color;
  357.                         RotateLeft(parent);
  358.                     }
  359.                 }
  360.                 parent->color = 0;
  361.             } else {
  362.                 sib->color = 1;
  363.                 if (parent->color == 0)
  364.                     fixDoubleBlack(parent);
  365.                 else
  366.                     parent->color = 0;
  367.             }
  368.         }
  369.     }
  370. }
  371.  
  372. template<class T>
  373. void RBT<T>::deleteNode(RBTNode<T> *v) {
  374.     RBTNode<T> *u = MinReplacedVal(v);
  375.  
  376.     bool bb = ((u == nullptr or u->color == 0) and (v->color == 0));
  377.     RBTNode<T> *parent = v->Parent;
  378.  
  379.     if (u == nullptr) {
  380.         if (v == Head) {
  381.             Head = nullptr;
  382.         } else {
  383.             if (bb)
  384.                 fixDoubleBlack(v);
  385.             else {
  386.                 RBTNode<T> *t = sibling(v);
  387.                 if (t != nullptr)
  388.                     t->color = 1;
  389.             }
  390.  
  391.             if (v->Parent->Left == v)
  392.                 parent->Left = nullptr;
  393.             else
  394.                 parent->Right = nullptr;
  395.         }
  396.         delete v;
  397.         return;
  398.     }
  399.  
  400.     if (v->Left == nullptr or v->Right == nullptr) {
  401.         if (v == Head) {
  402.             v->value = u->value;
  403.             v->Left = v->Right = nullptr;
  404.             delete u;
  405.         } else {
  406.             if (v->Parent->Left == v)
  407.                 parent->Left = u;
  408.             else
  409.                 parent->Right = u;
  410.  
  411.             delete v;
  412.             u->Parent = parent;
  413.  
  414.             if (bb)
  415.                 fixDoubleBlack(u);
  416.             else
  417.                 u->color = 0;
  418.         }
  419.         return;
  420.     }
  421.  
  422.     T temp_variable;
  423.     temp_variable = u->value;
  424.     u->value = v->value;
  425.     v->value = temp_variable;
  426.  
  427.     deleteNode(u);
  428. }
  429.  
  430. template<class T>
  431. RBTNode<T> *RBT<T>::search(T v) {
  432.     if (Head == nullptr) return nullptr;
  433.     RBTNode<T> *root = Head;
  434.     while (root != nullptr) {
  435.         if (root->value > v)
  436.             root = root->Left;
  437.         else if (root->value < v) root = root->Right;
  438.         else if (root->value == v)
  439.             return root;
  440.     }
  441.     return nullptr;
  442. }
  443.  
  444. template<class T>
  445. void RBT<T>::deleteVal(T v) {
  446.     auto t = search(v);
  447.     if (t == nullptr) return;
  448.     deleteNode(t);
  449.     --size;
  450. }
  451.  
  452.  
  453. template<class T>
  454. void RBT<T>::insert(T v) {
  455.     if (Head == nullptr) Head = new RBTNode<T>(v);
  456.     else
  457.         Insert(Head, v);
  458.     ++size;
  459. }
  460.  
  461. template<class T>
  462. bool RBT<T>::Empty() {
  463.     return size == 0;
  464. }
  465.  
  466. int main() {
  467.     auto tree = new RBT<int64_t>();
  468.     tree->insert(25);
  469.     cout << (tree->search(25) != nullptr) << '\n';
  470.     tree->insert(36);
  471.     tree->deleteVal(25);
  472.     cout << *tree->begin();
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement