-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
173 lines (158 loc) · 4.74 KB
/
Trie.java
File metadata and controls
173 lines (158 loc) · 4.74 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
165
166
167
168
169
170
171
172
173
package datastructure.trie;
/**
* The type Trie.
*/
public class Trie {
/**
* The Root.
*/
private TrieNode root;
/**
* Gets root.
*
* @return the root
*/
public TrieNode getRoot() {
return root;
}
/**
* Instantiates a new Trie.
*/
Trie(){
this.root =new TrieNode();
}
/**
* Get index int.
*
* @param t the t
* @return the int
*/
public int getIndex(char t){
return t - 'a';
}
/**
* Insert.
*
* @param key the key
*/
//Function to insert a key,value pair in the Trie
public void insert(String key){
if(null==key){
System.out.println("Null Key can not be Inserted!");
return;
}
key=key.toLowerCase();
TrieNode node = this.root;
int index =0;
for(int level =0 ;level<key.length();level++){
index=getIndex(key.charAt(level));
if(null==node.getChildren()[index]){
node.getChildren()[index]=new TrieNode();
}
node=node.getChildren()[index];
}
node.markAsLeaf();
}
/**
* Search boolean.
*
* @param key the key
* @return the boolean
*/
//Function to search given key in Trie
public boolean search(String key){
if(null==key)
return false;
key=key.toLowerCase();
TrieNode currentNode = this.root;
int index =0;
for(int level =0;level<key.length();level++){
index = getIndex(key.charAt(level));
if(currentNode.getChildren()[index]==null)
return false;
currentNode=currentNode.getChildren()[index];
}
return currentNode.isEndWord();
}
/**
* Delete boolean.
*
* @param key the key
* @return the boolean
*/
//Function to delete given key from Trie
public boolean delete(String key){
if(null==root || null == key)
return false;
return deleteHelper(key,root,key.length(),0);
}
/**
* Delete helper boolean.
*
* @param key the key
* @param currentNode the current node
* @param length the length
* @param level the level
* @return the boolean
*/
private boolean deleteHelper(String key, TrieNode currentNode, int length, int level) {
boolean deletedSelf = false;
if (currentNode == null) {
System.out.println("Key does not exist");
return deletedSelf;
}
//Base Case: If we have reached at the node which points to the alphabet at the end of the key.
if (level == length) {
//If there are no nodes ahead of this node in this path
//Then we can delete this node
if (hasNoChildren(currentNode)) {
currentNode = null;
deletedSelf = true;
}
//If there are nodes ahead of currentNode in this path
//Then we cannot delete currentNode. We simply unmark this as leaf
else {
currentNode.unMarkAsLeaf();
deletedSelf = false;
}
} else {
TrieNode childNode = currentNode.getChildren()[getIndex(key.charAt(level))];
boolean childDeleted = deleteHelper(key, childNode, length, level + 1);
if (childDeleted) {
//Making children pointer also null: since child is deleted
currentNode.getChildren()[getIndex(key.charAt(level))] = null;
//If currentNode is leaf node that means currntNode is part of another key
//and hence we can not delete this node and it's parent path nodes
if (currentNode.isEndWord()) {
deletedSelf = false;
}
//If childNode is deleted but if currentNode has more children then currentNode must be part of another key.
//So, we cannot delete currenNode
else if (!hasNoChildren(currentNode)) {
deletedSelf = false;
}
//Else we can delete currentNode
else {
currentNode = null;
deletedSelf = true;
}
} else {
deletedSelf = false;
}
}
return deletedSelf;
}
/**
* Has no children boolean.
*
* @param currentNode the current node
* @return the boolean
*/
private boolean hasNoChildren(TrieNode currentNode) {
for (int i = 0; i < currentNode.getChildren().length; i++) {
if ((currentNode.getChildren()[i]) != null)
return false;
}
return true;
}
}