Skip to content

Commit f363114

Browse files
committed
Added Tries
1 parent 14a23b7 commit f363114

File tree

1 file changed

+125
-0
lines changed
  • src/main/java/com/thealgorithms/datastructures/tries

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.thealgorithms.datastructures.tries;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* A Trie (prefix tree) data structure implementation for storing strings.
8+
*
9+
* <p>
10+
* The Trie allows for efficient insertion, search, and prefix-matching
11+
* operations.
12+
* It is commonly used for tasks such as autocomplete, spell checking, and
13+
* dictionary storage.
14+
*
15+
* <p>
16+
* This implementation supports only lowercase English letters by default but
17+
* can
18+
* be easily extended to handle other character sets using a {@link HashMap}.
19+
*
20+
* <p>
21+
* Trie is not thread-safe and should not be accessed by multiple threads
22+
* concurrently.
23+
*/
24+
public class Trie {
25+
26+
/** The root node of the Trie. */
27+
private final TrieNode root;
28+
29+
/**
30+
* Constructs an empty Trie.
31+
* <p>
32+
* This initializes the Trie with an empty root node.
33+
*/
34+
public Trie() {
35+
root = new TrieNode();
36+
}
37+
38+
/**
39+
* Inserts a word into the Trie.
40+
*
41+
* <p>
42+
* This method adds all characters of the specified word into the Trie.
43+
* If a character path does not exist, it is created. At the end of the word,
44+
* the terminal node is marked as an end of a valid word.
45+
*
46+
* @param word the word to insert; must not be {@code null} or empty
47+
*/
48+
public void insert(String word) {
49+
TrieNode currentNode = root;
50+
for (char ch : word.toCharArray()) {
51+
currentNode.children.putIfAbsent(ch, new TrieNode());
52+
currentNode = currentNode.children.get(ch);
53+
}
54+
currentNode.isEndOfWord = true;
55+
}
56+
57+
/**
58+
* Checks whether the Trie contains a specific word.
59+
*
60+
* <p>
61+
* This method returns {@code true} only if the exact word has been inserted
62+
* into the Trie earlier.
63+
*
64+
* @param word the word to check for; must not be {@code null}
65+
* @return {@code true} if the Trie contains the specified word; {@code false}
66+
* otherwise
67+
*/
68+
public boolean search(String word) {
69+
TrieNode currentNode = root;
70+
for (char ch : word.toCharArray()) {
71+
currentNode = currentNode.children.get(ch);
72+
if (currentNode == null) {
73+
return false;
74+
}
75+
}
76+
return currentNode.isEndOfWord;
77+
}
78+
79+
/**
80+
* Checks whether there is any word in the Trie that starts with the given
81+
* prefix.
82+
*
83+
* <p>
84+
* This method is useful for prefix-based searches or autocomplete
85+
* functionality.
86+
*
87+
* @param prefix the prefix to check for; must not be {@code null}
88+
* @return {@code true} if there is at least one word starting with the prefix;
89+
* {@code false} otherwise
90+
*/
91+
public boolean startsWith(String prefix) {
92+
TrieNode currentNode = root;
93+
for (char ch : prefix.toCharArray()) {
94+
currentNode = currentNode.children.get(ch);
95+
if (currentNode == null) {
96+
return false;
97+
}
98+
}
99+
return true;
100+
}
101+
102+
/**
103+
* Represents a single node in the Trie.
104+
*
105+
* <p>
106+
* Each node maintains a mapping of character children and a flag indicating
107+
* whether it represents the end of a valid word.
108+
*/
109+
private static class TrieNode {
110+
private final Map<Character, TrieNode> children;
111+
private boolean isEndOfWord;
112+
113+
/**
114+
* Constructs an empty TrieNode.
115+
*
116+
* <p>
117+
* Initializes an empty map for child nodes and sets the end-of-word flag to
118+
* false.
119+
*/
120+
TrieNode() {
121+
children = new HashMap<>();
122+
isEndOfWord = false;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)