Skip to content

Commit 441de52

Browse files
Merge pull request TheAlgorithms#140 from dheeraj92/master
Trie Data structure
2 parents ee5136c + 077b763 commit 441de52

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

data_structures/Trees/TrieImp.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//Trie Data structure implementation without any libraries */
2+
3+
/**
4+
*
5+
* @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92)
6+
*
7+
*/
8+
import java.util.Scanner;
9+
10+
public class TrieImp {
11+
12+
public class TrieNode {
13+
TrieNode[] child;
14+
boolean end;
15+
16+
public TrieNode(){
17+
child = new TrieNode[26];
18+
end = false;
19+
}
20+
}
21+
private final TrieNode root;
22+
public TrieImp(){
23+
root = new TrieNode();
24+
}
25+
26+
public void insert(String word){
27+
TrieNode currentNode = root;
28+
for(int i=0; i < word.length();i++){
29+
TrieNode node = currentNode.child[word.charAt(i)-'a'];
30+
if(node == null){
31+
node = new TrieNode();
32+
currentNode.child[word.charAt(i)-'a']=node;
33+
}
34+
currentNode = node;
35+
}
36+
currentNode.end = true;
37+
}
38+
public boolean search(String word){
39+
TrieNode currentNode = root;
40+
for(int i=0;i<word.length();i++){
41+
char ch = word.charAt(i);
42+
TrieNode node = currentNode.child[ch-'a'];
43+
if(node == null){
44+
return false;
45+
}
46+
currentNode = node;
47+
}
48+
return currentNode.end;
49+
}
50+
51+
public boolean delete(String word){
52+
TrieNode currentNode = root;
53+
for(int i=0;i<word.length();i++){
54+
char ch = word.charAt(i);
55+
TrieNode node = currentNode.child[ch-'a'];
56+
if(node == null){
57+
return false;
58+
}
59+
currentNode = node;
60+
}
61+
if(currentNode.end == true){
62+
currentNode.end = false;
63+
return true;
64+
}
65+
return false;
66+
}
67+
68+
public static void sop(String print){
69+
System.out.println(print);
70+
}
71+
72+
//Regex to check if word contains only a-z character
73+
public static boolean isValid(String word){
74+
return word.matches("^[a-z]+$");
75+
}
76+
77+
public static void main(String[] args) {
78+
TrieImp obj = new TrieImp();
79+
String word;
80+
@SuppressWarnings("resource")
81+
Scanner scan = new Scanner(System.in);
82+
sop("string should contain only a-z character for all operation");
83+
while(true){
84+
sop("1. Insert\n2. Search\n3. Delete\n4. Quit");
85+
try{
86+
int t = scan.nextInt();
87+
switch (t) {
88+
case 1:
89+
word = scan.next();
90+
if(isValid(word))
91+
obj.insert(word);
92+
else
93+
sop("Invalid string: allowed only a-z");
94+
break;
95+
case 2:
96+
word = scan.next();
97+
boolean resS=false;
98+
if(isValid(word))
99+
resS = obj.search(word);
100+
else
101+
sop("Invalid string: allowed only a-z");
102+
if(resS)
103+
sop("word found");
104+
else
105+
sop("word not found");
106+
break;
107+
case 3:
108+
word = scan.next();
109+
boolean resD=false;
110+
if(isValid(word))
111+
resD = obj.delete(word);
112+
else
113+
sop("Invalid string: allowed only a-z");
114+
if(resD){
115+
sop("word got deleted successfully");
116+
}else{
117+
sop("word not found");
118+
}
119+
break;
120+
case 4:
121+
sop("Quit successfully");
122+
System.exit(1);
123+
break;
124+
default:
125+
sop("Input int from 1-4");
126+
break;
127+
}
128+
}catch(Exception e){
129+
String badInput = scan.next();
130+
sop("This is bad input: " + badInput);
131+
}
132+
}
133+
}
134+
135+
}

0 commit comments

Comments
 (0)