Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct Node {
  9. char ch;
  10. map<char, Node*> children;
  11. };
  12.  
  13. class Trie {
  14. public:
  15. Trie() { head.ch = -1; };
  16. ~Trie();
  17.  
  18. void build_trie(string words[], int length);
  19. void insert(string word);
  20. void search(string word, bool &result);
  21.  
  22. void print_tree(map<char, Node*> tree);
  23. void print();
  24.  
  25. protected:
  26. Node head;
  27. // Keep all newly created node in an array, for the ease of
  28. // memory release.
  29. vector<Node*> children;
  30. };
  31.  
  32. Trie::~Trie() {
  33. for (int i=0; i<children.size(); ++i) {
  34. delete children[i];
  35. }
  36. }
  37.  
  38. void Trie::build_trie(string words[], int length) {
  39. for (int i=0; i<length; ++i) {
  40. insert(words[i]);
  41. }
  42. }
  43.  
  44. void Trie::insert(string word) {
  45. map<char, Node*> *current_tree = &head.children;
  46. map<char, Node*>::iterator it;
  47.  
  48. for (int i=0; i<word.length(); ++i) {
  49. char ch = word[i];
  50.  
  51. if ((it = current_tree->find(ch)) != current_tree->end()) {
  52. current_tree = &it->second->children;
  53. continue;
  54. }
  55.  
  56. if (it == current_tree->end()) {
  57. // Display inserting position in the tree, for debug use
  58. //
  59. // cout << "Inserting " << ch << endl;
  60. // cout << "on layer " << endl;
  61. // map<char, Node*>::iterator temp = current_tree->begin();
  62. // for (; temp != current_tree->end(); ++temp)
  63. // cout << temp->first << endl;
  64.  
  65. Node* new_node = new Node();
  66. new_node->ch = ch;
  67. (*current_tree)[ch] = new_node;
  68.  
  69. // For continuous inserting a word.
  70. current_tree = &new_node->children;
  71.  
  72. // For the ease of memory clean up.
  73. children.push_back(new_node);
  74. }
  75. }
  76. }
  77.  
  78. void Trie::search(string word, bool &result) {
  79. map<char, Node*> current_tree = head.children;
  80. map<char, Node*>::iterator it;
  81.  
  82. for (int i=0; i<word.length(); ++i) {
  83. if ((it = current_tree.find(word[i])) == current_tree.end()) {
  84. result = false;
  85. return;
  86. }
  87. current_tree = it->second->children;
  88. }
  89.  
  90. result = true;
  91. return ;
  92. }
  93.  
  94. void Trie::print_tree(map<char, Node*> tree) {
  95. if (tree.empty()) {
  96. return;
  97. }
  98.  
  99. for (map<char, Node*>::iterator it=tree.begin(); it!=tree.end(); ++it) {
  100. cout << it->first << endl;
  101. print_tree(it->second->children);
  102. }
  103. }
  104.  
  105. void Trie::print() {
  106. map<char, Node*> current_tree = head.children;
  107. print_tree(current_tree);
  108. }
  109.  
  110.  
  111. int main(int argc, char** argv)
  112. {
  113. string words[] = {"foo", "bar", "baz", "barz"};
  114. Trie trie;
  115. trie.build_trie(words, 4);
  116. cout << "All nodes..." << endl;
  117. trie.print();
  118.  
  119. cout << "Searching..." << endl;
  120. bool in_trie = false;
  121. trie.search("foo", in_trie);
  122. cout << "foo " << in_trie << endl;
  123.  
  124. trie.search("fooz", in_trie);
  125. cout << "fooz " << in_trie << endl;
  126.  
  127. trie.search("bar", in_trie);
  128. cout << "bar " << in_trie << endl;
  129.  
  130. trie.search("baz", in_trie);
  131. cout << "baz " << in_trie << endl;
  132.  
  133. trie.search("barz", in_trie);
  134. cout << "barz " << in_trie << endl;;
  135.  
  136. trie.search("bbb", in_trie);
  137. cout << "bbb " << in_trie << endl;;
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement