forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
136 lines (108 loc) · 3.4 KB
/
Heap.java
File metadata and controls
136 lines (108 loc) · 3.4 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
/**
* Created by Zebin Xu.
*
*/
package heapsort;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
public class Heap {
public static void main(String[] args) {
int[] array = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
List<Integer> heapArray = Arrays.stream(array).boxed().collect(Collectors.toList());
Heap heap = new Heap(heapArray);
System.out.println(heap);
/** heap operations **/
heap.buildHeap(HeapType.MAX);
System.out.printf("build a max heap: %s%n", heap);
heap.buildHeap(HeapType.MIN);
System.out.printf("build a min heap: %s%n", heap);
/** heapsort examples **/
heap.sort(HeapType.MAX);
System.out.printf("heap sort in ascending order: %s%n", heap);
heap.sort(HeapType.MIN);
System.out.printf("heap sort in descending order: %s%n", heap);
}
public static enum HeapType {
MIN,
MAX
}
protected final List<Integer> array;
public Heap() {
this.array = new ArrayList<>();
}
public Heap(List<Integer> array) {
this.array = array;
}
public int parent(int index) {
return (index - 1) >> 1;
}
public int left(int index) {
return (index << 1) + 1;
}
public int right(int index) {
return (index << 1) + 2;
}
public void heapify(List<Integer> heapArray, int heapSize, int i, HeapType type) {
int l = left(i);
int r = right(i);
int minOrMax = i;
if (l < heapSize && compare(heapArray.get(l), heapArray.get(i), type)) {
minOrMax = l;
}
if (r < heapSize && compare(heapArray.get(r), heapArray.get(minOrMax), type)) {
minOrMax = r;
}
if (minOrMax != i) {
swap(heapArray, i, minOrMax);
heapify(heapArray, heapSize, minOrMax, type);
}
}
private boolean compare(int one, int another, HeapType type) {
switch(type) {
case MIN:
return one < another;
case MAX:
return one > another;
default:
throw new IllegalArgumentException(String.format("Unrecognized HeapType %s", type.name()));
}
}
protected void swap(List<Integer> heapArray, int indexA, int indexB) {
int temp = heapArray.get(indexA);
heapArray.set(indexA, heapArray.get(indexB));
heapArray.set(indexB, temp);
}
public void buildHeap(HeapType type) {
buildHeap(this.array, type);
}
private void buildHeap(List<Integer> array, HeapType type) {
final int heapSize = array.size();
for (int i = heapSize / 2 - 1; i >= 0; --i) {
heapify(array, heapSize, i, type);
}
}
public void sort(HeapType type) {
heapSort(this.array, type);
}
private void heapSort(List<Integer> array, HeapType type) {
buildHeap(array, type);
int size = array.size();
for (int i = array.size() - 1; i > 0; --i) {
swap(array, 0, i);
size -= 1;
heapify(array, size, 0, type);
}
}
/** auxiliary methods **/
public int size() {
return array.size();
}
public boolean isEmpty() {
return array.isEmpty();
}
@Override public String toString() {
return array.toString();
}
}