-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.java
More file actions
171 lines (147 loc) · 4.71 KB
/
Copy pathMinHeap.java
File metadata and controls
171 lines (147 loc) · 4.71 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
package TreePackage;
import java.util.Arrays;
public class MinHeap<T extends Comparable<? super T>> implements MinHeapInterface<T>
{
// Array to hold the heap entries
private T[] heap;
// Index of last entry
private int lastIndex;
private static final int DEFAULT_CAPACITY = 25;
private static final int MAX_CAPACITY = 10000;
public MinHeap(int initialCapacity)
{
if (initialCapacity < DEFAULT_CAPACITY)
initialCapacity = DEFAULT_CAPACITY;
else
checkCapacity(initialCapacity);
// Create an array one larger than capacity since index 0 will be empty
@SuppressWarnings("unchecked")
T[] tempHeap = (T[]) new Comparable[initialCapacity + 1];
heap = tempHeap;
lastIndex = 0;
}
/** Constructor to build a heap from an existing array
* @param arr The array to turn into a heap */
public MinHeap(T[] arr)
{
// Call the other constructor to create an internal array of size arr.length
this(arr.length);
lastIndex = arr.length;
// Copy the values from the array parameter to the internal array
for (int i = 0; i < arr.length; i++)
{
heap[i + 1] = arr[i];
}
// Convert the internal array to a heap
// We can ignore the nodes in the final level, since they are already heaps
for (int rootIndex = lastIndex / 2; rootIndex > 0; rootIndex--)
{
reheap(rootIndex);
}
}
@Override
public void add(T newEntry)
{
// Find the first open index, or the next available leaf, in the heap
int newIndex = lastIndex + 1;
// Find the parent of the newly added leaf
int parentIndex = newIndex / 2;
while (parentIndex > 0 && newEntry.compareTo(heap[parentIndex]) < 0)
{
// Swap up!
heap[newIndex] = heap[parentIndex];
// Update newIndex to be the newly empty index
newIndex = parentIndex;
// Find the parent index of newIndex
parentIndex = newIndex / 2;
}
// We are done swapping, so place the new item at newIndex
heap[newIndex] = newEntry;
lastIndex++;
ensureCapacity();
}
@Override
public T removeMin()
{
T max = null;
if (!isEmpty())
{
max = heap[1];
heap[1] = heap[lastIndex]; // Place the rightmost leaf in the root
lastIndex--;
reheap(1);
}
return max;
}
@Override
public T getMin()
{
if (lastIndex < 1)
return null;
return heap[1];
}
@Override
public boolean isEmpty()
{
return lastIndex == 0;
}
@Override
public int getSize()
{
return lastIndex;
}
@Override
public void clear()
{
heap = null;
}
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Heap cannot have " + capacity + " items in it!");
}
private void reheap(int rootIndex)
{
boolean done = false;
// orphan is the entry that needs to move down the heap
T orphan = heap[rootIndex];
int leftChildIndex = 2 * rootIndex;
while (!done && leftChildIndex <= lastIndex)
{
// find the larger child of the entry in rootIndex
int largerChildIndex = leftChildIndex;
int rightChildIndex = 2 * rootIndex + 1;
if (rightChildIndex <= lastIndex && heap[rightChildIndex].compareTo(heap[largerChildIndex]) < 0)
{
// Root has a right child and it is larger than the left child
largerChildIndex = rightChildIndex;
}
if (orphan.compareTo(heap[largerChildIndex]) > 0)
{
// Orphan is less than the largest child, so swap down
heap[rootIndex] = heap[largerChildIndex];
rootIndex = largerChildIndex;
leftChildIndex = 2 * rootIndex;
}
else
done = true;
}
heap[rootIndex] = orphan;
}
public void printHeap()
{
for (int i = 1; i <= lastIndex; i++)
{
System.out.print(heap[i] + " ");
}
}
private void ensureCapacity()
{
if(lastIndex == heap.length - 1)
{
int newLength = 2 * heap.length;
checkCapacity(newLength);
heap = Arrays.copyOf(heap, newLength);
}
}
}