1
1
package com .thealgorithms .datastructures .trees ;
2
2
3
+ import java .util .HashMap ;
3
4
import java .util .Scanner ;
4
5
5
6
/**
8
9
* @author <a href="https://github.com/dheeraj92">Dheeraj Kumar Barnwal</a>
9
10
*/
10
11
public class TrieImp {
11
-
12
12
public class TrieNode {
13
-
14
- TrieNode [] child ;
13
+ char value ;
14
+ HashMap < Character , TrieNode > child ;
15
15
boolean end ;
16
16
17
- public TrieNode () {
18
- child = new TrieNode [26 ];
17
+ public TrieNode (char value ) {
18
+ this .value = value ;
19
+ child = new HashMap <>();
19
20
end = false ;
20
21
}
21
22
}
22
23
24
+ private static final char ROOT_NODE_VALUE = '*' ;
25
+
23
26
private final TrieNode root ;
24
27
25
28
public TrieImp () {
26
- root = new TrieNode ();
29
+ root = new TrieNode (ROOT_NODE_VALUE );
27
30
}
28
31
29
32
public void insert (String word ) {
30
33
TrieNode currentNode = root ;
31
34
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 ) ;
33
36
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 ) ;
36
39
}
37
40
currentNode = node ;
38
41
}
@@ -43,7 +46,7 @@ public boolean search(String word) {
43
46
TrieNode currentNode = root ;
44
47
for (int i = 0 ; i < word .length (); i ++) {
45
48
char ch = word .charAt (i );
46
- TrieNode node = currentNode .child [ ch - 'a' ] ;
49
+ TrieNode node = currentNode .child . getOrDefault ( ch , null ) ;
47
50
if (node == null ) {
48
51
return false ;
49
52
}
@@ -56,7 +59,7 @@ public boolean delete(String word) {
56
59
TrieNode currentNode = root ;
57
60
for (int i = 0 ; i < word .length (); i ++) {
58
61
char ch = word .charAt (i );
59
- TrieNode node = currentNode .child [ ch - 'a' ] ;
62
+ TrieNode node = currentNode .child . getOrDefault ( ch , null ) ;
60
63
if (node == null ) {
61
64
return false ;
62
65
}
@@ -73,13 +76,6 @@ public static void sop(String print) {
73
76
System .out .println (print );
74
77
}
75
78
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
-
83
79
public static void main (String [] args ) {
84
80
TrieImp obj = new TrieImp ();
85
81
String word ;
@@ -92,20 +88,12 @@ public static void main(String[] args) {
92
88
switch (t ) {
93
89
case 1 :
94
90
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 );
100
92
break ;
101
93
case 2 :
102
94
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
+
109
97
if (resS ) {
110
98
sop ("word found" );
111
99
} else {
@@ -115,11 +103,8 @@ public static void main(String[] args) {
115
103
case 3 :
116
104
word = scan .next ();
117
105
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
+
123
108
if (resD ) {
124
109
sop ("word got deleted successfully" );
125
110
} else {
0 commit comments