-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapi.java
408 lines (383 loc) · 12.3 KB
/
heapi.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* The general description is explained in main
*
* @author (Leah Kronengold,214087173)
* @author (Ester Gotliv,213497878)
* @version (24/06/2023)
*/
public class heapi
{
/**
* The size of the heap.
*/
public static int size = 0;
/**
* Builds a heap from the given array.
*
* @param a The array to build the heap from
* @return The heap array
*/
public static double[] BUILDHEAP(double[] a) {
for (int i = (size) / 2 - 1; i > -1; i--) {
heapi(a, i);
}
return a;
}
/**
* Performs the heapify operation on the array starting from the given index.
*
* @param a The array
* @param i The index to start heapify from
*/
public static void heapi(double[] a, int i) {
int result = (int) ((Math.log(i + 1)) / (Math.log(2)));//Depth calculation
if (result % 2 == 0) {
pushDownMax(a, i);
} else {
pushDownMin(a, i);
}
}
/**
* Performs the push down operation on the max heap starting from the given index.
*
* @param a The array
* @param i The index to start push down from
*/
public static void pushDownMax(double[] a, int i) {
int m;//The index of the largest value
double temp;//Substitution auxiliary variable
if (kids(a, i) == true) {
m = largestIndex(a, i);
if (m != left(i) && m != right(i) && m != i) {
// If not a child
if (a[m] > a[i]) {
temp = a[m];
a[m] = a[i];
a[i] = temp;
if (a[m] < a[perent(m)]) {
temp = a[m];
a[m] = a[perent(m)];
a[perent(m)] = temp;
}
heapi(a, m);
}
} else if (a[m] > a[i]) {
temp = a[m];
a[m] = a[i];
a[i] = temp;
}
}
}
/**
* Performs the push down operation on the min heap starting from the given index.
*
* @param a The array
* @param i The index to start push down from
*/
public static void pushDownMin(double[] a, int i) {
int m;//The index of the smallest value
double temp = 0;//Substitution auxiliary variable
if (kids(a, i) == true) {
m = smallestIndex(a, i);
if (m != left(i) && m != right(i)) {
// If not a child
if (a[m] < a[i]) {
temp = a[m];
a[m] = a[i];
a[i] = temp;
if (a[m] > a[perent(m)]) {
temp = a[m];
a[m] = a[perent(m)];
a[perent(m)] = temp;
}
heapi(a, m);
}
} else if (a[m] < a[i]) {
temp = a[m];
a[m] = a[i];
a[i] = temp;
}
}
}
/**
* Returns the index of the parent node for the given index.
*
* @param i The index
* @return The index of the parent node
*/
public static int perent(int i) {
return ((i - 1) / 2);
}
/**
* Returns the index of the left child of a given index i in a heap data structure.
*
* @param i The index
* @return The index of the left node
*/
public static int left(int i) {
return 2*i+1;
}
/**
* Returns the index of the right child of a given index i in a heap data structure.
*
* @param i The index
* @return The index of the right node
*/
public static int right(int i) {
return 2*i+2;
}
/**
*checks if a given index i in a heap data structure has any children.
*It returns true if the index has at least one child, and false otherwise
* @param i The index
* @param a the array
*/
public static boolean kids(double[] a,int i) {
if((left(i)<= size-1 && left(i)>=0) || (right(i)<= size-1 && right(i)>=0)){
return true;
}
return false;
}
/**
*checks if a given index i in a heap data structure has any grandchildren.
*It returns true if the index has at least one grandchild, and false otherwise
* @param i The index
* @param a the array
*/
public static boolean grandchildren(double[] a,int i) {
if(4*i+3<size && 4*i+3>-1){
return true;
}
return false;
}
/**
* returns the index of the maximum element among a given index i and its children in a heap data structure.
* @param i The index
* @param a the array
*/
public static int subArrayMax(double [] a,int i){
int x=i;//An auxiliary variable that receives the index
if(kids(a,i)==true){
if(a[left(i)]>a[x]){
x=left(i);
if(right(i)<size && right(i)>-1){
if(a[right(i)]>a[x]){
x=right(i);
}
}
}
else{
if(right(i)<size && right(i)>-1){
if(a[right(i)]>a[x]){
x=right(i);
}
}
}
}
return x;
}
/**
*returns the index of the largest element among a given index i, its left child, and its right child in a heap data structure.
* @param i The index
* @param a the array
*/
public static int largestIndex(double[] a, int i) {
double x;//An auxiliary variable for comparing two sizes
double y;//An auxiliary variable for comparing two sizes
if((left(i)<size && left(i)>-1)&&(right(i)<size && right(i)>-1)){
x=a[subArrayMax(a,left(i))];
y=a[subArrayMax(a,right(i))];
if(x>y){
return subArrayMax(a,left(i));
}
return subArrayMax(a,right(i));
}
if(left(i)<size && left(i)>-1){
return left(i);
}
return -1;
}
/**
* returns the index of the minimum element among a given index i and its children in a heap data structure.
* @param i The index
* @param a the array
*/
public static int subArrayMin(double[] a, int i) {
int x=i;//An auxiliary variable that receives the index
if(kids(a,i)==true){
if(a[left(i)]<a[x]){
x=left(i);
if(right(i)<size && right(i)>-1){
if(a[right(i)]<a[x]){
x=right(i);
}
}
}
else{
if(right(i)<size && right(i)>-1){
if(a[right(i)]<a[x]){
x=right(i);
}
}
}
}
return x;
}
/**
* returns the index of the smallest element among a given index i, its left child, and its right child in a heap data structure.
* @param i The index
* @param a the array
*/
public static int smallestIndex(double[] a, int i) {
double x;//An auxiliary variable for comparing two sizes
double y;//An auxiliary variable for comparing two sizes
if((left(i)<size && left(i)>-1)&&(right(i)<size && right(i)>-1)){
x=a[subArrayMin(a,left(i))];
y=a[subArrayMin(a,right(i))];
if(x<y){
return subArrayMin(a,left(i));
}
return subArrayMin(a,right(i));
}
if(left(i)<size && left(i)>-1){
x=a[subArrayMin(a,left(i))];
if(x<a[i]){
return subArrayMin(a,left(i));
}
else{
return i;
}
}
return -1;
}
/**
* extracts the maximum element from a heap data structure, removes it from the heap, and returns the updated heap.
* @param a the array
*/
public static double [] HEAPEXTRACTMAX(double[] a){
if(size==0)
return a;
double temp = a[0];//Substitution auxiliary variable
a[0] = a[size-1];
a[size-1] = temp;
size=size-1;//Reducing stack length
heapi(a,0);
return a;
}
/**
* extracts the minimum element from a heap data structure, removes it from the heap, and returns the updated heap.
* @param i The index
* @param a the array
*/
public static double[] HEAPEXTRACTMIN(double[] a){
if(size==0)
return a;
int min;//Auxiliary variable for finding the minimum length
if(a[1]>a[2])
min=2;
else
min=1;
double temp=a[min];//Substitution auxiliary variable
a[min] = a[size-1];
a[size-1] = temp;
size=size-1;//Decrease stack size by one
heapi(a,min);
return a;
}
/**
* deletes an element at index i from a heap data structure represented by the array A.
* It first marks the element at index i as a sentinel value (A[0]+1), then adjusts the heap by swapping the element with its parent if necessary,
* and finally performs HEAPEXTRACTMAX(A) to remove the sentinel element and restore the heap property.
* The updated heap array A is returned. If the given index i is out of bounds, an error message is printed, and the original array A is returned.
* @param i The index
* @param a the array
*/
public static double[] HEAPDELETE(double[] A, int i){
if(i>=size || i<0){
System.out.println("Error: The index does not exist");
return A;
}
A[i] = (A[0]+1);//Placing the place in place of the index to be the largest number in the stack to push it up
for(int j = i; j >0 && j<size-1;){
if(A[j]>A[perent(j)]){
double temp = A[j];
A[j] = A[perent(j)];
A[perent(j)] = temp;
heapi(A,j);
}
j = (j-1)/2;
if(j==0){
break;
}
}
HEAPEXTRACTMAX(A);//Sending to spend the maximum
return A;
}
/**
* Inserts a value into the heap and maintains the heap property.
*
* @param A The array representing the heap
* @param value The value to be inserted
* @return The modified heap array
*/
public static double[] HEAPINSERT(double[] A, double value) {
size++; // Increase the size of the heap
int i = (size) - 1;
A[size - 1] = value + A[0] + 1; // Insert the value into the last position
// Maintain the heap property by comparing with parent nodes and swapping if necessary
for (int j = i; j > 0 && j < size;) {
if (A[j] > A[perent(j)]) {
double temp = A[j];
A[j] = A[perent(j)];
A[perent(j)] = temp;
heapi(A, j);
}
j = (j - 1) / 2;
if (j == 0) {
break;
}
}
A[0] = value; // Update the root with the inserted value
heapi(A, 0);
return A;
}
/**
* Prints the elements of the heap array.
*
* @param A The array representing the heap
*/
public static void PrintHeap(double A[]) {
for (int i = 0; i < size; i++) {
if (i == 0) {
System.out.print("[");
}
System.out.print(A[i]);
if (i != size - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
* Sorts the heap array in ascending order.
*
* @param arr The array representing the heap
* @return The sorted heap array
*/
public static double[] heapSort(double[] arr) {
int n = size;//por the print
for (int i = n - 1; i > 0; i--) {
// Move the maximum to the end
double temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
size--;//Reducing the length of the array
heapi(arr, 0);
}
size = n;
System.out.print("The sorted heap: (heap does not exist anymore)\t");
PrintHeap(arr);
size = 0; // Reset the size to 0
return arr;
}
}