1
1
package com .thealgorithms .tries ;
2
2
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
3
6
/**
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
5
8
*/
6
9
class TrieNode {
7
- private static final int CHILDREN_NODE_COUNT = 26 ;
8
-
9
- private TrieNode [] children ;
10
+ private Map <Character , TrieNode > children ;
10
11
private char letter ;
11
12
private boolean end ;
12
13
13
14
TrieNode (char letter ) {
14
15
this .letter = letter ;
15
- this .children = new TrieNode [ CHILDREN_NODE_COUNT ] ;
16
+ this .children = new HashMap <>() ;
16
17
this .end = false ;
17
18
}
18
19
19
- public TrieNode [] getChildren () {
20
+ public Map < Character , TrieNode > getChildren () {
20
21
return children ;
21
22
}
22
23
23
- public void setChildren (TrieNode [] children ) {
24
+ public void setChildren (Map < Character , TrieNode > children ) {
24
25
this .children = children ;
25
26
}
26
27
@@ -42,14 +43,14 @@ public void setEnd(boolean end) {
42
43
}
43
44
44
45
/**
45
- * Trie class which holds Strings of LowerCase Sensitive characters.
46
+ * Trie class which holds Strings of characters.
47
+ *
46
48
* <a href="https://en.wikipedia.org/wiki/Trie">Wikipedia</a>
47
49
*
48
- * @author <a href="https://github.com/sailok">Sailok Chinta</a>
50
+ * @author <a href="https://github.com/sailok">Sailok Chinta</a>
49
51
*/
50
52
public class Trie {
51
53
private static final char ROOT_CHAR = '*' ;
52
- private static final char BASE_CHAR = 'a' ;
53
54
54
55
private final TrieNode root ;
55
56
@@ -68,11 +69,11 @@ public void insert(String word) {
68
69
for (int i = 0 ; i < word .length (); i ++) {
69
70
char c = word .charAt (i );
70
71
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 ) );
73
74
}
74
75
75
- head = head .getChildren ()[ c - BASE_CHAR ] ;
76
+ head = head .getChildren (). get ( c ) ;
76
77
}
77
78
78
79
head .setEnd (true );
@@ -90,11 +91,11 @@ public boolean search(String word) {
90
91
for (int i = 0 ; i < word .length (); i ++) {
91
92
char c = word .charAt (i );
92
93
93
- if (head .getChildren ()[ c - BASE_CHAR ] == null ) {
94
+ if (! head .getChildren (). containsKey ( c ) ) {
94
95
return false ;
95
96
}
96
97
97
- head = head .getChildren ()[ c - BASE_CHAR ] ;
98
+ head = head .getChildren (). get ( c ) ;
98
99
}
99
100
100
101
return head .isEnd ();
@@ -112,11 +113,11 @@ public boolean startsWith(String prefix) {
112
113
for (int i = 0 ; i < prefix .length (); i ++) {
113
114
char c = prefix .charAt (i );
114
115
115
- if (head .getChildren ()[ c - BASE_CHAR ] == null ) {
116
+ if (! head .getChildren (). containsKey ( c ) ) {
116
117
return false ;
117
118
}
118
119
119
- head = head .getChildren ()[ c - BASE_CHAR ] ;
120
+ head = head .getChildren (). get ( c ) ;
120
121
}
121
122
122
123
return true ;
0 commit comments