-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathTrie.java
More file actions
78 lines (69 loc) · 1.68 KB
/
Trie.java
File metadata and controls
78 lines (69 loc) · 1.68 KB
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
import java.util.Scanner;
import java.util.Stack;
class Node{
boolean word_end;
Node[] children = new Node[26];
Node(){
word_end = false;
for(int i = 0;i < 26;i++){
children[i] = null;
}
}
}
class Trie{
private static Node root;
static void insert(String str){
Node current = root;
for(int i = 0;i < str.length();i++){
char ch = str.charAt(i);
if(current.children[ch-'a'] == null){
current.children[ch-'a'] = new Node();
}
current = current.children[ch-'a'];
}
current.word_end = true;
System.out.println("String inserted");
}
static void search(String str){
Node current = root;
for(int i = 0;i < str.length();i++){
char ch = str.charAt(i);
if(current.children[ch-'a'] == null){
System.out.println("Word does not exist");
return;
}
else{
current = current.children[ch-'a'];
}
}
if(current.word_end){
System.out.println("Word found");
}
else{
System.out.println("Word does not exist");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
root = new Node();
while(choice != 3){
System.out.println("Select one operation : 1) Insert\t2) Search\t3) Exit");
choice = sc.nextInt();
switch(choice){
case 1:
System.out.print("Enter a string to insert - ");
String str = sc.next();
insert(str);
break;
case 2:
System.out.print("Enter a string to search -");
String st = sc.next();
search(st);
break;
default:
break;
}
}
}
}