Advertisement
ludaludaed

rb_tree_old

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