-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
36 lines (33 loc) · 791 Bytes
/
Trie.cpp
File metadata and controls
36 lines (33 loc) · 791 Bytes
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
#include "Trie.h"
#include "Node.h"
Trie::Trie(){
//Set the root node as a new node
root = getNode();
}
void Trie::insert(string key){
Node *pCrawl = root;
for(int i = 0;i < key.length();i++){
int index = key[i] - 'a';
if(!pCrawl->getChild(index))
pCrawl->setChild(index);
pCrawl = pCrawl->getChild(index);
}
pCrawl->setEndOfWord();
}
bool Trie::search(string key){
Node* pCrawl = root;
for(int i = 0;i < key.length();i++){
int index = key[i] - 'a';
if(!pCrawl->getChild(index))
return false;
pCrawl = pCrawl->getChild(index);
}
return (pCrawl->isEndOfWord());
}
Node* Trie::getNode(){
Node* temp = new Node();
return temp;
}
Node* Trie::getRoot(){
return root;
}