import
java.io.*;
import
java.util.*;
class
TrieHash {
private
HashMap<Character, HashMap> origin;
public
TrieHash()
{
origin =
new
HashMap<Character, HashMap>();
}
public
TrieHash(String[] array)
{
origin =
new
HashMap<Character, HashMap>();
for
(String c : array)
attach(c);
}
public
void
attach(String str)
{
HashMap<Character, HashMap> node = origin;
int
i =
0
;
while
(i < str.length()) {
if
(node.containsKey(str.charAt(i))) {
node = node.get(str.charAt(i));
}
else
{
node.put(str.charAt(i),
new
HashMap<Character, HashMap>());
node = node.get(str.charAt(i));
}
i++;
}
node.put(
'\0'
,
new
HashMap<Character, HashMap>(
0
));
}
public
boolean
search(String str)
{
HashMap<Character, HashMap> presentNode = origin;
int
i =
0
;
while
(i < str.length()) {
if
(presentNode.containsKey(str.charAt(i))) {
presentNode
= presentNode.get(str.charAt(i));
}
else
{
return
false
;
}
i++;
}
if
(presentNode.containsKey(
'\0'
)) {
return
true
;
}
else
{
return
false
;
}
}
}
public
class
Main {
public
static
void
main(String[] args)
throws
IOException
{
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(System.in));
System.out.println(
"Enter the words separated with space to be entered into trie"
);
String input = br.readLine();
String[] c = input.split(
" "
);
TrieHash trie =
new
TrieHash(c);
System.out.println(
"How many words you have to search in the trie"
);
String count = br.readLine();
int
counts = Integer.parseInt(count);
for
(
int
i =
0
; i < counts; i++) {
System.out.println(
"\nEnter the word one by one to be searched in hash-trie "
);
String word = br.readLine();
if
(trie.search(word)) {
System.out.println(
"Word Found in the trie"
);
}
else
{
System.out.println(
"Word NOT Found in the trie!"
);
}
}
}
}