-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionInTries.java
More file actions
40 lines (34 loc) · 1.03 KB
/
Copy pathInsertionInTries.java
File metadata and controls
40 lines (34 loc) · 1.03 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
public class InsertionInTries {
static class Node{
Node[] children;
boolean isTerminal;
public Node(){
children = new Node[26];
for(int i=0;i<26;i++){
children[i] = null;
}
isTerminal = false;
}
}
static Node root = new Node();
static void insert(String word){
Node current = root;
for(int i=0;i<word.length(); i++){
int index = word.charAt(i) - 'a';
if(current.children[index] == null){
current.children[index] = new Node();
}
if(i==word.length()-1){
current.children[index].isTerminal = true;
System.out.println("WORD IS INSERTED : " + word );
}
current = current.children[index];
}
}
public static void main(String[] args) {
String arr[] ={"bag","apple","cat"};
for(String i:arr){
insert(i);
}
}
}