AALanguage
The best language for those who have nothing to do
Loading...
Searching...
No Matches
Trie.cpp
Go to the documentation of this file.
1#include "Trie.h"
2
8 _curr_char = c;
9 _ends = _count = 0;
10}
12 for (auto& u : _next) {
13 if (u.second != nullptr)
14 delete u.second;
15 }
16}
18 return _curr_char;
19}
20std::map<char, TrieNode*>* TrieNode::get_next() {
21 return &_next;
22}
24 return &_ends;
25}
27 return &_count;
28}
29
31 _root = new TrieNode(0);
32}
34 delete _root;
35}
36
37void Trie::add(std::string::iterator itBeg, std::string::iterator itEnd, TrieNode* start) {
38 if (start == nullptr)
39 start = _root;
40
41 if (itBeg == itEnd) {
42 *start->get_ends() += 1;
43 return;
44 }
45
46 *start->get_count() += 1;
47 char curr_char = *itBeg;
48
49 if (start->get_next()->count(curr_char) == 0)
50 start->get_next()->insert(std::make_pair(curr_char, new TrieNode(curr_char)));
51
52 auto tmpIt = itBeg;
53 tmpIt++;
54
55 add(tmpIt, itEnd, start->get_next()->operator[](curr_char));
56}
57TrieNode* Trie::find(std::string::iterator itBeg, std::string::iterator itEnd, TrieNode* start) {
58 if (start == nullptr)
59 start = _root;
60
61 if (itBeg == itEnd)
62 return *start->get_ends() == 0 ? nullptr : start;
63
64 char curr_char = *itBeg;
65 if (start->get_next()->count(curr_char) == 0) return nullptr;
66
67 auto tmpIt = itBeg;
68 tmpIt++;
69 return find(tmpIt, itEnd, start->get_next()->operator[](curr_char));
70}
72 delete _root;
73 _root = new TrieNode(0);
74}
76 return *_root->get_count();
77}