-
Notifications
You must be signed in to change notification settings - Fork 31
/
0208-implement-trie-prefix-tree.cpp
186 lines (148 loc) · 5.49 KB
/
0208-implement-trie-prefix-tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Problem: LeetCode 208 - Implement Trie (Prefix Tree)
Description:
Implement a trie with insert, search, and startsWith methods.
Intuition:
A trie, also known as a prefix tree, is a tree-like data structure that stores a set of strings. Each node in the trie represents a prefix or a complete word. The trie allows efficient insertion, search, and prefix matching operations.
Approach:
1. TrieNode:
- Define a TrieNode class that represents each node in the trie.
- Each TrieNode has an array of pointers to child nodes, representing the 26 lowercase letters of the English alphabet.
- Each TrieNode also has a boolean flag to indicate if it represents a complete word.
2. Trie:
- Define a Trie class that contains the root of the trie.
- Implement the insert method to insert a word into the trie:
- Start from the root and iterate over each character in the word.
- For each character, check if the corresponding child node exists. If not, create a new node and link it to the current node.
- Move to the child node and repeat the process for the next character.
- After iterating through all characters, mark the last node as a complete word.
- Implement the search method to search for a word in the trie:
- Start from the root and iterate over each character in the word.
- For each character, check if the corresponding child node exists. If not, the word is not in the trie.
- Move to the child node and repeat the process for the next character.
- After iterating through all characters, check if the last node represents a complete word.
- Implement the startsWith method to check if there is any word in the trie that starts with the given prefix:
- Start from the root and iterate over each character in the prefix.
- For each character, check if the corresponding child node exists. If not, there are no words with the given prefix.
- Move to the child node and repeat the process for the next character.
- After iterating through all characters, return true, indicating that there are words with the given prefix.
Time Complexity:
- Insert: O(m), where m is the length of the word being inserted.
- Search: O(m), where m is the length of the word being searched.
- StartsWith: O(m), where m is the length of the prefix being checked.
Space Complexity:
- The space complexity is O(n*m), where n is the number of words inserted into the trie and m is the average length of the words.
*/
class TrieNode {
public:
bool isWord;
TrieNode *children[26];
TrieNode() {
isWord = false;
for (int i = 0; i < 26; i++) {
children[i] = nullptr;
}
}
};
class Trie {
private:
TrieNode *root;
public:
Trie() {
root = new TrieNode();
}
void insert(string word) {
TrieNode *node = root;
for (char c : word) {
int index = c - 'a';
if (!node->children[index]) {
node->children[index] = new TrieNode();
}
node = node->children[index];
}
node->isWord = true;
}
bool search(string word) {
TrieNode *node = root;
for (char c : word) {
int index = c - 'a';
if (!node->children[index]) {
return false;
}
node = node->children[index];
}
return node->isWord;
}
bool startsWith(string prefix) {
TrieNode *node = root;
for (char c : prefix) {
int index = c - 'a';
if (!node->children[index]) {
return false;
}
node = node->children[index];
}
return true;
}
};
/*
class Trie {
private:
// Defining TrieNode Datatype
struct TrieNode {
// Can have 26 diffenet childen because thats the amt of alphabets
TrieNode *child[26];
// To check where the word ends
bool isWord;
//Constructor to initialise values
TrieNode() {
isWord = false;
for (auto &c : child)
c = nullptr;
}
};
// Root pointer
TrieNode *root;
public:
Trie() {
root = new TrieNode();
}
void insert(string word) {
// Pointer to point the character we insert in trie
TrieNode* current = root;
for(auto i: word) {
// index of the character i.e. a = 0, z = 25
int index = i - 'a';
if(current->child[index] == NULL)
current->child[index] = new TrieNode();
// Pointing current to the character we just inserted
current = current->child[index];
}
current->isWord = true;
}
bool search(string word) {
TrieNode *current = root;
for(auto i: word) {
int index = i - 'a';
// If the child isn't there, return false. Else keep going.
if(current->child[index] == NULL)
return false;
current = current->child[index];
}
// Will return true if it's a word
return current->isWord;
}
// Same code as search but here we dont need to check if its a word
bool startsWith(string prefix) {
TrieNode *current = root;
for(auto i: prefix) {
int index = i - 'a';
// If the child isn't there, return false. Else keep going.
if(current->child[index] == NULL)
return false;
current = current->child[index];
}
return true;
}
};
*/