Skip to content

Commit

Permalink
Trie: Add exist member function
Browse files Browse the repository at this point in the history
  • Loading branch information
akshitgrover committed May 2, 2019
1 parent 9c2be47 commit 4e02b21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class trie {
public:
trie();
void insert(std::string, T);
bool exist(std::string);
private:
tnode<T>* root;
int size;
Expand Down
20 changes: 20 additions & 0 deletions trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ void trie<T>::insert(std::string key, T val) {
node->update(val);
node->markEnd();
}

template <typename T>
bool trie<T>::exist(std::string key) {
int ascii;
bool res = true;
tnode<T>* node = this->root;
sIter(key, it) {
ascii = (int)*it;
if (node->getChild(ascii) == nullptr) {
res = false;
break;
} else {
node = node->getChild(ascii);
}
}
if (!node->isEnd()) {
res = false;
}
return res;
}

0 comments on commit 4e02b21

Please sign in to comment.