Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <memory>
  5. using namespace std;
  6.  
  7. template<typename Type>
  8. struct Node {
  9.     Type key;
  10.     shared_ptr<Node <Type>> parent, l_son, r_son;
  11.  
  12.     Node() {}
  13.  
  14.     Node(const Node &other): key(other.key),
  15.                              parent(nullptr),
  16.                              l_son(nullptr),
  17.                              r_son(nullptr) {}
  18.  
  19.     Node(const Type &key) : key(key),
  20.                             parent(nullptr),
  21.                             l_son(nullptr),
  22.                             r_son(nullptr) {}
  23.  
  24.     Node(const Type &key, shared_ptr<Node> l_son, shared_ptr<Node> r_son) : key(key),
  25.                                                                             parent(nullptr),
  26.                                                                             l_son(l_son),
  27.                                                                             r_son(r_son) {}
  28.     ~Node() {}
  29.  
  30.     friend ostream &operator<<(ostream &stream, Node<Type> &node) {
  31.         stream << node << ": " << "key = " << node.key
  32.                << " l_son = " << node.l_son << " r_son = "
  33.                << node.r_son << " parent = " << node.parent << endl;
  34.  
  35.         return stream;
  36.     }
  37. };
  38.  
  39. template<typename Type>
  40. class NodeIterator {
  41.  private:
  42.     const Node <Type> *p, *root;
  43.  
  44.  public:
  45.     NodeIterator(): p(nullptr), root(nullptr) {}
  46.  
  47.     NodeIterator(shared_ptr<const Node <Type>> p,
  48.                  shared_ptr<const Node <Type>> root): p(p.get()), root(root.get()) {}
  49.  
  50.     NodeIterator(shared_ptr<const Node<Type>> root): root(root.get()) {
  51.         p = this->root;
  52.         while (p != nullptr && p->l_son != nullptr) {
  53.             p = p->l_son.get();
  54.         }
  55.     }
  56.  
  57.     NodeIterator(const NodeIterator &other): p(other.p), root(other.root) {}
  58.  
  59.     NodeIterator &operator=(const NodeIterator &other) {
  60.         p = other.p;
  61.         root = other.root;
  62.         return *this;
  63.     }
  64.  
  65.     ~NodeIterator() {
  66.     }
  67.  
  68.     const Type &operator*() const {
  69.         return p->key;
  70.     }
  71.  
  72.     const Type *operator->() const {
  73.         return &(p->key);
  74.     }
  75.  
  76.     bool operator==(const NodeIterator<Type> &other) const {
  77.         return (p == other.p && root == other.root);
  78.     }
  79.  
  80.     bool operator!=(const NodeIterator<Type> &other) const {
  81.         return !(p == other.p && root == other.root);
  82.     }
  83.  
  84.     NodeIterator &operator++() {
  85.         if (p == nullptr) {
  86.             return *this;
  87.         }
  88.  
  89.         if (p->r_son != nullptr) {
  90.             p = p->r_son.get();
  91.         } else {
  92.             while (p->parent != nullptr && p->parent->r_son.get() == p) {
  93.                 p = p->parent.get();
  94.             }
  95.             p = p->parent.get();
  96.  
  97.             return *this;
  98.         }
  99.  
  100.         while (p->l_son != nullptr) {
  101.             p = p->l_son.get();
  102.         }
  103.  
  104.         return *this;
  105.     }
  106.  
  107.     NodeIterator operator++(int) {
  108.         auto res = *this;
  109.         ++*this;
  110.  
  111.         return res;
  112.     }
  113.  
  114.     NodeIterator &operator--() {
  115.         if (p == nullptr) {
  116.             p = root;
  117.             if (p == nullptr) {
  118.                 return *this;
  119.             }
  120.  
  121.             while (p->r_son != nullptr) {
  122.                 p = p->r_son.get();
  123.             }
  124.  
  125.             return *this;
  126.         }
  127.  
  128.         if (p->l_son != nullptr) {
  129.             p = p->l_son.get();
  130.             while (p->r_son != nullptr) {
  131.                 p = p->r_son.get();
  132.             }
  133.  
  134.             return *this;
  135.         }
  136.  
  137.         while (p->parent != nullptr && p->parent->l_son.get() == p) {
  138.             p = p->parent.get();
  139.         }
  140.         p = p->parent.get();
  141.  
  142.         return *this;
  143.     }
  144.  
  145.     NodeIterator operator--(int) {
  146.         auto res = *this;
  147.         --*this;
  148.  
  149.         return res;
  150.     }
  151. };
  152.  
  153. template<typename Type>
  154. class Set {
  155.     typedef NodeIterator<Type> iterator;
  156.  private:
  157.     int cnt;
  158.     shared_ptr<Node <Type>> root;
  159.     iterator begin_iterator;
  160.  
  161.  public:
  162.  
  163.     // Constuctors
  164.     Set(): cnt(0),
  165.            root(nullptr) {}
  166.  
  167.     template <class TIterator>
  168.     Set(TIterator first, TIterator last): Set() {
  169.         while (first != last) {
  170.             insert(*first++);
  171.         }
  172.     }
  173.  
  174.     Set(std::initializer_list<Type> data): Set() {
  175.         for (auto x : data) {
  176.             insert(x);
  177.         }
  178.     }
  179.  
  180.     Set(const Set<Type> &other) {
  181.         cnt = other.cnt;
  182.         root = copy_tree(other.root);
  183.     }
  184.  
  185.     ~Set() {
  186.         delete_tree(root);
  187.     }
  188.  
  189.     Set<Type> &operator=(const Set<Type> &other) {
  190.         cnt = other.cnt;
  191.         delete_tree(root);
  192.         root = copy_tree(other.root);
  193.  
  194.         return *this;
  195.     }
  196.  
  197.     void update_min() {
  198.         if (root == nullptr) {
  199.             return;
  200.         }
  201.  
  202.         auto p = root;
  203.         while (p->l_son != nullptr) {
  204.             p = p->l_son;
  205.         }
  206.  
  207.         root = splay(p);
  208.     }
  209.  
  210.     void update_max() {
  211.         if (root == nullptr) {
  212.             return;
  213.         }
  214.  
  215.         auto p = root;
  216.         while (p->r_son != nullptr) {
  217.             p = p->r_son;
  218.         }
  219.  
  220.         root = splay(p);
  221.     }
  222.  
  223.     bool equal(const Type &key1, const Type &key2) const {
  224.         return !(key1 < key2 || key2 < key1);
  225.     }
  226.  
  227.     bool bigger(const Type &key1, const Type &key2) const {
  228.         return (key2 < key1);
  229.     }
  230.  
  231.     void delete_tree(shared_ptr<Node <Type>> &v) {
  232.         if (v == nullptr) {
  233.             return;
  234.         }
  235.  
  236.         delete_tree(v->l_son);
  237.         delete_tree(v->r_son);
  238.  
  239.         v->parent = nullptr;
  240.     }
  241.  
  242.     shared_ptr<Node <Type>> copy_tree(const shared_ptr<Node <Type>> &v) {
  243.         if (v == nullptr) {
  244.             return nullptr;
  245.         }
  246.  
  247.         shared_ptr<Node <Type>> new_v(new Node<Type>(v->key));
  248.         new_v->l_son = copy_tree(v->l_son);
  249.         new_v->r_son = copy_tree(v->r_son);
  250.  
  251.         keep_parent(new_v);
  252.  
  253.         return new_v;
  254.     }
  255.  
  256.     void print(const shared_ptr<Node <Type>> node) const {
  257.         if (node == nullptr) {
  258.             return;
  259.         }
  260.  
  261.         print(node->l_son);
  262.  
  263.         cout << node << ": " << "key = " << node->key
  264.              << " l_son = " << node->l_son << " r_son = "
  265.              << node->r_son << " parent = " << node->parent << endl;
  266.  
  267.         print(node->r_son);
  268.     }
  269.  
  270.     shared_ptr<Node <Type>> find(shared_ptr<Node <Type>> node, const Type &key) {
  271.         while (true) {
  272.             if (node == nullptr) {
  273.                 return node;
  274.             }
  275.  
  276.             if (equal(node->key, key)) {
  277.                 return splay(node);
  278.             }
  279.  
  280.             if (bigger(node->key, key) && node->l_son != nullptr) {
  281.                 node = node->l_son;
  282.             } else if (node->key < key && node->r_son != nullptr) {
  283.                 node = node->r_son;
  284.             } else {
  285.                 return splay(node);
  286.             }
  287.         }
  288.     }
  289.  
  290.     shared_ptr<Node <Type>> insert(shared_ptr<Node <Type>> root, const Type &key) {
  291.         auto sons = split(root, key);
  292.         shared_ptr<Node <Type>> new_root(new Node<Type>(key, sons.first, sons.second));
  293.         keep_parent(new_root);
  294.  
  295.         return new_root;
  296.     }
  297.  
  298.     shared_ptr<Node <Type>> remove(shared_ptr<Node <Type>> root, const Type &key) {
  299.         root = find(root, key);
  300.  
  301.         if (root->key != key) {
  302.             return root;
  303.         }
  304.  
  305.         set_parent(root->l_son, nullptr);
  306.         set_parent(root->r_son, nullptr);
  307.  
  308.         return merge(root->l_son, root->r_son);
  309.     }
  310.  
  311.     void print_once(const shared_ptr<Node <Type>> node) const {
  312.         if (node == nullptr) {
  313.             cout << "nullptr" << endl;
  314.             return;
  315.         }
  316.  
  317.         cout << node << ": " << "key = " << node->key
  318.              << " l_son = " << node->l_son << " r_son = "
  319.              << node->r_son << " parent = " << node->parent << endl;
  320.     }
  321.  
  322.     void set_parent(shared_ptr<Node <Type>> child, shared_ptr<Node <Type>> parent) {
  323.         if (child == nullptr) {
  324.             return;
  325.         }
  326.  
  327.         child->parent = parent;
  328.     }
  329.  
  330.     void keep_parent(shared_ptr<Node <Type>> node) {
  331.         if (node == nullptr) {
  332.             return;
  333.         }
  334.  
  335.         set_parent(node->l_son, node);
  336.         set_parent(node->r_son, node);
  337.     }
  338.  
  339.     void rotate(shared_ptr<Node <Type>> child, shared_ptr<Node <Type>> parent) {
  340.         shared_ptr<Node <Type>> gparent = parent->parent;
  341.         if (gparent != nullptr) {
  342.             if (gparent->l_son == parent) {
  343.                 gparent->l_son = child;
  344.             } else {
  345.                 gparent->r_son = child;
  346.             }
  347.         }
  348.  
  349.         if (parent->l_son == child) {
  350.             parent->l_son = child->r_son;
  351.             child->r_son = parent;
  352.         } else {
  353.             parent->r_son = child->l_son;
  354.             child->l_son = parent;
  355.         }
  356.  
  357.         keep_parent(child);
  358.         keep_parent(parent);
  359.         child->parent = gparent;
  360.     }
  361.  
  362.     shared_ptr<Node <Type>> splay(shared_ptr<Node <Type>> child) {
  363.         while (true) {
  364.             if (child->parent == nullptr) {
  365.                 return child;
  366.             }
  367.  
  368.             auto parent = child->parent;
  369.             auto gparent = parent->parent;
  370.  
  371.             if (gparent == nullptr) {
  372.                 rotate(child, parent);
  373.                 return child;
  374.             }
  375.  
  376.             bool zigzig = ((gparent->l_son == parent) == (parent->l_son == child));
  377.             if (zigzig) {
  378.                 rotate(parent, gparent);
  379.                 rotate(child, parent);
  380.             } else {
  381.                 rotate(child, parent);
  382.                 rotate(child, gparent);
  383.             }
  384.         }
  385.     }
  386.  
  387.     pair<shared_ptr<Node <Type>>, shared_ptr<Node <Type>>>
  388.     split(shared_ptr<Node <Type>> root, const Type &key) {
  389.         if (root == nullptr) {
  390.             return make_pair(root, root);
  391.         }
  392.  
  393.         root = find(root, key);
  394.         if (equal(root->key, key)) {
  395.             set_parent(root->l_son, nullptr);
  396.             set_parent(root->r_son, nullptr);
  397.  
  398.             return make_pair(root->l_son, root->r_son);
  399.         }
  400.  
  401.         if (root->key < key) {
  402.             auto r_son = root->r_son;
  403.             root->r_son = nullptr;
  404.             set_parent(r_son, nullptr);
  405.             return make_pair(root, r_son);
  406.         }
  407.  
  408.         auto l_son = root->l_son;
  409.         root->l_son = nullptr;
  410.         set_parent(l_son, nullptr);
  411.         return make_pair(l_son, root);
  412.     }
  413.  
  414.     shared_ptr<Node <Type>> merge(shared_ptr<Node <Type>> left, shared_ptr<Node <Type>> right) {
  415.         if (left == nullptr) {
  416.             return right;
  417.         }
  418.         if (right == nullptr) {
  419.             return left;
  420.         }
  421.  
  422.         right = find(right, left->key);
  423.         right->l_son = left;
  424.         left->parent = right;
  425.         return right;
  426.     }
  427.  
  428.     // interface
  429.  
  430.     void print() const {
  431.         if (root == nullptr) {
  432.             cout << "empty" << endl;
  433.         } else {
  434.             print(root);
  435.         }
  436.     }
  437.  
  438.     iterator find(const Type &key) const {
  439.         shared_ptr<Node <Type>> p = root;
  440.  
  441.         while (p != nullptr && !equal(p->key, key)) {
  442.             if (bigger(p->key, key)) {
  443.                 p = p->l_son;
  444.             } else {
  445.                 p = p->r_son;
  446.             }
  447.         }
  448.  
  449.         if (p == nullptr) {
  450.             return end();
  451.         }
  452.         return iterator(p, root);
  453.     }
  454.  
  455.     iterator lower_bound(const Type &key) const {
  456.         return lower_bound(root, key);
  457.     }
  458.  
  459.     iterator lower_bound(shared_ptr<Node <Type>> v, const Type &key) const {
  460.         if (v == nullptr) {
  461.             return end();
  462.         }
  463.  
  464.         if (v->key < key) {
  465.             return lower_bound(v->r_son, key);
  466.         }
  467.  
  468.         iterator res(v, root);
  469.         iterator res1 = lower_bound(v->l_son, key);
  470.         if (res1 != end() && *res1 < *res) {
  471.             return res1;
  472.         }
  473.         return res;
  474.     }
  475.  
  476.     void erase(const Type &key) {
  477.         if (root == nullptr) {
  478.             return;
  479.         }
  480.  
  481.         root = find(root, key);
  482.         if (root->key == key) {
  483.             root = remove(root, key);
  484.             --cnt;
  485.         }
  486.     }
  487.  
  488.     void insert(const Type &key) {
  489.         root = find(root, key);
  490.  
  491.         if (root == nullptr || !equal(root->key, key)) {
  492.             root = insert(root, key);
  493.             ++cnt;
  494.         }
  495.     }
  496.  
  497.     bool empty() const {
  498.         return cnt == 0;
  499.     }
  500.  
  501.     size_t size() const {
  502.         return cnt;
  503.     }
  504.  
  505.     iterator begin() const {
  506.         return iterator(root);
  507.     }
  508.  
  509.     iterator end() const {
  510.         return iterator(nullptr, root);
  511.     }
  512. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement