-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapImpl.java
More file actions
88 lines (73 loc) · 2.12 KB
/
HeapImpl.java
File metadata and controls
88 lines (73 loc) · 2.12 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
class HeapImpl<T extends Comparable<? super T>> implements Heap<T> {
private static final int INITIAL_CAPACITY = 128;
private T[] _storage;
private int _numElements;
@SuppressWarnings("unchecked")
public HeapImpl() {
_storage = (T[]) new Comparable[INITIAL_CAPACITY];
_numElements = 0;
}
@SuppressWarnings("unchecked")
public void add(T data) {
if (_numElements < INITIAL_CAPACITY) {
_storage[_numElements] = data;
_numElements++;
}
// "trickle up" method
trickleUp(data, _numElements - 1);
}
/**
* makes sure that the largest data is always on top, swaps them if not
*
* @param data the variable that we added
* @param index the current index of the data
*/
public void trickleUp(T data, int index) {
int parent = (index - 1) / 2;
if (parent >= 0) {
if (data.compareTo(_storage[parent]) > 0) {
_storage[index] = _storage[parent];
_storage[parent] = data;
trickleUp(data, parent);
}
}
}
public T removeFirst() {
T largest = _storage[0];
_storage[0] = _storage[_numElements - 1];
_storage[_numElements - 1] = null;
_numElements--;
trickleDown(0);
return largest;
}
/**
* makes sure the largest data is always at top even after removing the root
*
* @param index the current index of the data
*/
public void trickleDown(int index) {
int leftNodeIndex = (2 * index) + 1;
int rightNodeIndex = (2 * index) + 2;
int largest = index;
// checks if it's not greater than the size, sees if the left is larger than the
// root
if (leftNodeIndex < _numElements && _storage[leftNodeIndex].compareTo(_storage[largest]) > 0) {
largest = leftNodeIndex;
}
// checks if its not greater than the size, sees if the right is larger than the
// largest so far
if (rightNodeIndex < _numElements && _storage[rightNodeIndex].compareTo(_storage[largest]) > 0) {
largest = rightNodeIndex;
}
// swaps the largest, calls the function recursively
if (largest != index) {
T tempData = _storage[largest];
_storage[largest] = _storage[index];
_storage[index] = tempData;
trickleDown(largest);
}
}
public int size() {
return _numElements;
}
}