-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedBinarySearchTree.java
executable file
·344 lines (299 loc) · 10.2 KB
/
LinkedBinarySearchTree.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.example.binaryTree;
import com.example.exceptions.*;
import com.example.interfaces.BinarySearchTreeADT;
/**
* Implementation of an AVL binary search tree
* <p>
*/
public class LinkedBinarySearchTree<T extends Comparable<? super T>> extends LinkedBinaryTree<T> implements BinarySearchTreeADT<T> {
protected T elementRemovedTemp;
private BinaryTreeNode<T> nodeRemovedTemp;
/**
* Creates an empty binary search tree.
*/
public LinkedBinarySearchTree() {
super();
this.elementRemovedTemp = null;
this.nodeRemovedTemp = null;
}
/**
* Creates an AVL binary search with the specified element as its root.
*
* @param element the element that will be the root of the new binary search
* tree
*/
public LinkedBinarySearchTree(T element) {
super(element);
this.elementRemovedTemp = null;
this.nodeRemovedTemp = null;
}
/**
* {@inheritDoc }
*/
@Override
public void addElement(T element) {
if (element == null) {
throw new NullPointerException("The element to add should not be null");
}
this.root = insert(this.root, element);
this.count++;
}
private BinaryTreeNode<T> insert(BinaryTreeNode<T> node, T element) {
if (node == null) {
node = new BinaryTreeNode<>(element);
} else if (element.compareTo(node.element) < 0) {
node.left = insert(node.left, element);
} else {
node.right = insert(node.right, element);
}
return balance(node);
}
protected BinaryTreeNode<T> balance(BinaryTreeNode<T> node) {
updateHeight(node);
if (getFactorBalance(node) < -1) {//Caused to the left
if (getFactorBalance(node.left) == 1) {//Caused to the left by right child
node.left = rotateLeft(node.left);
}
node = rotateRight(node);
} else if (getFactorBalance(node) > 1) {//Caused to the right
if (getFactorBalance(node.right) == -1) {//Caused to the right by left child
node.right = rotateRight(node.right);
}
node = rotateLeft(node);
}
return node;
}
protected BinaryTreeNode<T> rotateRight(BinaryTreeNode<T> subTreeRight) {
BinaryTreeNode<T> newRoot = subTreeRight.left;
subTreeRight.left = newRoot.right;
newRoot.right = subTreeRight;
updateHeight(subTreeRight);
updateHeight(newRoot);
return newRoot;
}
protected BinaryTreeNode<T> rotateLeft(BinaryTreeNode<T> subTreeLeft) {
BinaryTreeNode<T> newRoot = subTreeLeft.right;
subTreeLeft.right = newRoot.left;
newRoot.left = subTreeLeft;
updateHeight(subTreeLeft);
updateHeight(newRoot);
return newRoot;
}
private int getFactorBalance(BinaryTreeNode<T> node) {
return height(node.right) - height(node.left);
}
private void updateHeight(BinaryTreeNode<T> node) {
node.height = 1 + Math.max(height(node.left), height(node.right));
}
private int height(BinaryTreeNode<T> node) {
return node == null ? -1 : node.height;
}
private T getRemovedElement() {
T element = this.elementRemovedTemp;
this.elementRemovedTemp = null;
return element;
}
private void setRemovedElement(T element) {
this.elementRemovedTemp = element;
}
private BinaryTreeNode<T> getRemovedNode() {
BinaryTreeNode<T> temp = this.nodeRemovedTemp;
this.nodeRemovedTemp = null;
return temp;
}
private void setRemovedNode(BinaryTreeNode<T> node) {
this.nodeRemovedTemp = node;
}
/**
* Returns a reference to a node that will replace the one specified for
* removal. In the case where the removed node has two children, the inorder
* successor is used as its replacement.
*
* @param node the node to be removeed
* @return a reference to the replacing node
*/
protected BinaryTreeNode<T> replace(BinaryTreeNode<T> node) {
BinaryTreeNode<T> result = null;
if ((node.left == null) && (node.right == null)) {
result = null;
} else if ((node.left != null) && (node.right == null)) {
result = node.left;
} else if ((node.left == null) && (node.right != null)) {
result = node.right;
} else {//Remover node sucessor ao node que queremos remover e trocar de lugar
BinaryTreeNode<T> newSubRightTree = removeMinSubTree(node.right);
BinaryTreeNode<T> successorInOrder = getRemovedNode();
successorInOrder.right = newSubRightTree;
successorInOrder.left = node.left;
result = balance(successorInOrder);
}
return result;
}
protected BinaryTreeNode<T> removeMinSubTree(BinaryTreeNode<T> subTree) {
T removed = getRemovedElement();
if (subTree.left == null) {
setRemovedNode(subTree);
subTree = subTree.right;
} else {
subTree = deleteMin(subTree);
}
setRemovedElement(removed);
return subTree;
}
/**
{@inheritDoc}
*/
@Override
public T removeElement(T targetElement) throws ElementNotFoundException, EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
T removed;
if (this.root.element.compareTo(targetElement) == 0) {
removed = this.root.element;
this.root = replace(this.root);
} else {
this.root = remove(this.root, targetElement);
removed = getRemovedElement();
if (removed == null) {//Caso não tenha encontrado
throw new ElementNotFoundException(ElementNotFoundException.ELEMENT_NOT_FOUND);
}
}
//Para ser possivel voltar a colocar a null
this.count--;
return removed;
}
private BinaryTreeNode<T> remove(BinaryTreeNode<T> parent, T targetElement) {
int comparationParent = targetElement.compareTo(parent.element);
BinaryTreeNode<T> current = comparationParent > 0 ? parent.right : parent.left;
if (current != null && getRemovedElement() == null) {
if (targetElement.compareTo(current.element) == 0) {
setRemovedElement(current.element);
if (parent.left == current) {
parent.left = replace(current);
} else {
parent.right = replace(current);
}
} else if (comparationParent > 0) {
parent.right = remove(current, targetElement);
} else {
parent.left = remove(current, targetElement);
}
}
return balance(parent);
}
/**
* {@inheritDoc }
*/
@Override
public void removeAllOccurrences(T targetElement) throws ElementNotFoundException, EmptyCollectionException {
removeElement(targetElement);
boolean allRemoved = false;
while (!allRemoved) {
try {
removeElement(targetElement);
} catch (ElementNotFoundException | EmptyCollectionException e) {
allRemoved = true;
}
}
}
/**
* {@inheritDoc }
*/
@Override
public T removeMin() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
T removed;
if (this.root.left == null) {
removed = this.root.element;
this.root = this.root.right;
} else {
this.root = deleteMin(this.root);
removed = getRemovedElement();
}
count--;
return removed;
}
private BinaryTreeNode<T> deleteMin(BinaryTreeNode<T> parent) {
if (parent.left.left == null) {
setRemovedElement(parent.left.element);
setRemovedNode(parent.left);
if (parent.left.right == null) {
parent.left = null;
} else {
parent.left = parent.left.right;
}
} else {
deleteMin(parent.left);
}
return balance(parent);
}
/**
* {@inheritDoc }
*/
@Override
public T removeMax() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
T removed;
if (this.root.right == null) {
removed = this.root.element;
this.root = root.left;
} else {
this.root = deleteMax(root);
removed = getRemovedElement();
}
this.count--;
return removed;
}
private BinaryTreeNode<T> deleteMax(BinaryTreeNode<T> parent) {
if (parent.right.right == null) {
setRemovedElement(parent.right.element);
if (parent.right.left == null) {
parent.right = null;
} else {
parent.right = parent.right.left;
}
} else {
deleteMax(parent.right);
}
return balance(parent);
}
/**
* {@inheritDoc }
*/
@Override
public T findMin() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
BinaryTreeNode<T> current = this.root;
if (this.root.left != null) {
current = this.root.left;
while (current.left != null) {
current = current.left;
}
}
return current.element;
}
/**
* {@inheritDoc }
*/
@Override
public T findMax() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
BinaryTreeNode<T> current = root;
if (this.root.right != null) {
current = this.root.right;
while (current.right != null) {
current = current.right;
}
}
return current.element;
}
}