#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int allocated; /* current allcoation of array */
int filled; /* number of items present in the binheap */
int *array; /* array of values */
} BinaryHeap;
/* Init allocates the structure BinaryHeap and
* also the membre array with the given size
* it also fill allocated (size) and intializes
* filled to 0 */
BinaryHeap * Init(int size);
/* InsertValue insert value into the binary heap
* the array is reallocated if necessary (allocated changed
* with respect to the new size )
* filled is incremented by 1 */
void InsertValue(BinaryHeap * heap, int value);
/* ExtractMAx returns 0 if the binary heap is empty
* otherwise it return 1 and fills *val with the maximum
* value present in the binary heap
* filled is decremented by 1 and the max value is removed
* from the binary heap */
int ExtractMax(BinaryHeap * heap, int * val);
/* Destroy frees the structure and the array */
void Destroy(BinaryHeap * heap);
void Print(BinaryHeap * heap);
int main(void)
{
char lecture[100];
int val;
BinaryHeap * heap;
heap = Init(10);
while (strcmp(lecture
,"bye")!=0) { if (strcmp(lecture
,"insert")==0) { val
= strtol(lecture
,NULL
,10); InsertValue(heap,val);
} else if (strcmp(lecture
,"extract")==0) { if(ExtractMax(heap,&val))
{
}
else if (strcmp(lecture
,"print")==0) { Print(heap);
}
}
}
Destroy(heap);
return 0;
}
BinaryHeap * Init(int size)
{
BinaryHeap * heap;
heap
= malloc(sizeof(BinaryHeap
)); heap -> allocated = size;
heap -> filled = 0;
heap
-> array
= malloc(heap
-> allocated
*sizeof(int)); return heap;
}
void InsertValue(BinaryHeap * heap, int value)
{
if(heap -> filled == heap -> allocated)
{
heap -> allocated = heap -> allocated + 1;
heap
-> array
= realloc(heap
-> array
,heap
-> allocated
*sizeof(int)); }
heap -> array[heap -> filled] = value;
int i = heap -> filled;
while (i!=0 && heap -> array[(i-1)/2] < value)
{
heap -> array[i] = heap -> array[(i-1)/2];
heap -> array[(i-1)/2] = value;
}
heap -> filled = heap -> filled + 1;
}
int ExtractMax(BinaryHeap * heap, int *res)
{
if (heap -> filled == 0)
{
return 0;
}
else
{
res = (heap -> array[0]);
heap -> filled = heap -> filled -1;
heap -> array [0] = heap -> array[heap -> filled];
int i = 0;
int tampon;
while(2*i+2<heap -> filled && (heap -> array[i] < heap -> array[2*i+1] || heap -> array[i] < heap -> array[2*i+2]))
{
if (heap -> array[2*i+1] < heap -> array[2*i+2])
{
tampon = heap -> array[2*i+2];
heap -> array[2*i+2] = heap -> array[i];
heap -> array[i] = tampon;
i = 2*i+2;
}
else if (heap -> array[2*i+1] > heap -> array[2*i+2])
{
tampon = heap -> array[2*i+1];
heap -> array[2*i+1] = heap -> array[i];
heap -> array[i] = tampon;
i = 2*i+1;
}
}
if(2*i+1<heap -> filled && heap -> array[i] < heap -> array[2*i+1])
{
tampon = heap -> array[2*i+1];
heap -> array[2*i+1] = heap -> array[i];
heap -> array[i] = tampon;
i = 2*i+1;
}
return 1;
}
}
void Destroy(BinaryHeap * heap)
{
/*for (int i=(heap -> filled)-1 ; i = 0 ;i--)
{
free(heap -> array[i]);
}*/
}
void Print(BinaryHeap * heap)
{
for(int i =0; i< heap -> filled; i++)
{
printf("%d\r\n",heap
-> array
[i
]); }
}