in_chainz

Untitled

Mar 1st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <map>
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. struct Node {
  7.     std::map<std::string, Node> children;
  8. };
  9.  
  10. class Tree {
  11. private:
  12.     Node root;
  13.  
  14. public:
  15.     bool Has(const std::vector<std::string> &node) const;
  16.  
  17.     void Insert(const std::vector<std::string> &node);
  18.  
  19.     void Delete(const std::vector<std::string> &node);
  20.  
  21.     void print(Node &cur);
  22.  
  23.     Node & getroot() {
  24.         return root;
  25.     }
  26. };
  27.  
  28. bool Tree::Has(const std::vector<std::string> &node) const {
  29.     auto it = node.begin();
  30.     Node cur = this->root;
  31. /*
  32.     for (auto& [key, val] : cur.children)
  33.         std::cerr << key << ' ';
  34. */
  35.     for (; it != node.end() && cur.children.find(*it) != cur.children.end();
  36.            cur = cur.children[*it]) {}
  37.     return it == node.end();
  38. }
  39.  
  40. void Tree::Insert(const std::vector<std::string> &node) {
  41.     if (Has(node))
  42.         return;
  43.  
  44.     auto it = node.begin();
  45.     Node cur = this->root;
  46.     for (; it != node.end(); ++it) {
  47.         if (cur.children.find(*it) == cur.children.end())
  48.             cur.children[*it] = Node();
  49.         // std::cerr << (cur.children.find(*it) != cur.children.end()) << std::endl;
  50.         // for (auto& [key, val] : cur.children)
  51.             // std::cerr << key << ' ';
  52.         cur = cur.children[*it];
  53.     }
  54. }
  55.  
  56. void Tree::Delete(const std::vector<std::string> &node) {
  57.     if (!Has(node))
  58.         return;
  59.  
  60.     auto it = node.begin();
  61.     Node cur = this->root;
  62.  
  63.     uint32_t counter = 0;
  64.     for (; it != node.end() && cur.children.find(*it) != cur.children.end(); ++counter) {
  65.         if (counter + 1 == node.size())
  66.             cur.children.erase(*it);
  67.         else
  68.             cur = cur.children[*it];
  69.     }
  70. }
  71.  
  72. void Tree::print(Node &cur) {
  73.     for (auto& [key, val] : cur.children)
  74.         std::cout << key << ' ';
  75.     std::cout << std::endl;
  76.     for (auto& [key, val] : cur.children)
  77.         print(val);
  78. }
  79.  
  80. int main() {
  81.  
  82.     std::vector<std::string> one = {"hello", "world", "world", "kek", "lol"};
  83.     std::vector<std::string> two = {"hello", "world", "world"};
  84.     std::vector<std::string> three = {"hello", "world", "world", "kek", "lol", "bye"};
  85.     std::vector<std::string> four = {"hello", "world", "world", "bye"};
  86.  
  87.     Tree t;
  88.     std::cout << t.Has(one) << std::endl;
  89.     t.Insert(one);
  90.     // t.print(t.getroot());
  91.     std::cout << t.Has(one) << ' ' << t.Has(two) <<  ' ' << t.Has(three) << std::endl;
  92.     t.Insert(four);
  93.     // t.print(t.getroot());
  94. }
Advertisement
Add Comment
Please, Sign In to add comment