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