From 7d07f73f4da5338e0057ac184aa4b2935511c89a Mon Sep 17 00:00:00 2001 From: Akshit Grover Date: Sun, 5 May 2019 13:42:23 +0530 Subject: [PATCH] Trie: Add rbeing memeber function --- trie.h | 1 + trie.hpp | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/trie.h b/trie.h index aa64d2b..a972b94 100644 --- a/trie.h +++ b/trie.h @@ -12,6 +12,7 @@ class trie { bool exist(std::string); bool empty(); iterator begin(); + iterator rbegin(); private: tnode* root; int size; diff --git a/trie.hpp b/trie.hpp index 00819ca..fb406f0 100644 --- a/trie.hpp +++ b/trie.hpp @@ -62,3 +62,28 @@ typename trie::iterator trie::begin() { trie_iterator it = *(new trie_iterator(this->root)); return ++it; } + +template +tnode* rbrecur(tnode* n, int offset = 127, tnode* r = nullptr) { + tnode* 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 trie::iterator trie::rbegin() { + tnode* t = rbrecur(this->root); + trie_iterator it = *(new trie_iterator(t)); + return it; +}