-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedHeapMin.java
executable file
·196 lines (163 loc) · 5.52 KB
/
LinkedHeapMin.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
package com.example.binaryTree;
import com.example.exceptions.EmptyCollectionException;
import com.example.interfaces.HeapMinADT;
import java.util.NoSuchElementException;
/**
* Heap implements a heap.
*/
public class LinkedHeapMin<T extends Comparable<? super T>> extends LinkedBinaryTree<T> implements HeapMinADT<T> {
private HeapNode<T> lastNode;
public LinkedHeapMin() {
super();
this.lastNode = null;
}
public LinkedHeapMin(T element) {
super(element);
this.lastNode = null;
}
/**
* {@inheritDoc}
*/
@Override
public void addElement(T element) {
if (element == null) {
throw new NoSuchElementException("The element to add most not be null");
}
HeapNode<T> node = new HeapNode<>(element);
if (isEmpty()) {
this.root = node;
this.lastNode = node;
} else {
HeapNode<T> nextParentAdd = getNextParentAdd();
if (nextParentAdd.left == null) {
nextParentAdd.left = node;
} else {
nextParentAdd.right = node;
}
node.parent = nextParentAdd;
this.lastNode = node;
heapifyAdd();
}
this.count++;
}
/**
* Returns the node that will be the parent of the new node
*
* @return the node that will be a parent of the new node
*/
private HeapNode<T> getNextParentAdd() {
HeapNode<T> result = this.lastNode;
//Quando parent à esquerda igual a elemento provavel haver lugar a sua direita
while (result != this.root && result.parent.left != result) {
result = result.parent;
}
if (result != this.root) {
if (result.parent.right == null) {
result = result.parent;
} else {
result = (HeapNode<T>) result.parent.right;
while (result.left != null) {
result = (HeapNode<T>) result.left;
}
}
} else {//Caso fiqui uma arvore cheia
while (result.left != null) {
result = (HeapNode<T>) result.left;
}
}
return result;
}
/**
* Reorders this heap after adding a node.
*/
private void heapifyAdd() {
HeapNode<T> current = this.lastNode;
T elementAdd = current.element;
while (current != this.root && elementAdd.compareTo(current.parent.element) < 0) {
current.element = current.parent.element;
current = current.parent;
}
current.element = elementAdd;
}
/**
* {@inheritDoc}
*/
@Override
public T removeMin() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
T removed = this.root.element;
this.root.element = this.lastNode.element;
if (this.count == 1) {
this.root = null;
this.lastNode = null;
} else {
HeapNode<T> newLast = getNewLastNode();
if (this.lastNode.parent.left == this.lastNode) {
this.lastNode.parent.left = null;
} else {
this.lastNode.parent.right = null;
}
this.lastNode = newLast;
heapifyRemove();
}
this.count--;
return removed;
}
private void heapifyRemove() {
HeapNode<T> current = (HeapNode<T>) this.root;
T lastElement = current.element;
if (current.left == null && current.right == null) {
current = null;
} else if (current.left == null) {
current = (HeapNode<T>) current.right;
} else if (current.right == null) {
current = (HeapNode<T>) current.left;
} else {
current = current.right.element.compareTo(current.left.element) > 0
? (HeapNode<T>) current.left : (HeapNode<T>) current.right;
}
while (current != null && current.element.compareTo(lastElement) < 0) {
current.parent.element = current.element;
current.element = lastElement;
if (current.left == null && current.right == null) {
current = null;
} else if (current.left == null) {
current = (HeapNode<T>) current.right;
} else if (current.right == null) {
current = (HeapNode<T>) current.left;
} else {
current = current.right.element.compareTo(current.left.element) > 0
? (HeapNode<T>) current.left : (HeapNode<T>) current.right;
}
}
}
private HeapNode<T> getNewLastNode() {
HeapNode<T> result = this.lastNode;
while (result != this.root && result.parent.left == result) {
result = result.parent;
}
if (result != this.root) {
result = (HeapNode<T>) result.parent.left;
while (result.right != null) {
result = (HeapNode<T>) result.right;
}
} else {
while (result.right != null) {
result = (HeapNode<T>) result.right;
}
}
return result;
}
/**
* {@inheritDoc }
*/
@Override
public T findMin() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
return this.root.element;
}
}