Skip to content

Commit

Permalink
Trie: Add rbeing memeber function
Browse files Browse the repository at this point in the history
  • Loading branch information
akshitgrover committed May 5, 2019
1 parent 6788428 commit 7d07f73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class trie {
bool exist(std::string);
bool empty();
iterator begin();
iterator rbegin();
private:
tnode<T>* root;
int size;
Expand Down
25 changes: 25 additions & 0 deletions trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,28 @@ typename trie<T>::iterator trie<T>::begin() {
trie_iterator<T> it = *(new trie_iterator<T>(this->root));
return ++it;
}

template <typename T>
tnode<T>* rbrecur(tnode<T>* n, int offset = 127, tnode<T>* r = nullptr) {
tnode<T>* it = nullptr;
for (int i = offset; i > -1; i--) {
it = n->getChild(i);
if (it == nullptr) {
if (i == 0) {
return r;
}
continue;
}
if (it->isEnd()) {
r = it;
}
return rbrecur(it, 127, r);
}
}

template <typename T>
typename trie<T>::iterator trie<T>::rbegin() {
tnode<T>* t = rbrecur(this->root);
trie_iterator<T> it = *(new trie_iterator<T>(t));
return it;
}

0 comments on commit 7d07f73

Please sign in to comment.