Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- #include "trie.hpp"
- #include <iostream>
- #include <vector>
- #include <algorithm>
- bool hasAnotherWord = false;
- trie_node* find(const std::string &str,trie_node* root) {
- trie_node* currentNode = root;
- const char* sptr = str.c_str();
- while (*sptr) {
- if(currentNode->children[*sptr] == nullptr) {
- throw 20;
- }
- currentNode = currentNode->children[*sptr];
- sptr++;
- }
- return currentNode;
- }
- trie_node* addChild(trie_node* node,char child,bool terminal,size_t &size) {
- trie_node* newNode = new trie_node;
- newNode->payload = child;
- newNode->is_terminal = terminal;
- newNode->parent = node;
- if(terminal) {
- size++;
- }
- node->children[child] = newNode;
- hasAnotherWord = true;
- return newNode;
- }
- bool trie::contains(const std::string& str) const {
- if(str.empty() && m_root->is_terminal == true) {
- return true;
- } else if(str.empty()) {
- return false;
- }
- try {
- trie_node* node = find(str,m_root);
- if(node->is_terminal) {
- return true;
- } else {
- return false;
- }
- }catch(int e) {
- if(e ==20) {
- return false;
- }
- }
- return false;
- }
- bool trie::insert(const std::string& str) {
- if(contains(str)) {
- return false;
- } else {
- if(str.empty()) {
- m_root->is_terminal = true;
- m_size++;
- return true;
- }
- trie_node* currentNode = m_root;
- const char* sptr = str.c_str();
- int length = 1;
- while (*sptr) {
- if(currentNode->children[*sptr] == nullptr) {
- currentNode = addChild(currentNode,*sptr,length==str.length(),m_size);
- length++;
- }else {
- currentNode = currentNode->children[*sptr];
- if(length == str.length()) {
- currentNode->is_terminal = true;
- m_size++;
- }
- length++;
- }
- sptr++;
- }
- return true;
- }
- }
- void deallocate_node(trie_node* node) {
- for(int i = 0;i<128;i++) {
- if(node->children[i] != nullptr) {
- deallocate_node(node->children[i]);
- }
- }
- delete node;
- }
- bool trie::erase(const std::string& str) {
- try {
- trie_node* nodeToErase = find(str,m_root);
- if(nodeToErase->is_terminal == true) {
- nodeToErase->is_terminal = false;
- m_size--;
- } else {
- return false;
- }
- }catch(int e) {
- if(e == 20)
- return false;
- }
- return true;
- }
- std::vector<std::string> trie::search_by_prefix(const std::string& prefix) const {
- std::vector<std::string> foundWords;
- trie_node* currentNode = find(prefix,m_root);
- word_cursor* cursor = new word_cursor(currentNode);
- if(!currentNode->is_terminal) {
- cursor->move_to_next_word();
- }
- while(cursor->has_word()) {
- foundWords.push_back(cursor->read_word());
- cursor->move_to_next_word();
- }
- delete cursor;
- return foundWords;
- }
- std::vector<std::string> trie::get_prefixes(const std::string& str) const {
- std::vector<std::string> prefixes;
- trie_node* currentNode;
- std::string workingString = str;
- getprefixes:
- try {
- currentNode = find(workingString,m_root);
- } catch(int e) {
- if(e ==20) {
- workingString = workingString.substr(0,workingString.length()-2);
- goto getprefixes;
- }
- }
- int index = 0;
- while(true) {
- if(currentNode->is_terminal) {
- prefixes.push_back(workingString.substr(0,workingString.length()-index));
- }
- currentNode = currentNode->parent;
- if(currentNode == m_root) {
- break;
- }
- index++;
- }
- return prefixes;
- }
- word_cursor trie::get_word_cursor() const {
- word_cursor cursor = word_cursor(m_root);
- if(!m_root->is_terminal) {
- cursor.move_to_next_word();
- }
- return cursor;
- }
- size_t trie::size() const {
- return m_size;
- }
- bool trie::empty() const {
- return m_size == 0;
- }
- trie::trie(const std::vector<std::string>& strings) :
- m_root(new trie_node)
- {
- for(size_t i= 0;i<strings.size();i++) {
- insert(strings[i]);
- }
- }
- trie::trie() :
- m_root(new trie_node) {
- }
- trie::~trie() {
- deallocate_node(m_root);
- m_size = 0;
- }
- word_cursor::word_cursor(const trie_node* word_ptr):
- m_ptr(word_ptr){
- }
- bool word_cursor::has_word() const {
- return hasAnotherWord;
- }
- std::string word_cursor::read_word() const {
- const trie_node* currentNode = m_ptr;
- std::string word = "";
- while(true) {
- if(currentNode->payload == '\0') {
- break;
- }
- word += currentNode->payload;
- currentNode = currentNode->parent;
- }
- std::reverse(word.begin(),word.end());
- return word;
- }
- void word_cursor::move_to_next_word() {
- nextword:
- std::string word = read_word();
- for(int i = 0;i<128;i++) {
- if(m_ptr->children[i] != nullptr) {
- m_ptr = m_ptr->children[i];
- if(!m_ptr->is_terminal) {
- goto nextword;
- return;
- } else if(m_ptr->is_terminal){
- return;
- }
- }
- }
- int index = 1;
- while(m_ptr->payload != '\0') {
- m_ptr = m_ptr->parent;
- // m_ptr = return_to_root(m_ptr);
- for(int i = 0;i<128;i++) {
- if(m_ptr->children[i] != nullptr) {
- if(m_ptr->children[i]->payload <= word[word.length()-index]) {
- continue;
- } else if(!m_ptr->children[i]->is_terminal) {
- m_ptr = m_ptr->children[i];
- goto nextword;
- } else if(m_ptr->children[i]->is_terminal) {
- m_ptr = m_ptr->children[i];
- return;
- }
- }
- }
- index++;
- }
- hasAnotherWord = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement