Skip to content

Commit 2cd862b

Browse files
author
sailok.chinta
committed
feat: extend Trie Class to have more characters
1 parent 2419346 commit 2cd862b

File tree

1 file changed

+18
-17
lines changed
  • src/main/java/com/thealgorithms/tries

1 file changed

+18
-17
lines changed

src/main/java/com/thealgorithms/tries/Trie.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package com.thealgorithms.tries;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
/**
4-
* TrieNode class which holds the lowercase letter and references to its child nodes
7+
* TrieNode class which holds the characters and references to its child nodes
58
*/
69
class TrieNode {
7-
private static final int CHILDREN_NODE_COUNT = 26;
8-
9-
private TrieNode[] children;
10+
private Map<Character, TrieNode> children;
1011
private char letter;
1112
private boolean end;
1213

1314
TrieNode(char letter) {
1415
this.letter = letter;
15-
this.children = new TrieNode[CHILDREN_NODE_COUNT];
16+
this.children = new HashMap<>();
1617
this.end = false;
1718
}
1819

19-
public TrieNode[] getChildren() {
20+
public Map<Character, TrieNode> getChildren() {
2021
return children;
2122
}
2223

23-
public void setChildren(TrieNode[] children) {
24+
public void setChildren(Map<Character, TrieNode> children) {
2425
this.children = children;
2526
}
2627

@@ -42,14 +43,14 @@ public void setEnd(boolean end) {
4243
}
4344

4445
/**
45-
* Trie class which holds Strings of LowerCase Sensitive characters.
46+
* Trie class which holds Strings of characters.
47+
*
4648
* <a href="https://en.wikipedia.org/wiki/Trie">Wikipedia</a>
4749
*
48-
* @author <a href="https://github.com/sailok">Sailok Chinta</a>
50+
* @author <a href="https://github.com/sailok">Sailok Chinta</a>
4951
*/
5052
public class Trie {
5153
private static final char ROOT_CHAR = '*';
52-
private static final char BASE_CHAR = 'a';
5354

5455
private final TrieNode root;
5556

@@ -68,11 +69,11 @@ public void insert(String word) {
6869
for (int i = 0; i < word.length(); i++) {
6970
char c = word.charAt(i);
7071

71-
if (head.getChildren()[c - BASE_CHAR] == null) {
72-
head.getChildren()[c - BASE_CHAR] = new TrieNode(c);
72+
if (!head.getChildren().containsKey(c)) {
73+
head.getChildren().put(c, new TrieNode(c));
7374
}
7475

75-
head = head.getChildren()[c - BASE_CHAR];
76+
head = head.getChildren().get(c);
7677
}
7778

7879
head.setEnd(true);
@@ -90,11 +91,11 @@ public boolean search(String word) {
9091
for (int i = 0; i < word.length(); i++) {
9192
char c = word.charAt(i);
9293

93-
if (head.getChildren()[c - BASE_CHAR] == null) {
94+
if (!head.getChildren().containsKey(c)) {
9495
return false;
9596
}
9697

97-
head = head.getChildren()[c - BASE_CHAR];
98+
head = head.getChildren().get(c);
9899
}
99100

100101
return head.isEnd();
@@ -112,11 +113,11 @@ public boolean startsWith(String prefix) {
112113
for (int i = 0; i < prefix.length(); i++) {
113114
char c = prefix.charAt(i);
114115

115-
if (head.getChildren()[c - BASE_CHAR] == null) {
116+
if (!head.getChildren().containsKey(c)) {
116117
return false;
117118
}
118119

119-
head = head.getChildren()[c - BASE_CHAR];
120+
head = head.getChildren().get(c);
120121
}
121122

122123
return true;

0 commit comments

Comments
 (0)