Skip to content

Commit ab5bb83

Browse files
author
sailok.chinta
committed
feat: extend Trie implementation from lowercase english alphabets to all ascii characters
1 parent 013d122 commit ab5bb83

File tree

1 file changed

+19
-34
lines changed
  • src/main/java/com/thealgorithms/datastructures/trees

1 file changed

+19
-34
lines changed

src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.datastructures.trees;
22

3+
import java.util.HashMap;
34
import java.util.Scanner;
45

56
/**
@@ -8,31 +9,33 @@
89
* @author <a href="https://github.com/dheeraj92">Dheeraj Kumar Barnwal</a>
910
*/
1011
public class TrieImp {
11-
1212
public class TrieNode {
13-
14-
TrieNode[] child;
13+
char value;
14+
HashMap<Character, TrieNode> child;
1515
boolean end;
1616

17-
public TrieNode() {
18-
child = new TrieNode[26];
17+
public TrieNode(char value) {
18+
this.value = value;
19+
child = new HashMap<>();
1920
end = false;
2021
}
2122
}
2223

24+
private static final char ROOT_NODE_VALUE = '*';
25+
2326
private final TrieNode root;
2427

2528
public TrieImp() {
26-
root = new TrieNode();
29+
root = new TrieNode(ROOT_NODE_VALUE);
2730
}
2831

2932
public void insert(String word) {
3033
TrieNode currentNode = root;
3134
for (int i = 0; i < word.length(); i++) {
32-
TrieNode node = currentNode.child[word.charAt(i) - 'a'];
35+
TrieNode node = currentNode.child.getOrDefault(word.charAt(i), null);
3336
if (node == null) {
34-
node = new TrieNode();
35-
currentNode.child[word.charAt(i) - 'a'] = node;
37+
node = new TrieNode(word.charAt(i));
38+
currentNode.child.put(word.charAt(i), node);
3639
}
3740
currentNode = node;
3841
}
@@ -43,7 +46,7 @@ public boolean search(String word) {
4346
TrieNode currentNode = root;
4447
for (int i = 0; i < word.length(); i++) {
4548
char ch = word.charAt(i);
46-
TrieNode node = currentNode.child[ch - 'a'];
49+
TrieNode node = currentNode.child.getOrDefault(ch, null);
4750
if (node == null) {
4851
return false;
4952
}
@@ -56,7 +59,7 @@ public boolean delete(String word) {
5659
TrieNode currentNode = root;
5760
for (int i = 0; i < word.length(); i++) {
5861
char ch = word.charAt(i);
59-
TrieNode node = currentNode.child[ch - 'a'];
62+
TrieNode node = currentNode.child.getOrDefault(ch, null);
6063
if (node == null) {
6164
return false;
6265
}
@@ -73,13 +76,6 @@ public static void sop(String print) {
7376
System.out.println(print);
7477
}
7578

76-
/**
77-
* Regex to check if word contains only a-z character
78-
*/
79-
public static boolean isValid(String word) {
80-
return word.matches("^[a-z]+$");
81-
}
82-
8379
public static void main(String[] args) {
8480
TrieImp obj = new TrieImp();
8581
String word;
@@ -92,20 +88,12 @@ public static void main(String[] args) {
9288
switch (t) {
9389
case 1:
9490
word = scan.next();
95-
if (isValid(word)) {
96-
obj.insert(word);
97-
} else {
98-
sop("Invalid string: allowed only a-z");
99-
}
91+
obj.insert(word);
10092
break;
10193
case 2:
10294
word = scan.next();
103-
boolean resS = false;
104-
if (isValid(word)) {
105-
resS = obj.search(word);
106-
} else {
107-
sop("Invalid string: allowed only a-z");
108-
}
95+
boolean resS = obj.search(word);
96+
10997
if (resS) {
11098
sop("word found");
11199
} else {
@@ -115,11 +103,8 @@ public static void main(String[] args) {
115103
case 3:
116104
word = scan.next();
117105
boolean resD = false;
118-
if (isValid(word)) {
119-
resD = obj.delete(word);
120-
} else {
121-
sop("Invalid string: allowed only a-z");
122-
}
106+
resD = obj.delete(word);
107+
123108
if (resD) {
124109
sop("word got deleted successfully");
125110
} else {

0 commit comments

Comments
 (0)