-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentHeapClass.java
317 lines (233 loc) · 7.34 KB
/
StudentHeapClass.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
package p10_package;
public class StudentHeapClass {
private int arrayCapacity;
private int arraySize;
int DEFAULT_ARRAY_CAPACITY = 10;
//Display flag can be set to observe bubble up and trickle down operations
private boolean displayFlag;
private StudentClass[] heapArray;
int LEFT = 2002;
int PARENT = 1001;
int RIGHT = 3003;
public StudentHeapClass()
{
arrayCapacity = DEFAULT_ARRAY_CAPACITY;
heapArray = new StudentClass[arrayCapacity];
arraySize = 0;
displayFlag = false;
}
public StudentHeapClass(StudentHeapClass copied)
{
arraySize = copied.arraySize;
arrayCapacity = copied.arrayCapacity;
heapArray = new StudentClass[arrayCapacity];
displayFlag = false;
int index;
for(index = 0; index < arraySize; index++)
{
heapArray[index] = new StudentClass(copied.heapArray[index]);
}
}
public void addItem(StudentClass newItem)
{
//Accepts StudentData item and adds it to heap
//Note: uses bubbleUpArrayHeap to resolve unbalanced heap after data addition
//Note: must check for resize before attempting to add an item
checkForResize();
heapArray[arraySize] = newItem;
arraySize++;
bubbleUpArrayHeap(arraySize-1);
}
private void bubbleUpArrayHeap(int currentIndex)
{
//Recursive operation to reset data in the correct order
//for the max heap after new data addition
int parentIndex = ( currentIndex - 1 ) / 2;
//if the current is greater than the value at my parent index
if(heapArray[currentIndex].compareTo(heapArray[parentIndex]) > 0)
{
//swap
StudentClass swapped = heapArray[currentIndex];
heapArray[currentIndex] = heapArray[parentIndex];
heapArray[parentIndex] = swapped;
//recurse
bubbleUpArrayHeap(parentIndex);
}
}
private void checkForResize()
{
//Automatic resize operation used prior to any new data addition in the heap
//Tests for full heap array, and resizes to twice the current capacity as required
int index;
if(this.arraySize == arrayCapacity)
{
arrayCapacity *=2;
StudentClass[] copyArray = new StudentClass[arrayCapacity];
for(index = 0; index < arraySize ;index++)
{
copyArray[index] = heapArray[index];
}
heapArray = copyArray;
}
}
public boolean isEmpty()
{
//Tests for empty heap
return arraySize == 0;
}
//*****************************
public StudentClass removeItem()
{
StudentClass temp = null;
if(!isEmpty())
{
if(displayFlag)
{
System.out
.println("\nRemoving process: " + this.heapArray[0].toString());
}
temp = this.heapArray[0];
this.heapArray[0] = this.heapArray[arraySize - 1];
this.arraySize--;
this.trickleDownArrayHeap(0);
}
return temp;
}
public void setDisplayFlag(boolean setState)
{
//Utility method to set the display flag for displaying internal operations
//of the heap bubble and trickle operations
displayFlag = setState;
}
public void showArray()
{
//Dumps array to screen as is, no filtering or management
int index;
for(index = 0; index < arraySize; index++)
{
System.out.print(heapArray[index].toString());
}
}
private void trickleDownArrayHeap(int currentIndex)
{
//Recursive operation to reset data in the correct order for the max heap after data removal
int leftIndex = 2 * currentIndex + 1;
int rightIndex = 2 * currentIndex + 2;
if(leftIndex < arraySize)
{
if(rightIndex < arraySize)
{
if(heapArray[leftIndex].compareTo
(heapArray[rightIndex]) > 0)
{
//left index greater than right
//compare left index to current index
if(heapArray[leftIndex].compareTo
(heapArray[currentIndex]) > 0)
{
//if the left index is greater than the current
//swap
StudentClass swapped = heapArray[currentIndex];
heapArray[currentIndex] = heapArray[leftIndex];
heapArray[leftIndex] = swapped;
//and recurse
trickleDownArrayHeap(leftIndex);
}
}
else if(heapArray[rightIndex].compareTo
(heapArray[leftIndex]) > 0)
{
//right index greater than left
//compare right index to current index
if(heapArray[rightIndex].compareTo
(heapArray[currentIndex]) >0)
{
//if the right index is greater
//swap
StudentClass swapped = heapArray[currentIndex];
heapArray[currentIndex] = heapArray[rightIndex];
heapArray[leftIndex] = swapped;
//and recurse
trickleDownArrayHeap(rightIndex);
}
}
}
else
{
//if the right index is not less than array size
//assume one child
if(heapArray[leftIndex].compareTo
(heapArray[currentIndex]) > 0)
{
//if the left index is greater than the current
//swap
StudentClass swapped = heapArray[currentIndex];
heapArray[currentIndex] = heapArray[leftIndex];
heapArray[leftIndex] = swapped;
//and recurse
trickleDownArrayHeap(leftIndex);
}
}
}
//if left index is less than arraysize, must still check for right, right?
else if(rightIndex < arraySize)
{
if(heapArray[rightIndex].compareTo
(heapArray[leftIndex]) > 0)
{
//right index greater than left
//compare right index to current index
if(heapArray[rightIndex].compareTo
(heapArray[currentIndex]) > 0)
{
//if the right index is greater
//swap
StudentClass swapped = heapArray[currentIndex];
heapArray[currentIndex] = heapArray[rightIndex];
heapArray[leftIndex] = swapped;
//and recurse
trickleDownArrayHeap(rightIndex);
}
}
}
}
private void trickleDownArrayHeap( int currentIndex )
{
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
int largestFlag = PARENT;
StudentClass leftChildData = heapArray[ leftChildIndex ];
StudentClass rightChildData = heapArray[ rightChildIndex ] ;
StudentClass parentData = heapArray[ currentIndex ];
if( leftChildIndex < arraySize )
{
if( leftChildData.compareTo( parentData ) > 0 )
{
largestFlag = LEFT;
}
if( rightChildIndex < arraySize )
{
if( rightChildData.compareTo( parentData ) > 0
&& rightChildData.compareTo( leftChildData ) > 0 )
{
largestFlag = RIGHT;
}
}
if( largestFlag != PARENT )
{
if( largestFlag == LEFT )
{
heapArray[ currentIndex ] = leftChildData;
heapArray[ leftChildIndex ] = parentData;
trickleDownArrayHeap( leftChildIndex );
}
else // assume right child is largest
{
heapArray[ currentIndex ] = rightChildData;
heapArray[ rightChildIndex ] = parentData;
trickleDownArrayHeap( rightChildIndex );
}
}
}
}
}