TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
Trie Class Reference
Collaboration diagram for Trie:
[legend]

Classes

struct  TrieNode
 

Public Member Functions

 Trie ()
 constructor to initialise the root of the trie.
 
void insert (const std::string &word)
 
bool search (const std::string &word)
 
void removeWord (const std::string &word)
 

Private Member Functions

std::shared_ptr< TrieNoderemoveWordHelper (const std::string &word, std::shared_ptr< TrieNode > curr, size_t index)
 

Static Private Member Functions

static bool hasChildren (std::shared_ptr< TrieNode > node)
 

Private Attributes

std::shared_ptr< TrieNodem_root
 

Static Private Attributes

static constexpr size_t ALPHABETS = 26
 

Detailed Description

A basic implementation of trie class to store only lower-case strings. You can extend the implementation to all the ASCII characters by changing the value of @ ALPHABETS to 128.

Definition at line 16 of file trie_modern.cpp.

Constructor & Destructor Documentation

◆ Trie()

Trie::Trie ( )
inline

constructor to initialise the root of the trie.

Definition at line 103 of file trie_modern.cpp.

103: m_root(std::make_shared<TrieNode>()) {}

Member Function Documentation

◆ hasChildren()

static bool Trie::hasChildren ( std::shared_ptr< TrieNode > node)
inlinestaticprivate

Function to check if a node has some children which can form words.

Parameters
nodewhose character array of pointers need to be checked for children.
Returns
true if a child is found
false if a child is not found

Definition at line 41 of file trie_modern.cpp.

41 {
42 for (size_t i = 0; i < ALPHABETS; i++) {
43 if (node->character[i]) {
44 return true;
45 }
46 }
47 return false;
48 }

◆ insert()

void Trie::insert ( const std::string & word)
inline

Insert a word into the trie.

Parameters
wordwhich needs to be inserted into the string.

Definition at line 109 of file trie_modern.cpp.

109 {
110 auto curr = m_root;
111 for (char ch : word) {
112 size_t index = ch - 'a';
113
114 // if a node for current word is not already present in trie, create
115 // a new node for it.
116 if (!curr->character[index]) {
117 curr->character[index] = std::make_shared<TrieNode>();
118 }
119
120 curr = curr->character[index];
121 }
122 curr->isEndOfWord = true;
123 }

◆ removeWord()

void Trie::removeWord ( const std::string & word)
inline

Definition at line 148 of file trie_modern.cpp.

148 {
149 m_root = removeWordHelper(word, m_root, 0);
150 }
std::shared_ptr< TrieNode > removeWordHelper(const std::string &word, std::shared_ptr< TrieNode > curr, size_t index)

◆ removeWordHelper()

std::shared_ptr< TrieNode > Trie::removeWordHelper ( const std::string & word,
std::shared_ptr< TrieNode > curr,
size_t index )
inlineprivate

A recursive helper function to remove a word from the trie. First, it recursively traverses to the location of last character of word in the trie. However, if the word is not found, the function returns a runtime error. Upon successfully reaching the last character of word in trie, if sets the isEndOfWord to false and deletes the node if and only if it has no children, else it returns the current node.

Parameters
wordis the string which needs to be removed from trie.
curris the current node we are at.
indexis the index of the @word we are at.
Returns
if current node has childern, it returns @ curr, else it returns nullptr.
Exceptions
aruntime error in case @ word is not found in the trie.

Definition at line 64 of file trie_modern.cpp.

66 {
67 if (word.size() == index) {
68 if (curr->isEndOfWord) {
69 curr->isEndOfWord = false;
70 }
71 if (hasChildren(curr)) {
72 return curr;
73 }
74 return nullptr;
75 }
76
77 size_t idx = word[index] - 'a';
78
79 // Throw a runtime error in case the user enters a word which is not
80 // present in the trie.
81 if (!curr->character[idx]) {
82 throw std::runtime_error(std::move(std::string("Word not found.")));
83 }
84
85 curr->character[idx] =
86 removeWordHelper(word, curr->character[idx], index + 1);
87
88 // This if condition checks if the node has some childern.
89 // The 1st if check, i.e. (curr->character[idx]) is checked specifically
90 // because if the older string is a prefix of some other string, then,
91 // there would be no need to check all 26 characters. Example- str1 =
92 // abbey, str2 = abbex and we want to delete string "abbey", then in
93 // this case, there would be no need to check all characters for the
94 // chars a,b,b.
95 if (curr->character[idx] || hasChildren(curr)) {
96 return curr;
97 }
98 return nullptr;
99 }
static bool hasChildren(std::shared_ptr< TrieNode > node)

◆ search()

bool Trie::search ( const std::string & word)
inline

Search if a word is present in trie or not.

Parameters
wordwhich is needed to be searched in the trie.
Returns
True if the word is found in trie and isEndOfWord is set to true.
False if word is not found in trie or isEndOfWord is set to false.

Definition at line 132 of file trie_modern.cpp.

132 {
133 auto curr = m_root;
134 for (char ch : word) {
135 size_t index = ch - 'a';
136
137 // if any node for a character is not found, then return that the
138 // word cannot be formed.
139 if (!curr->character[index]) {
140 return false;
141 }
142 curr = curr->character[index];
143 }
144 return curr->isEndOfWord;
145 }

Member Data Documentation

◆ ALPHABETS

size_t Trie::ALPHABETS = 26
staticconstexprprivate

Definition at line 18 of file trie_modern.cpp.

◆ m_root

std::shared_ptr<TrieNode> Trie::m_root
private

Definition at line 154 of file trie_modern.cpp.


The documentation for this class was generated from the following file: