-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLMap.java
284 lines (262 loc) · 7.91 KB
/
AVLMap.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Course: Comp 282
Semester: Summer 2016
Assignment: AVLMap
FileName: AVLMap.java
Author: Piechota, Michael
*/
import java.util.Stack;
public class AVLMap<K extends Comparable<K>, V>
{
private AVLNode<K, V> root;
private int size;
public AVLMap()
{
size = 0;
}
public void clear()
{
root = null;
size = 0;
}
public int size()
{
return size;
}
public boolean containsKey(Object key)
{
if (key == null) {
throw new NullPointerException();
}
AVLNode<K, V> node = root;
while (node != null)
{
//if the key is smaller than go left
if (node.getKey().compareTo((K) key) > 0)
{
node = node.getLeft();
}
//if the key is larger than go right
else if (node.getKey().compareTo((K) key) < 0)
{
node = node.getRight();
}
else
{
return true;
}
}
//if it is not found
return false;
}
public boolean containsValue(Object value)
{
if (root == null) //if the tree is empty
{
return false;
}
/*in order to search all the nodes in the tree, an iterative
stack is implemented. */
Stack<AVLNode<K, V>> stack = new Stack<AVLNode<K, V>>();
stack.push(root);
while (!stack.empty())
{
AVLNode<K, V> node = stack.pop();
if (node.getValue().equals((V) value))
{
return true;
}
if (node.getRight() != null)
{
stack.push(node.getRight());
}
if (node.getLeft() != null)
{
stack.push(node.getLeft());
}
}
return false;
}
public V get(Object key)
{
if (key == null)
{
throw new NullPointerException();
}
AVLNode<K, V> node = root;
while (node != null)
{
if (node.getKey().compareTo((K) key) > 0)
{
node = node.getLeft();
}
else if (node.getKey().compareTo((K) key) < 0)
{
node = node.getRight();
}
else
{
return node.getValue();
}
}
return null;
}
public V put(K key, V value)
{
if (key == null)
{
throw new NullPointerException();
}
//if exist then update the value and return old value
if (containsKey(key))
{
V v = get(key);
AVLNode<K, V> node = root;
while (node != null)
{
if (node.getKey().compareTo((K) key) > 0)
{
node = node.getLeft();
}
else if (node.getKey().compareTo((K) key) < 0)
{
node = node.getRight();
}
else
{
node.setValue(value);
break;
}
}
return v;
}
//If there is a NEW KEY, create a node and restore the balance
else //if the tree IS EMPTY:
{
if (root == null) //if the root IS NULL, create a new root
{
root = new AVLNode<K, V>(key, value); // Create a NEW root
AVLNode<K, V> rootArg = root;
rootArg.fixMe(key, root);
size++;
return null;
}
else
{
//if the tree is NOT emptry, find the parent node.
//rootArg2 needed to give the method fix me the root argument
AVLNode<K, V> parent = null;
AVLNode<K, V> current = root;
AVLNode<K, V> rootArg2 = current;
while (current != null)
{
if (key.compareTo(current.getKey()) < 0)
{
parent = current;
current = current.getLeft();
}
else if (key.compareTo(current.getKey()) > 0)
{
parent = current;
current = current.getRight();
}
}
//Now create the new node and attach it to the parent node
if (key.compareTo(parent.getKey()) < 0)
{
parent.setLeft(new AVLNode<K, V>(key, value));
}
else
{
parent.setRight(new AVLNode<K, V>(key, value));
}
//re balances the tree; increments the size.
rootArg2.fixMe(key, current);
size++;
return null;
}
}
}
public V remove(Object key)
{
if (key == null)
{
throw new NullPointerException();
}
if (root == null)
{//returns null if the element you want to remove is NOT in the tree
return null;
}
V v = get(key);
/*now locate the node to be removed, and also located that node's
parent node */
AVLNode<K, V> parent = null;
AVLNode<K, V> current = root;
while (current != null)
{
if (current.getKey().compareTo((K) key) > 0)
{
parent = current;
current = current.getLeft();
}
else if (current.getKey().compareTo((K) key) < 0)
{
parent = current;
current = current.getRight();
}
else //the element IS THE TREE poointed at by "current"
{
break;
}
}
if (current == null)
{ //the element is NOT in the tree
return null;
}
if (current.getLeft() == null)
{ //if "current" has NO LEFT CHILDREN:
if (parent == null)
{
root = current.getRight();
}
else if (parent.getKey().compareTo((K) key) > 0)
{
parent.setLeft(current.getRight());
}
else
{
parent.setRight(current.getRight());
}
current.fixMe(parent.getKey(), current); //re balance tree
}
else
{
/* "current" node has a left child
therefore locate the right most node in the left
subtree of the current node & its parent*/
AVLNode<K, V> parentOfRightMost = current;
AVLNode<K, V> rightMost = current.getLeft();
while (rightMost.getRight() != null)
{
parentOfRightMost = rightMost;
//keep on going to the right
rightMost = rightMost.getRight();
}
//now replace the key in "current" w/key in rightMostNode
current.setKey(rightMost.getKey());
//now delete the rightMostNode node
if (parentOfRightMost.getRight() == rightMost)
{
parentOfRightMost.setRight(rightMost.getLeft());
}
else
{//if the parentOfTheRightMostNode is "current"
parentOfRightMost.setLeft(rightMost.getLeft());
}
//now re balance the tree
current.fixMe(parentOfRightMost.getKey(), current);
}
size--;
return v;
}
}