-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom_Generic_Heap.java
178 lines (147 loc) · 4.48 KB
/
Custom_Generic_Heap.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
/**
*
* @author pulkit4tech
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
class Custom_Generic_Heap implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new Custom_Generic_Heap()).start();
}
void solve() throws Exception {
runmyheap();
}
void runmyheap() {
Heap<String> h = new Heap<String>();
h.insert("p");
h.insert("r");
h.insert("i");
h.insert("o");
h.deleteMin();
System.out.println(h);
h.deleteMin();
System.out.println(h);
Heap<Integer> tmp = new Heap<Integer>();
Integer[] a = {4, 7, 7, 7, 5, 0, 2, 3, 5, 1};
tmp.heapSort(a);
System.out.println(Arrays.toString(a));
}
public class Heap<AnyType extends Comparable<AnyType>> {
private static final int CAPACITY = 2;
private int size; // Number of elements in heap
private AnyType[] heap; // The heap array
public Heap() {
size = 0;
heap = (AnyType[]) new Comparable[CAPACITY];
}
/**
* Construct the binary heap given an array of items.
*/
public Heap(AnyType[] array) {
size = array.length;
heap = (AnyType[]) new Comparable[array.length + 1];
System.arraycopy(array, 0, heap, 1, array.length);//we do not use 0 index
buildHeap();
}
/**
* runs at O(size)
*/
private void buildHeap() {
for (int k = size / 2; k > 0; k--) {
heapify(k);
}
}
private void heapify(int k) {
AnyType tmp = heap[k];
int child;
for (; 2 * k <= size; k = child) {
child = 2 * k;
if (child != size
&& heap[child].compareTo(heap[child + 1]) > 0) {
child++;
}
if (tmp.compareTo(heap[child]) > 0) {
heap[k] = heap[child];
} else {
break;
}
}
heap[k] = tmp;
}
/**
* Sorts a given array of items.
*/
public void heapSort(AnyType[] array) {
size = array.length;
heap = (AnyType[]) new Comparable[size + 1];
System.arraycopy(array, 0, heap, 1, size);
buildHeap();
for (int i = size; i > 0; i--) {
AnyType tmp = heap[i]; //move top item to the end of the heap array
heap[i] = heap[1];
heap[1] = tmp;
size--;
heapify(1);
}
for (int k = 0; k < heap.length - 1; k++) {
array[k] = heap[heap.length - 1 - k];
}
}
/**
* Deletes the top item
*/
public AnyType deleteMin() throws RuntimeException {
if (size == 0) {
throw new RuntimeException();
}
AnyType min = heap[1];
heap[1] = heap[size--];
heapify(1);
return min;
}
/**
* Inserts a new item
*/
public void insert(AnyType x) {
if (size == heap.length - 1) {
doubleSize();
}
//Insert a new item to the end of the array
int pos = ++size;
//Percolate up
for (; pos > 1 && x.compareTo(heap[pos / 2]) < 0; pos = pos / 2) {
heap[pos] = heap[pos / 2];
}
heap[pos] = x;
}
private void doubleSize() {
AnyType[] old = heap;
heap = (AnyType[]) new Comparable[heap.length * 2];
System.arraycopy(old, 1, heap, 1, size);
}
public String toString() {
String out = "";
for (int k = 1; k <= size; k++) {
out += heap[k] + " ";
}
return out;
}
}
}