Skip to content

Commit b7d1d72

Browse files
Merge pull request ephremdeme#145 from ANKITAKHAN/ank
Added trie data structure in C++
2 parents f703e0c + 7e053f2 commit b7d1d72

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

trie data structure/trie.cpp

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#include <iostream>
2+
3+
// define character size
4+
#define CHAR_SIZE 128
5+
6+
// A Class representing a Trie node
7+
class Trie
8+
{
9+
public:
10+
bool isLeaf;
11+
Trie* character[CHAR_SIZE];
12+
13+
// Constructor
14+
Trie()
15+
{
16+
this->isLeaf = false;
17+
18+
for (int i = 0; i < CHAR_SIZE; i++)
19+
this->character[i] = nullptr;
20+
}
21+
22+
void insert(std::string);
23+
bool deletion(Trie*&, std::string);
24+
bool search(std::string);
25+
bool haveChildren(Trie const*);
26+
};
27+
28+
// Iterative function to insert a key in the Trie
29+
void Trie::insert(std::string key)
30+
{
31+
// start from root node
32+
Trie* curr = this;
33+
for (int i = 0; i < key.length(); i++)
34+
{
35+
// create a new node if path doesn't exists
36+
if (curr->character[key[i]] == nullptr)
37+
curr->character[key[i]] = new Trie();
38+
39+
// go to next node
40+
curr = curr->character[key[i]];
41+
}
42+
43+
// mark current node as leaf
44+
curr->isLeaf = true;
45+
}
46+
47+
// Iterative function to search a key in Trie. It returns true
48+
// if the key is found in the Trie, else it returns false
49+
bool Trie::search(std::string key)
50+
{
51+
// return false if Trie is empty
52+
if (this == nullptr)
53+
return false;
54+
55+
Trie* curr = this;
56+
for (int i = 0; i < key.length(); i++)
57+
{
58+
// go to next node
59+
curr = curr->character[key[i]];
60+
61+
// if string is invalid (reached end of path in Trie)
62+
if (curr == nullptr)
63+
return false;
64+
}
65+
66+
// if current node is a leaf and we have reached the
67+
// end of the string, return true
68+
return curr->isLeaf;
69+
}
70+
71+
// returns true if given node has any children
72+
bool Trie::haveChildren(Trie const* curr)
73+
{
74+
for (int i = 0; i < CHAR_SIZE; i++)
75+
if (curr->character[i])
76+
return true;// child found
77+
78+
return false;
79+
}
80+
81+
// Recursive function to delete a key in the Trie
82+
bool Trie::deletion(Trie*& curr, std::string key)
83+
{
84+
// return if Trie is empty
85+
if (curr == nullptr)
86+
return false;
87+
88+
// if we have not reached the end of the key
89+
if (key.length())
90+
{
91+
// recur for the node corresponding to next character in the key
92+
// and if it returns true, delete current node (if it is non-leaf)
93+
94+
if (curr != nullptr &&
95+
curr->character[key[0]] != nullptr &&
96+
deletion(curr->character[key[0]], key.substr(1)) &&
97+
curr->isLeaf == false)
98+
{
99+
if (!haveChildren(curr))
100+
{
101+
delete curr;
102+
curr = nullptr;
103+
return true;
104+
}
105+
else {
106+
return false;
107+
}
108+
}
109+
}
110+
111+
// if we have reached the end of the key
112+
if (key.length() == 0 && curr->isLeaf)
113+
{
114+
// if current node is a leaf node and don't have any children
115+
if (!haveChildren(curr))
116+
{
117+
// delete current node
118+
delete curr;
119+
curr = nullptr;
120+
121+
// delete non-leaf parent nodes
122+
return true;
123+
}
124+
125+
// if current node is a leaf node and have children
126+
else
127+
{
128+
// mark current node as non-leaf node (DON'T DELETE IT)
129+
curr->isLeaf = false;
130+
131+
// don't delete its parent nodes
132+
return false;
133+
}
134+
}
135+
136+
return false;
137+
}
138+
139+
// C++ implementation of Trie Data Structure
140+
int main()
141+
{
142+
Trie* head = new Trie();
143+
144+
head->insert("hello");
145+
std::cout << head->search("hello") << " "; // print 1
146+
147+
head->insert("helloworld");
148+
std::cout << head->search("helloworld") << " "; // print 1
149+
150+
std::cout << head->search("helll") << " "; // print 0 (Not found)
151+
152+
head->insert("hell");
153+
std::cout << head->search("hell") << " "; // print 1
154+
155+
head->insert("h");
156+
std::cout << head->search("h"); // print 1
157+
158+
std::cout << std::endl;
159+
160+
head->deletion(head, "hello");
161+
std::cout << head->search("hello") << " "; // print 0
162+
163+
std::cout << head->search("helloworld") << " "; // print 1
164+
std::cout << head->search("hell"); // print 1
165+
166+
std::cout << std::endl;
167+
168+
head->deletion(head, "h");
169+
std::cout << head->search("h") << " "; // print 0
170+
std::cout << head->search("hell") << " "; // print 1
171+
std::cout << head->search("helloworld");// print 1
172+
173+
std::cout << std::endl;
174+
175+
head->deletion(head, "helloworld");
176+
std::cout << head->search("helloworld") << " "; // print 0
177+
std::cout << head->search("hell") << " "; // print 1
178+
179+
head->deletion(head, "hell");
180+
std::cout << head->search("hell"); // print 0
181+
182+
std::cout << std::endl;
183+
184+
if (head == nullptr)
185+
std::cout << "Trie empty!!\n"; // Trie is empty now
186+
187+
std::cout << head->search("hell"); // print 0
188+
189+
return 0;
190+
}
191+
192+
/*
193+
Output:
194+
195+
1 1 0 1 1
196+
0 1 1
197+
0 1 1
198+
0 1 0
199+
Trie empty!!
200+
0
201+
202+
*/

0 commit comments

Comments
 (0)