-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyHeap.java
316 lines (231 loc) · 4.87 KB
/
MyHeap.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
import java.util.Arrays;
/**
*
*
*
*
* @author Maria Sigal
* Marina Khytynova
*
* Tutorium , Dienstag 16:00
*
*/
public class MyHeap {
private int lastElem;
private Integer[] arr;
public MyHeap(int size){
//create array with size as param
arr = new Integer[size];
lastElem=0;
}
/**
*
* check if element in index i has left child
*
*
* @param i
* @return
*/
public boolean hasLeftChild(int i){
return leftChildIndex(i) <=lastElem;
}
/**
* check if element in index i has right child
*
*
* @param i
* @return
*/
public boolean hasRightChild(int i){
return rightChildIndex(i)<=lastElem;
}
/**
* returns left child index of element in index i
*
*
* @param i
* @return
*/
public int leftChildIndex(int i){
return i*2;
}
/**
* returns right child index of element in index i
*
*
* @param i
* @return
*/
public int rightChildIndex(int i){
return i*2+1;
}
/**
* returns true in element in index k has parent
*
* @param k
* @return
*/
public boolean hasParent(int k){
int i = k/2;
return i!=0;
}
/**
* returns index of parent of element in index i
*
*
* @param i
* @return
*/
public int parent(int i){
return i/2;
}
/**
*
* removes element with smallest key
*
*
* @return
* @throws Exception
*/
public int deleteMin() throws Exception{
//if heap is empty , throw exception
if(lastElem==0){
throw new Exception("Empty Array");
}
int result = arr[1];
arr [1] = arr[lastElem];
arr[lastElem] =(Integer) null;
lastElem--;
//restore the heap property
bubbleDown(1);
return result;
}
/**
*
* insert new element to the heap
*
* @param elm
* @throws Exception
*/
public void insert(int elm) throws Exception{
//if heap is full, throw exception
if(lastElem+1>=arr.length){
throw new Exception("Heap overflow");
}
//increase number of elements in heap
lastElem++;
arr[lastElem]=elm;
//fix the heap property
bubbleUp(lastElem);
}
/*
* fixes the heap property with bubble up approach
*
* goes up to the tree from i element ans swaps elements
* as longs as the order is not correct
*/
private void bubbleUp(int i){
while(hasParent(i) && arr[parent(i)]>arr[i]){
swap(parent(i),i);
i=parent(i);
}
}
/*
* fixes the heap property with bubble down approach
* goes down the tree from i ans swaps elements
* as lons as heap is not in right order
*
*/
private void bubbleDown(int i){
while(hasLeftChild(i)){
int smallerChildIndex =leftChildIndex(i);
if( hasRightChild(i) && arr[rightChildIndex(i)]<arr[ smallerChildIndex ]){
smallerChildIndex =rightChildIndex(i);
}
if(arr[i]>arr[ smallerChildIndex ]){
swap(i,smallerChildIndex);
i = smallerChildIndex;
}else{
break;
}
}
}
/**
* update element in index i with new key k
*
*
* @param i
* @param k
*/
public void updateKey(int i, int k){
arr[i] = k;
//
//if parent is bigger than new key,
//the heap property is corrupted, we need to fix
if (hasParent(i) && parent(i)>k){
bubbleUp(i);
//if left child is smaller than new key,
//the heap property is corrupted, we need to fix
}else if(hasLeftChild(i) && leftChildIndex(i)<k){
bubbleDown(i);
}else if(hasRightChild(i) && rightChildIndex(i)<k){
bubbleDown(i);
}
}
public boolean isEmpty(){
return lastElem <1;
}
/**
*
*
* sort the elements
* @return
*/
public Integer[] sort(){
Integer[]sortedArray = new Integer[lastElem];
int i=0;
while(!isEmpty()){
try {
Integer x = deleteMin();
sortedArray[i] =x ;
i++;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sortedArray;
}
private void swap(int i, int j){
int k = arr[i];
arr[i] = arr[j];
arr[j] = k;
}
public static void main(String[] args) {
MyHeap heap = new MyHeap(20);
//15, 50, -5, 34,90, 100, 0, 4, -9, - 45, 99 , 89
try{
heap.insert(15);
heap.insert(50);
heap.insert(-5);
heap.insert(34);
heap.insert(34);
heap.insert(90);
heap.insert(100);
heap.insert(0);
heap.insert(4);
heap.insert(-9);
heap.insert(-45);
heap.insert(99);
heap.insert(89);
System.out.println("array: " + Arrays.toString( heap.arr));
heap.updateKey(7, -100);
System.out.println("update key at pos 7 to -100: " + Arrays.toString( heap.arr));
Integer[] sortedArray = heap.sort();
//System.out.println("sorted array: " + Arrays.toString(sortedArray));
System.out.println("sorted array: " + Arrays.toString(sortedArray));
}catch(Exception exp){
System.out.println(exp.getMessage());
}
}
}