caramba2654

Binary Search Tree

Apr 9th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. template<typename K, typename V>
  4. class Dict;
  5.  
  6. template<typename T>
  7. struct Node {
  8.  
  9.     typedef Node<T>* node_ptr;
  10.  
  11.     Node(const T& value)
  12.         : _data(value)
  13.         , _prnt(NULL)
  14.         , _left(NULL)
  15.         , _rght(NULL)
  16.     {
  17.     }
  18.  
  19.     static node_ptr minimum(node_ptr node)
  20.     {
  21.         while (node->_left) {
  22.             node = node->_left;
  23.         }
  24.         return node;
  25.     }
  26.  
  27.     static node_ptr maximum(node_ptr node)
  28.     {
  29.         while (node->_rght) {
  30.             node = node->_rght;
  31.         }
  32.         return node;
  33.     }
  34.  
  35.     T        _data;
  36.     node_ptr _prnt;
  37.     node_ptr _left;
  38.     node_ptr _rght;
  39. };
  40.  
  41. template<typename K, typename V>
  42. class dict_iterator
  43. {
  44.  
  45. public:
  46.  
  47.     typedef std::bidirectional_iterator_tag iterator_category;
  48.     typedef std::ptrdiff_t                  difference_type;
  49.     typedef std::size_t                     size_type;
  50.     typedef std::pair<K, V>                               value_type;
  51.     typedef value_type*                              pointer;
  52.     typedef value_type&                              reference;
  53.     typedef typename Node<value_type>::node_ptr      node_ptr;
  54.     typedef dict_iterator<K, V>                iterator;
  55.  
  56.     friend class Dict<K, V>;
  57.  
  58. public:
  59.  
  60.     dict_iterator()
  61.         : _this(NULL)
  62.     {
  63.     }
  64.  
  65.     dict_iterator(node_ptr value)
  66.         : _this(value)
  67.     {
  68.     }
  69.  
  70.     dict_iterator(const iterator& other)
  71.         : _this(other._this)
  72.     {
  73.     }
  74.  
  75.     ///////////////////////////////////////////////////
  76.     // Operators
  77.     ///////////////////////////////////////////////////
  78.  
  79.     // Assignment
  80.  
  81.     iterator& operator = (const iterator& other)
  82.     {
  83.         _this = other._this;
  84.         return *this;
  85.     }
  86.  
  87.     iterator& operator += (difference_type size)
  88.     {
  89.         _this += size;
  90.         return *this;
  91.     }
  92.  
  93.     iterator& operator -= (difference_type size)
  94.     {
  95.         _this -= size;
  96.         return *this;
  97.     }
  98.  
  99.     // Increment
  100.  
  101.     iterator& operator ++ ()
  102.     {
  103.         if (_this->_rght) {
  104.             _this = _this->_rght;
  105.             while (_this->_left) {
  106.                 _this = _this->_left;
  107.             }
  108.         } else {
  109.             node_ptr temp = _this->_prnt;
  110.             while (_this == temp->_rght) {
  111.                 _this = temp;
  112.                 temp = temp->_prnt;
  113.             }
  114.             if (_this->_rght != temp) {
  115.                 _this->_rght = temp;
  116.             }
  117.         }
  118.         return *this;
  119.     }
  120.  
  121.     iterator operator ++ (int)
  122.     {
  123.         iterator temp(*this);
  124.         operator++();
  125.         return temp;
  126.     }
  127.  
  128.     iterator& operator -- ()
  129.     {
  130.         if (_this->_left) {
  131.             _this = _this->_left;
  132.             while (_this->_rght) {
  133.                 _this = _this->_rght;
  134.             }
  135.         } else {
  136.             node_ptr temp = _this->_prnt;
  137.             while (_this == temp->_left) {
  138.                 _this = temp;
  139.                 temp = temp->_prnt;
  140.             }
  141.             _this = temp;
  142.         }
  143.         return *this;
  144.     }
  145.  
  146.     iterator operator -- (int)
  147.     {
  148.         iterator temp(*this);
  149.         operator--();
  150.         return temp;
  151.     }
  152.  
  153.     // Binary
  154.  
  155.     friend iterator operator + (iterator lhs, difference_type size)
  156.     {
  157.         while (size-- > 0) {
  158.             ++lhs;
  159.         }
  160.         return lhs;
  161.     }
  162.  
  163.     friend iterator operator - (iterator lhs, difference_type size)
  164.     {
  165.         while (size-- > 0) {
  166.             --lhs;
  167.         }
  168.         return lhs;
  169.     }
  170.  
  171.     // Relational
  172.  
  173.     inline bool operator == (const iterator& rhs) const
  174.     {
  175.         return _this == rhs._this;
  176.     }
  177.  
  178.     inline bool operator != (const iterator& rhs) const
  179.     {
  180.         return !operator==(rhs);
  181.     }
  182.  
  183.     // Dereference
  184.  
  185.     reference operator * () const
  186.     {
  187.         return _this->_data;
  188.     }
  189.  
  190.     pointer operator -> () const
  191.     {
  192.         return &(operator*());
  193.     }
  194.  
  195. private:
  196.  
  197.     node_ptr _this;
  198.  
  199. };
  200.  
  201. template<typename K, typename V>
  202. class Dict {
  203.  
  204. private:
  205.     typedef Dict<K, V>                      dict;
  206. public:
  207.     typedef std::pair<K, V>                 value_type;
  208.     typedef value_type*                     pointer;
  209.     typedef const value_type*               const_pointer;
  210.     typedef value_type&                     reference;
  211.     typedef const value_type&               const_reference;
  212.     typedef std::size_t                     size_type;
  213.     typedef std::ptrdiff_t                  difference_type;
  214.     typedef Node<value_type>                node;
  215.     typedef node*                           node_ptr;
  216.     typedef node&                           node_reference;
  217.     typedef dict_iterator<K, V>       iterator;
  218.     typedef std::reverse_iterator<iterator> reverse_iterator;
  219.  
  220. public:
  221.  
  222.     iterator begin()
  223.     {
  224.         return iterator(_root->minimum());
  225.     }
  226.  
  227.     iterator end()
  228.     {
  229.         return iterator(NULL);
  230.     }
  231.  
  232.     reverse_iterator rbegin()
  233.     {
  234.         return reverse_iterator(_root->maximum());
  235.     }
  236.  
  237.     reverse_iterator rend()
  238.     {
  239.         return reverse_iterator(end());
  240.     }
  241.  
  242. public:
  243.  
  244.     Dict()
  245.         : _root(NULL)
  246.         , _size(0)
  247.     {
  248.  
  249.     }
  250.  
  251.     Dict& operator = (const Dict& other)
  252.     {
  253.  
  254.     }
  255.  
  256.     ~Dict()
  257.     {
  258.     }
  259.  
  260.     size_type size() const
  261.     {
  262.         return _size;
  263.     }
  264.  
  265.     iterator find(const K& key)
  266.     {
  267.         if (_size == 0) {
  268.             return end();
  269.         }
  270.  
  271.         if (_size == 1 && _root->_data.first == key) {
  272.             return iterator(_root);
  273.         }
  274.  
  275.         assert(_root != NULL);
  276.         node_ptr it = _root;
  277.         while (it) {
  278.             bool hasLeftChild = it->_left;
  279.             bool hasRghtChild = it->_rght;
  280.             bool isKeyLess = key < it->_data.first;
  281.             bool isKeyMore = it->_data.first < key;
  282.  
  283.             if (hasLeftChild && isKeyLess) {
  284.                 it = it->_left;
  285.             } else if (hasRghtChild && isKeyMore) {
  286.                 it = it->_rght;
  287.             } else {
  288.                 if (!(isKeyLess || isKeyMore)) {
  289.                     // Key found!
  290.                     return iterator(it);
  291.                 } else {
  292.                     return end();
  293.                 }
  294.             }
  295.         }
  296.         return end();
  297.     }
  298.  
  299.     inline std::pair<iterator, bool> insert(const K& key, const V& value)
  300.     {
  301.         return insert(std::make_pair(key, value));
  302.     }
  303.  
  304.     std::pair<iterator, bool> insert(const value_type& value)
  305.     {
  306.         // If tree in empty, give special treatment to root
  307.         if (_size == 0) {
  308.             _root = new node(value);
  309.             ++_size;
  310.             return std::make_pair(iterator(_root), true);
  311.         }
  312.  
  313.         node_ptr ptr = _root;
  314.         while (ptr) {
  315.             bool hasLeftChild = ptr->_left;
  316.             bool hasRghtChild = ptr->_rght;
  317.             bool isKeyLess = value.first < ptr->_data.first;
  318.             bool isKeyMore = ptr->_data.first < value.first;
  319.  
  320.             if (hasLeftChild && isKeyLess) {
  321.                 ptr = ptr->_left;
  322.             } else if (hasRghtChild && isKeyMore) {
  323.                 ptr = ptr->_rght;
  324.             } else {
  325.                 if (isKeyLess) {
  326.                     ptr->_left = new node(value);
  327.                     ptr->_left->_prnt = ptr;
  328.                     ptr = ptr->_left;
  329.                 } else if (isKeyMore) {
  330.                     ptr->_rght = new node(value);
  331.                     ptr->_rght->_prnt = ptr;
  332.                     ptr = ptr->_rght;
  333.                 }
  334.                 ++_size;
  335.                 return std::make_pair(iterator(ptr), true);
  336.             }
  337.         }
  338.         return std::make_pair(end(), false);
  339.     }
  340.  
  341.     void erase(iterator it)
  342.     {
  343.         // Return if the iterator doesn't make sense
  344.         if (it == end()) {
  345.             return;
  346.         }
  347.  
  348.         // If it's the last node, give it special treatment
  349.         if (_size == 1) {
  350.             delete _root;
  351.             _root = NULL;
  352.             --_size;
  353.             return;
  354.         }
  355.  
  356.         node_ptr ptr = it._this;
  357.         if (!ptr->_left) {
  358.             // No left child, so just have the right child be the new child
  359.             if (ptr->_prnt && ptr->_prnt->_left == ptr) {
  360.                 // If he's the left child
  361.                 ptr->_prnt->_left = ptr->_rght;
  362.             } else if (ptr->_prnt && ptr->_prnt->_rght == ptr) {
  363.                 // If he's the right child
  364.                 ptr->_prnt->_rght = ptr->_rght;
  365.             }
  366.             if (ptr->_rght) {
  367.                 ptr->_rght->_prnt = ptr->_prnt;
  368.             }
  369.             delete ptr;
  370.         } else if (!ptr->_rght) {
  371.             // No right child, so just have the left child be the new child
  372.             if (ptr->_prnt && ptr->_prnt->_left == ptr) {
  373.                 // If he's the left child
  374.                 ptr->_prnt->_left = ptr->_left;
  375.             } else if (ptr->_prnt && ptr->_prnt->_rght == ptr) {
  376.                 // If he's the right child
  377.                 ptr->_prnt->_rght = ptr->_left;
  378.             }
  379.             if (ptr->_left) {
  380.                 ptr->_left->_prnt = ptr->_prnt;
  381.             }
  382.             delete ptr;
  383.         } else {
  384.             // It has both children, so take the successor
  385.             node_ptr successor = node::minimum(ptr->_rght);
  386.             // If the successor has right child, make it children of his parent
  387.             successor->_prnt->_left = successor->_rght;
  388.             ptr->_data = successor->_data;
  389.             free(successor);
  390.         }
  391.         --_size;
  392.     }
  393.  
  394. //    V& operator[](const K& key)
  395. //    {
  396. //        return insert(key, V()).first->second;
  397. //    }
  398. //
  399. //    const V& operator[](const K& key) const
  400. //    {
  401. //        return insert(key, V()).first.second;
  402. //    }
  403.  
  404. private:
  405.  
  406.     node_ptr   _root;
  407.     size_type  _size;
  408.  
  409. };
  410.  
  411. int main()
  412. {
  413.     Dict<int, int> c;
  414.  
  415.     int read;
  416.     while (true) {
  417.         std::cin >> read;
  418.         if (read < 0) {
  419.             break;
  420.         }
  421.         if (c.find(read) == c.end()) {
  422.             c.insert(read, read);
  423.         }
  424.     }
  425.  
  426.     std::cin >> read;
  427.     Dict<int, int>::iterator search = c.find(read);
  428.     if (search == c.end()) {
  429.         c.insert(std::make_pair(read, read));
  430.     } else {
  431.         c.erase(search);
  432.     }
  433.  
  434.     std::cout << c.size() << '\n';
  435.  
  436. }
Add Comment
Please, Sign In to add comment