-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
90 lines (56 loc) · 1.59 KB
/
heap.c
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
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
void insertToHeap(double key,int currentSize,int N,double *heap){
if( currentSize > N){
// NEVER HAPPEN.
printf("Overflow...\n");
exit(0);
}
heap[currentSize] = key;
int i = currentSize;
// p=parent
int p = (i-1) / 2;
while ((i > 0) && (heap[i] > heap[p])){
swap(&heap[i], &heap[p]);
i = p;
p = (i-1) / 2;
}
}
double deleteMaxHeap(double *heap, int currentSize,int N){
if( currentSize > N){
printf("Overflow\n");
return 0;
}
double maxKey=heap[0];
heap[0]=heap[currentSize-1]; // copy the last key to the root
int i=0, l=1, r=2; // l, r current tree-node children
while (l < currentSize-1){
int maxValueChild = l; // maxValueChild : child with the biggest key
if (r <= currentSize && heap[r] > heap[l])
maxValueChild=r;
if (heap[i] >= heap[maxValueChild])
break;
swap(&heap[i],&heap[maxValueChild]); // swap values parent - child
i=maxValueChild;
l=2*i + 1;
r=l+1; // the new i and his children
}
heap[currentSize-1]=-1;
return maxKey;
}
void printHeap(int N,double *heap){
int i;
printf("\n\n");
for(i=0; i<N; i++){
if(heap[i] != -1)
printf("%.2f ",heap[i]);
}
printf("\n\n");
}
double maxNumberOfHeap(double *heap){
return heap[0];
}
void destroyHeap(double *heap){
free(heap);
}