TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
data_structures::trie Class Reference

Trie implementation for small-case English alphabets a-z More...

Public Member Functions

 trie ()=default
 Class default constructor.
 
void insert (const std::string &str)
 
bool search (const std::string &str, int index)
 
bool deleteString (const std::string &str, int index)
 

Private Member Functions

uint8_t char_to_int (const char &ch) const
 Convert a character to integer for indexing.
 
bool search (const std::shared_ptr< trie > &root, const std::string &str, int index)
 

Private Attributes

std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
 Recursive tree nodes as an array of shared-pointers.
 
bool isEndofWord = false
 identifier if a node is terminal node
 

Static Private Attributes

static constexpr uint8_t NUM_CHARS = 26
 Number of alphabets.
 

Detailed Description

Trie implementation for small-case English alphabets a-z

Definition at line 24 of file trie_tree.cpp.

Member Function Documentation

◆ char_to_int()

uint8_t data_structures::trie::char_to_int ( const char & ch) const
inlineprivate

Convert a character to integer for indexing.

Parameters
chcharacter to index
Returns
unsigned integer index

Definition at line 37 of file trie_tree.cpp.

37 {
38 if (ch >= 'A' && ch <= 'Z') {
39 return ch - 'A';
40 } else if (ch >= 'a' && ch <= 'z') {
41 return ch - 'a' + NUM_CHARS;
42 }
43
44 std::cerr << "Invalid character present. Exiting...";
45 std::exit(EXIT_FAILURE);
46 return 0;
47 }
static constexpr uint8_t NUM_CHARS
Number of alphabets.
Definition trie_tree.cpp:26

◆ deleteString()

bool data_structures::trie::deleteString ( const std::string & str,
int index )
inline

removes the string if it is not a prefix of any other string, if it is then just sets the ::data_structure::trie::isEndofWord to false, else removes the given string

Note
the function ::data_structure::trie::deleteString might be erroneous
Todo
review the function ::data_structure::trie::deleteString and the commented lines
Parameters
strstring to remove
indexindex to remove from
Returns
true if successful
false if unsuccessful

Definition at line 133 of file trie_tree.cpp.

133 {
134 if (index == str.length()) {
135 if (!isEndofWord) {
136 return false;
137 }
138 isEndofWord = false;
139 // following lines - possible source of error?
140 // for (int i = 0; i < NUM_CHARS; i++)
141 // if (!arr[i])
142 // return false;
143 return true;
144 }
145 int j = char_to_int(str[index]);
146 if (!arr[j]) {
147 return false;
148 }
149 bool var = deleteString(str, index + 1);
150 if (var) {
151 arr[j].reset();
152 if (isEndofWord) {
153 return false;
154 } else {
155 int i = 0;
156 for (i = 0; i < NUM_CHARS; i++) {
157 if (arr[i]) {
158 return false;
159 }
160 }
161 return true;
162 }
163 }
164
165 /* should not return here */
166 std::cout << __func__ << ":" << __LINE__
167 << "Should not reach this line\n";
168 return false;
169 }
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
Definition trie_tree.cpp:28
bool isEndofWord
identifier if a node is terminal node
Definition trie_tree.cpp:29
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
Definition trie_tree.cpp:37
bool deleteString(const std::string &str, int index)

◆ insert()

void data_structures::trie::insert ( const std::string & str)
inline

insert string into the trie

Parameters
strString to insert in the tree

Definition at line 76 of file trie_tree.cpp.

76 {
77 std::shared_ptr<trie> root(nullptr);
78
79 for (const char& ch : str) {
80 int j = char_to_int(ch);
81 if (root) {
82 if (root->arr[j]) {
83 root = root->arr[j];
84 } else {
85 std::shared_ptr<trie> temp(new trie());
86 root->arr[j] = temp;
87 root = temp;
88 }
89 } else if (arr[j]) {
90 root = arr[j];
91 } else {
92 std::shared_ptr<trie> temp(new trie());
93 arr[j] = temp;
94 root = temp;
95 }
96 }
97 root->isEndofWord = true;
98 }
trie()=default
Class default constructor.

◆ search() [1/2]

bool data_structures::trie::search ( const std::shared_ptr< trie > & root,
const std::string & str,
int index )
inlineprivate

search a string exists inside a given root trie

Parameters
strstring to search for
indexstart index to search from
Returns
true if found
false if not found

Definition at line 55 of file trie_tree.cpp.

56 {
57 if (index == str.length()) {
58 if (!root->isEndofWord) {
59 return false;
60 }
61 return true;
62 }
63 int j = char_to_int(str[index]);
64 if (!root->arr[j]) {
65 return false;
66 }
67 return search(root->arr[j], str, index + 1);
68 }
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
Definition trie_tree.cpp:55

◆ search() [2/2]

bool data_structures::trie::search ( const std::string & str,
int index )
inline

search a string exists inside the trie

Parameters
strstring to search for
indexstart index to search from
Returns
true if found
false if not found

Definition at line 106 of file trie_tree.cpp.

106 {
107 if (index == str.length()) {
108 if (!isEndofWord) {
109 return false;
110 }
111 return true;
112 }
113 int j = char_to_int(str[index]);
114 if (!arr[j]) {
115 return false;
116 }
117 return search(arr[j], str, index + 1);
118 }

Member Data Documentation

◆ arr

std::array<std::shared_ptr<trie>, NUM_CHARS << 1> data_structures::trie::arr
private

Recursive tree nodes as an array of shared-pointers.

Definition at line 28 of file trie_tree.cpp.

◆ isEndofWord

bool data_structures::trie::isEndofWord = false
private

identifier if a node is terminal node

Definition at line 29 of file trie_tree.cpp.

◆ NUM_CHARS

uint8_t data_structures::trie::NUM_CHARS = 26
staticconstexprprivate

Number of alphabets.

Definition at line 26 of file trie_tree.cpp.


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