-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyMinHeap.java
273 lines (241 loc) · 6.77 KB
/
MyMinHeap.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
/**
* This file contains a custom implimentation of the MinHeap Data
* struture using the list ADT
*
* Name: Qixuan "Harrison" Ma
* Email: [email protected]
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
/**
* This class is a custom implementation of the MinHeap data structure
* using the list ADT
* It has 2 constructors, 1 instance variable, and 13 methods
*/
public class MyMinHeap<E extends Comparable<E>>
{
// instance variable
protected List<E> list;
private static final int CONSTANT_TWO = 2;
/**
* default no-arg constructor
* initializes the list instance variable to empty ArrayList
*/
public MyMinHeap()
{
this.list = new ArrayList<E>();
}
/**
* second constructor
* initialize a MinHeap using elements in the collection
*
* @throws a NullPointerException if the collection or any element in the
* collection is null
*/
public MyMinHeap(Collection<? extends E> collection)
{
if (collection == null)
{
throw new NullPointerException("The input collection is null");
}
for (Object o : collection)
{
if (o == null)
{
throw new NullPointerException("The input collection is null");
}
}
this.list = new ArrayList<E>(collection);
for (int i = this.list.size() - 1; i >= 0; i --)
{
percolateDown(i);
}
}
/**
* swap the locations of the elements at the from and to indices in list
* @params from and to are indices that we want to swap
*/
@SuppressWarnings("unchecked")
protected void swap(int from, int to)
{
Object fromInd = this.list.get(from);
this.list.set(from, this.list.get(to));
this.list.set(to, (E) fromInd);
}
/**
* calculate and return the parent index of the parameter index
* @param index that we want to use to calculate parent index
* @return the parent index of the input index
*/
protected int getParentIdx(int index)
{
return (index - 1)/ 2;
}
/**
* calculate and return the left child index of the parameter index
* @param index of the parent
* @returns index of the left child
*/
protected int getLeftChildIdx(int index)
{
return 2 * index + 1;
}
/**
* calculate and return the right child index of the parameter index
* @param index of the parent
* @returns index of the right child
*/
protected int getRightChildIdx(int index)
{
return 2 * index + 2;
}
/**
* return the index of the smaller child of the element at index
* if the two children are equal, return the index of the left child
* @param index of the parent
* @returns index of the smaller child
*/
@SuppressWarnings("unchecked")
protected int getMinChildIdx(int index)
{
if ((2 * index + 2) > list.size() - 1)
{
if ((2 * index + 1) > list.size() - 1)
{
return 2 * index + 1;
}
return -1;
}
Object leftChild = this.list.get(2 * index + 1);
Object rightChild = this.list.get(2 * index + 2);
// if the two elements are equal
if (((E)leftChild).compareTo((E)rightChild) == 0)
{
return 2 * index + 1;
}
// if the left child is smaller
else if (((E)leftChild).compareTo((E)rightChild) < 0)
{
return 2 * index + 1;
}
// if the right child is smaller
else
{
return 2 * index + 2;
}
}
/**
* percolate the element at the parameter index up until no heap
* properties are violated by this element
* @param index of element to be percolatedUp
*/
protected void percolateUp(int index)
{
int currInd = index;
int parentInd = getParentIdx(index);
if (list.get(currInd).compareTo(list.get(parentInd)) < 0)
{
swap (currInd, parentInd);
percolateUp(parentInd);
}
}
/**
* percolate the element at the parameter index down until no heap
* properties are violated by this element
* @param index of element to be percolatedDown
*/
protected void percolateDown(int index)
{
if (index > list.size() - 1)
{
return;
}
int currInd = index;
int smallChild = getMinChildIdx(index);
if (smallChild > list.size() - 1)
{
return;
}
if (list.get(currInd).compareTo(list.get(smallChild)) > 0)
{
swap(currInd, smallChild);
percolateDown(smallChild);
}
}
/**
* remove the element at the specified index from list and return it
* @param index of the element to be removed
* @return element that was removed
*/
@SuppressWarnings("unchecked")
protected E deleteIndex(int index)
{
Object orig = list.get(0);
Object replacement = list.get(list.size() - 1);
list.set(index, list.get(list.size() - 1));
// the replacement is the same as the original
if (((E)orig).compareTo((E)replacement) == 0)
{
// do nothing, since the heap properties are already maintained
}
// the original is smaller than the replacement
else if (((E)orig).compareTo((E)replacement) > 0)
{
percolateUp(index);
}
// the original is larger than the replacement
else
{
percolateDown(index);
}
list.remove(list.size() - 1);
return (E)orig;
}
/**
* add element to the end of the heap and percolate the inserted element
* up until no heap properties are violated
* @param element to be inserted
*/
public void insert(E element)
{
this.list.add(element);
percolateUp(this.list.size() - 1);
}
/**
* @return the root (smallest) element of the heap
*/
public E getMin()
{
return this.list.get(0);
}
/**
* remove and return the root
* @return root of the heap
*/
@SuppressWarnings("unchecked")
public E remove()
{
if (this.size() == 0)
{
return null;
}
Object root = this.list.get(0);
deleteIndex(0);
return (E) root;
}
/**
* @return number of elements in the MinHeap
*/
public int size()
{
return this.list.size();
}
/**
* clear out the entire heap
*/
public void clear()
{
this.list = new ArrayList<>();
}
}