Advertisement
edward4324

Trie

Apr 20th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. //.h
  2.  
  3. #pragma once
  4. #include <iostream>
  5. #include <map>
  6.  
  7. //template <typename T>
  8. class Trie
  9. {
  10.  
  11. protected:
  12.  
  13.     struct Node{
  14.  
  15.         //T * value;
  16.         std::map<char, Node*> next;
  17.         bool isEnd;
  18.  
  19.         Node() : /*value(nullptr),*/ isEnd(false) {};
  20.  
  21.     };
  22.    
  23.     Node* root;
  24.  
  25.  
  26. public:
  27.  
  28.     Trie() { root = new Node(); };
  29.     //~Trie();
  30.  
  31.     void add(std::string str, int value);
  32.     /*void deleteWord(std::string word);
  33.     void clear();*/
  34.     void print();
  35.     bool count(std::string str);
  36.     void deleteWord(std::string);
  37.  
  38. private:
  39.    
  40.     void _add(Node* ptr, std::string str, int value);
  41.     /*void _deleteWord(std::string word);
  42.     void _clear();*/
  43.     void _print(Node* ptr, std::string str);
  44.     bool _count(Node* ptr, std::string str);
  45.     void _deleteWord(Node* ptr, std::string str, int level);
  46.  
  47.     //additional funcs
  48.     bool hasChild(Node*);
  49.  
  50.     /*void callback(int e);
  51.     void callback(std::string str);*/
  52.  
  53. };
  54.  
  55. //.cpp
  56.  
  57. #include "Trie.h"
  58.  
  59. void Trie::add(std::string str, int value)
  60. {
  61.    
  62.     _add(root, str, value);
  63.  
  64. }
  65.  
  66. void Trie::_add(Node* ptr, std::string str, int value)
  67. {
  68.  
  69.     for (int i = 0; i < str.length(); i++) {
  70.  
  71.         if (ptr->next.count(str[i]) == 0) {
  72.  
  73.             ptr->next[str[i]] = new Node();
  74.  
  75.         }
  76.  
  77.         ptr = ptr->next[str[i]];
  78.  
  79.     }
  80.  
  81.     ptr->isEnd = true;
  82.  
  83. }
  84.  
  85. //function to check if there a child of ptr pointer
  86. bool Trie::hasChild(Node* ptr)
  87. {
  88.  
  89.     bool result = false;
  90.  
  91.     for (std::map<char, Node*>::iterator it = ptr->next.begin(); it != ptr->next.end(); it++) {
  92.  
  93.         result = (it->second != nullptr);
  94.  
  95.     }
  96.  
  97.     return result;
  98. }
  99.  
  100. //void Trie::callback(int e)
  101. //{
  102. //
  103. //  switch (e) {
  104. //
  105. //  case 0:
  106. //
  107. //      std::cout << "Слово не было найдено" << std::endl;
  108. //
  109. //  break;
  110. //
  111. //  case 1:
  112. //
  113. //
  114. //
  115. //  break;
  116. //
  117. //  }
  118. //
  119. //}
  120. //
  121. //void Trie::callback(std::string str)
  122. //{
  123. //
  124. //  std::cout << str << std::endl;
  125. //
  126. //}
  127.  
  128. void Trie::print()
  129. {
  130.  
  131.     _print(root, "");
  132.  
  133. }
  134.  
  135. void Trie::_print(Node* ptr, std::string str)
  136. {
  137.     int i;
  138.  
  139.     if (ptr->isEnd) {
  140.  
  141.         str.push_back('\0');
  142.         std::cout << str << std::endl;
  143.  
  144.     }
  145.    
  146.     if (hasChild(ptr)) {
  147.  
  148.         for (auto& it : ptr->next) {
  149.        
  150.             str.push_back(it.first);
  151.             _print(it.second, str);
  152.  
  153.         }
  154.  
  155.     }
  156.  
  157. }
  158.  
  159. bool Trie::count(std::string str)
  160. {
  161.  
  162.     return _count(root, str);
  163.  
  164. }
  165.  
  166. bool Trie::_count(Node* ptr, std::string str)
  167. {
  168.  
  169.     for (int i = 0; i < str.length(); i++) {
  170.  
  171.         if (ptr->next.count(str[i]) != 0) {
  172.  
  173.             ptr = ptr->next[str[i]];
  174.  
  175.         }
  176.         else
  177.             return false;
  178.  
  179.     }
  180.  
  181.     return true;
  182.  
  183. }
  184.  
  185. void Trie::deleteWord(std::string str)
  186. {
  187.  
  188.     _deleteWord(root, str, 0);
  189.  
  190. }
  191.  
  192.  
  193. void Trie::_deleteWord(Node* ptr, std::string str, int level)
  194. {
  195.  
  196.     if (level < str.length()) {
  197.  
  198.         if (!hasChild(ptr->next[str[level]]) && ptr->next.size() == 1){
  199.        
  200.             ptr->next.clear();
  201.             delete ptr;
  202.  
  203.         }
  204.         else {
  205.  
  206.             _deleteWord(ptr->next[str[level]], str, level + 1);
  207.  
  208.         }
  209.  
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement