-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathHeap.java
More file actions
104 lines (91 loc) · 2.8 KB
/
Heap.java
File metadata and controls
104 lines (91 loc) · 2.8 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
import java.io.PrintStream;
import java.util.Arrays;
public class Heap {
public static int child1(int i) {
return (2 * i) + 1;
}
public static int child2(int i) {
return child1(i) + 1;
}
public static int lastParent(int[] arr) {
return lastParent(arr.length);
}
/** Return the index for the last parent node. */
public static int lastParent(int length) {
return (length / 2) - 1;
}
public static void heapify(int[] arr) {
for (int i = lastParent(arr); i >= 0; i--) {
maxHeapify(arr, i);
}
}
public static int maxHeapifyStep(int[] arr, int i) {
return maxHeapifyStep(arr, i, arr.length);
}
/** Make the heap property valid for a parent and its immediate children. */
public static int maxHeapifyStep(int[] arr, int i, int length) {
int child1I = child1(i);
int child2I = child2(i);
int child1 = arr[child1I];
int child2;
int swapWith;
int swapWithI;
int cur;
if (child2I == length) {
child2 = Integer.MIN_VALUE;
} else {
child2 = arr[child2I];
}
cur = arr[i];
if (child1 < child2) {
swapWith = child2;
swapWithI = child2I;
} else {
swapWith = child1;
swapWithI = child1I;
}
if (cur < swapWith) {
arr[swapWithI] = cur;
arr[i] = swapWith;
} else {
swapWithI = i;
}
return swapWithI;
}
public static void maxHeapify(int[] arr) {
maxHeapify(arr, 0);
}
public static void maxHeapify(int[] arr, int i) {
maxHeapify(arr, i, arr.length);
}
public static void maxHeapify(int[] arr, int i, int length) {
int lastParent = lastParent(length);
int lastI = -1;
while ((i != lastI) && (i <= lastParent)) {
lastI = i;
i = maxHeapifyStep(arr, i, length);
}
}
public static void main(String[] args) {
// heapify. The ideal would be to have a `isHeap` method
// since possible outputs are not unique.
{
final int ins[][] = {
{4, 5, 6, 2, 1, 3, 0, 7},
};
final int expected_outs[][] = {
{7, 5, 6, 4, 1, 3, 0, 2},
};
for (int i = 0; i < ins.length; i++) {
int out[] = ins[i].clone();
heapify(out);
if (!Arrays.equals(out, expected_outs[i])) {
System.err.println(Arrays.toString(ins[i]));
System.err.println(Arrays.toString(expected_outs[i]));
System.err.println(Arrays.toString(out));
System.exit(1);
}
}
}
}
}