forked from loopccoew/Buffer_2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrie.java
More file actions
164 lines (140 loc) · 3.44 KB
/
Trie.java
File metadata and controls
164 lines (140 loc) · 3.44 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package logic;
import java.util.Scanner;
public class Trie
{
private Node root;
public Trie()
{
root=new Node('\0');
}
public Node getRoot() {
return root;
}
public void insert(String word,String meaning)
{
Node curr=root;
for(int i=0;i<word.length();i++)
{
char ch=word.charAt(i);
if(curr.children[ch-'a']==null)
{
curr.children[ch-'a']=new Node(ch);
}
curr=curr.children[ch-'a'];
}
curr.isWord=true;
curr.meaning=meaning;
}
public Boolean search(String word)
{
boolean isPresent= false;
isPresent = ((getNode(word)!=null)&&(getNode(word).isWord));
if(isPresent)
{
System.out.println("\n Word found!!\n");
System.out.print(" Meaning of " + word + " is : ");
System.out.println(getNode(word).meaning);
}
return isPresent;
}
public void display(Node root,String word)
{
if(root == null)
{
return;
}
if(root.isWord)
{
//word=word+root.c;
System.out.println("\n" + word+" : "+root.meaning);
}
for(int i=0;i<26;i++)
{
if(root.children[i] != null)
{
word = word + root.children[i].c;
display(root.children[i], word);
word = word.substring(0, word.length()-1);
}
}
}
private Node getNode(String word)
{
Node curr=root;
for(int i=0;i<word.length();i++)
{
char a=word.charAt(i);
if(curr.children[a-'a']==null)
{
return null;
}
curr=curr.children[a-'a'];
}
return curr;
}
public Boolean delete(String word)
{
boolean isPresent = false;
isPresent = ((getNode(word)!=null)&&(getNode(word).isWord));
if(isPresent)
{
deleteWord(root, word, 0);
return true;
}
else
{
return false;
}
}
private Node deleteWord(Node node, String word, int i)
{
if (i == word.length()) //if end of word is reached
{
node.isWord = false;
}
else
{
int index = word.charAt(i) - 'a';
node.children[index] = deleteWord(node.children[index], word, i + 1);
}
if (node.isWord)
{
return node;
}
for(int n=0;n<26;n++)
{
if(node.children[n] != null)
{
return node;
}
}
return null;
}
public void update(Node root,String word)
{
boolean isPresent=((getNode(word)!=null)&&(getNode(word).isWord));
if(isPresent)
{
String newMeaning;
Scanner sc = new Scanner(System.in);
System.out.print("\n Enter the updated meaning : ");
newMeaning=sc.nextLine();
getNode(word).setMeaning(newMeaning);
System.out.println(" Word updated");
//sc.close();
}
else
{
System.out.println(" Sorry you cannot update the word since the word doesn't exist");
}
}
public void displayWithPrefix(Node root,String prefix)
{
boolean isPresent=(getNode(prefix)!=null);
if(isPresent)
{
Node last=getNode(prefix);
display(last,prefix);
}
}
}