-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathTrie.java
202 lines (172 loc) · 5.54 KB
/
Trie.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.thealgorithms.datastructures.trees;
import java.util.HashMap;
/**
* Represents a Trie Node that stores a character and pointers to its children.
* Each node has a hashmap which can point to all possible characters.
* Each node also has a boolean value to indicate if it is the end of a word.
*/
class TrieNode {
char value;
HashMap<Character, TrieNode> child;
boolean end;
/**
* Constructor to initialize a TrieNode with an empty hashmap
* and set end to false.
*/
TrieNode(char value) {
this.value = value;
this.child = new HashMap<>();
this.end = false;
}
}
/**
* Trie Data structure implementation without any libraries.
* <p>
* The Trie (also known as a prefix tree) is a special tree-like data structure
* that is used to store a dynamic set or associative array where the keys are
* usually strings. It is highly efficient for prefix-based searches.
* <p>
* This implementation supports basic Trie operations like insertion, search,
* and deletion.
* <p>
* Each node of the Trie represents a character and has child nodes for each
* possible character.
*
* @author <a href="https://github.com/dheeraj92">Dheeraj Kumar Barnwal</a>
* @author <a href="https://github.com/sailok">Sailok Chinta</a>
*/
public class Trie {
private static final char ROOT_NODE_VALUE = '*';
private final TrieNode root;
/**
* Constructor to initialize the Trie.
* The root node is created but doesn't represent any character.
*/
public Trie() {
root = new TrieNode(ROOT_NODE_VALUE);
}
/**
* Inserts a word into the Trie.
* <p>
* The method traverses the Trie from the root, character by character, and adds
* nodes if necessary. It marks the last node of the word as an end node.
*
* @param word The word to be inserted into the Trie.
*/
public void insert(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
TrieNode node = currentNode.child.getOrDefault(word.charAt(i), null);
if (node == null) {
node = new TrieNode(word.charAt(i));
currentNode.child.put(word.charAt(i), node);
}
currentNode = node;
}
currentNode.end = true;
}
/**
* Searches for a word in the Trie.
* <p>
* This method traverses the Trie based on the input word and checks whether
* the word exists. It returns true if the word is found and its end flag is
* true.
*
* @param word The word to search in the Trie.
* @return true if the word exists in the Trie, false otherwise.
*/
public boolean search(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
TrieNode node = currentNode.child.getOrDefault(word.charAt(i), null);
if (node == null) {
return false;
}
currentNode = node;
}
return currentNode.end;
}
/**
* Deletes a word from the Trie.
* <p>
* The method traverses the Trie to find the word and marks its end flag as
* false.
* It returns true if the word was successfully deleted, false if the word
* wasn't found.
*
* @param word The word to be deleted from the Trie.
* @return true if the word was found and deleted, false if it was not found.
*/
public boolean delete(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
TrieNode node = currentNode.child.getOrDefault(word.charAt(i), null);
if (node == null) {
return false;
}
currentNode = node;
}
if (currentNode.end) {
currentNode.end = false;
return true;
}
return false;
}
/**
* Counts the number of words in the trie
*<p>
* The method traverses the Trie and counts the number of words.
*
* @return count of words
*/
public int countWords() {
return countWords(root);
}
private int countWords(TrieNode node) {
if (node == null) {
return 0;
}
int count = 0;
if (node.end) {
count++;
}
for (TrieNode child : node.child.values()) {
count += countWords(child);
}
return count;
}
/**
* Check if the prefix exists in the trie
*
* @param prefix the prefix to be checked in the Trie
* @return true / false depending on the prefix if exists in the Trie
*/
public boolean startsWithPrefix(String prefix) {
TrieNode currentNode = root;
for (int i = 0; i < prefix.length(); i++) {
TrieNode node = currentNode.child.getOrDefault(prefix.charAt(i), null);
if (node == null) {
return false;
}
currentNode = node;
}
return true;
}
/**
* Count the number of words starting with the given prefix in the trie
*
* @param prefix the prefix to be checked in the Trie
* @return count of words
*/
public int countWordsWithPrefix(String prefix) {
TrieNode currentNode = root;
for (int i = 0; i < prefix.length(); i++) {
TrieNode node = currentNode.child.getOrDefault(prefix.charAt(i), null);
if (node == null) {
return 0;
}
currentNode = node;
}
return countWords(currentNode);
}
}