Advertisement
Sanlover

Untitled

Mar 23rd, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct TrieNode
  8. {
  9. vector<TrieNode*> children;
  10. bool isEndOfWord;
  11.  
  12. TrieNode()
  13. {
  14. children = vector<TrieNode*>(26, nullptr);
  15. isEndOfWord = false;
  16. }
  17. };
  18.  
  19. class Trie
  20. {
  21. private:
  22. TrieNode* root;
  23.  
  24. public:
  25. Trie()
  26. {
  27. root = new TrieNode();
  28. }
  29.  
  30. void insert(const string& word)
  31. {
  32. TrieNode* node = root;
  33. for (char c : word)
  34. {
  35. if (node->children[c - 'a'] == nullptr)
  36. {
  37. node->children[c - 'a'] = new TrieNode();
  38. }
  39. node = node->children[c - 'a'];
  40. }
  41. node->isEndOfWord = true;
  42. }
  43.  
  44. bool search(const string& word)
  45. {
  46. TrieNode* node = root;
  47. for (char c : word)
  48. {
  49. if (node->children[c - 'a'] == nullptr)
  50. {
  51. return false;
  52. }
  53. node = node->children[c - 'a'];
  54. }
  55. return node != nullptr && node->isEndOfWord;
  56. }
  57.  
  58. bool startsWith(const string& prefix)
  59. {
  60. TrieNode* node = root;
  61. for (char c : prefix)
  62. {
  63. if (node->children[c - 'a'] == nullptr)
  64. {
  65. return false;
  66. }
  67. node = node->children[c - 'a'];
  68. }
  69. return node != nullptr;
  70. }
  71.  
  72. void removePrefix(const string& prefix)
  73. {
  74. removePrefixHelper(root, prefix, 0);
  75. deleteEmptyNodes(root);
  76. }
  77.  
  78. private:
  79. void removePrefixHelper(TrieNode* node, const string& prefix, int index)
  80. {
  81. if (node == nullptr)
  82. {
  83. return;
  84. }
  85. if (index == prefix.length())
  86. {
  87. removeWords(node);
  88. return;
  89. }
  90. removePrefixHelper(node->children[prefix[index] - 'a'], prefix, index + 1);
  91. }
  92.  
  93. void removeWords(TrieNode* node)
  94. {
  95. if (node == nullptr)
  96. {
  97. return;
  98. }
  99. node->isEndOfWord = false;
  100. for (int i = 0; i < node->children.size(); i++)
  101. {
  102. removeWords(node->children[i]);
  103. }
  104. }
  105.  
  106. void deleteEmptyNodes(TrieNode* node)
  107. {
  108. if (node == nullptr)
  109. {
  110. return;
  111. }
  112. for (int i = 0; i < node->children.size(); i++)
  113. {
  114. if (node->children[i] != nullptr)
  115. {
  116. if (node->children[i]->isEndOfWord == false && isNodeEmpty(node->children[i]))
  117. {
  118. delete node->children[i];
  119. node->children[i] = nullptr;
  120. }
  121. else
  122. {
  123. deleteEmptyNodes(node->children[i]);
  124. }
  125. }
  126. }
  127. }
  128.  
  129. bool isNodeEmpty(TrieNode* node)
  130. {
  131. for (TrieNode* child : node->children)
  132. {
  133. if (child != nullptr)
  134. {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. };
  141.  
  142.  
  143. int main()
  144. {
  145. Trie trie;
  146. trie.insert("apple");
  147. trie.insert("banana");
  148. trie.insert("app");
  149. trie.insert("apartment");
  150. trie.insert("baby");
  151. trie.insert("book");
  152. trie.removePrefix("ap");
  153. cout << (trie.search("apple") ? "apple found" : "apple not found") << endl;
  154. cout << (trie.search("banana") ? "banana found" : "banana not found") << endl;
  155. cout << (trie.search("app") ? "app found" : "app not found") << endl;
  156. cout << (trie.search("apartment") ? "apartment found" : "apartment not found") << endl;
  157. cout << (trie.search("baby") ? "baby found" : "baby not found") << endl;
  158. cout << (trie.search("book") ? "book found" : "book not found") << endl;
  159. return 0;
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement