FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DataStructures/Trees/TrieImp.java at master · java66liu/Java · GitHub
java66liu
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
DataStructures
/
Trees
/
TrieImp.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
141 lines (127 loc) · 4.17 KB
Breadcrumbs
Java
/
DataStructures
/
Trees
/
TrieImp.java
Copy path
File metadata and controls
141 lines (127 loc) · 4.17 KB
Raw
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
130
131
132
133
134
135
136
137
138
139
140
141
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
\n
2. Search
\n
3. Delete
\n
4. 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
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL