Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
129 lines (117 loc) · 3.24 KB

File metadata and controls

129 lines (117 loc) · 3.24 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
129
package DataStructures.Trees;
/**
* Trie Data structure implementation without any libraries
*
* @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92)
*/
import java.util.Scanner;
public class TrieImp {
public class TrieNode {
TrieNode[] child;
boolean end;
public TrieNode() {
child = new TrieNode[26];
end = false;
}
}
private final TrieNode root;
public TrieImp() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
TrieNode node = currentNode.child[word.charAt(i) - 'a'];
if (node == null) {
node = new TrieNode();
currentNode.child[word.charAt(i) - 'a'] = 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[ch - 'a'];
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[ch - 'a'];
if (node == null) {
return false;
}
currentNode = node;
}
if (currentNode.end == true) {
currentNode.end = false;
return true;
}
return false;
}
public static void sop(String print) {
System.out.println(print);
}
/** Regex to check if word contains only a-z character */
public static boolean isValid(String word) {
return word.matches("^[a-z]+$");
}
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();
if (isValid(word)) obj.insert(word);
else sop("Invalid string: allowed only a-z");
break;
case 2:
word = scan.next();
boolean resS = false;
if (isValid(word)) resS = obj.search(word);
else sop("Invalid string: allowed only a-z");
if (resS) sop("word found");
else sop("word not found");
break;
case 3:
word = scan.next();
boolean resD = false;
if (isValid(word)) resD = obj.delete(word);
else sop("Invalid string: allowed only a-z");
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);
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.