I'm trying to create a Binary Min Heap in C using arrays. I wrote the code and tried multiple times to execute it with "pen and paper" and it seems to work. Can you please help me to understand why it fails instead?
Here is the library code:
#include <stdlib.h>
void swapInt(int *a, int *b) {
int aux = *a;
*a = *b;
*b = aux;
}
typedef struct intHeapArray {
int *array;
int size;
} IntHeapArray;
IntHeapArray newIntHeapArray (int dim) {
IntHeapArray new;
new.array = (int*) malloc(sizeof(int)*dim);
new.size = 0;
return new;
}
int findFather (int index) {
return (index - 1) / 2;
}
int findLeftChild (int index) {
return index * 2 + 1;
}
int findRightChild (int index) {
return index * 2 + 2;
}
int minFatherChilds (IntHeapArray h, int index) {
int leftchild = findLeftChild(index);
int rightchild = findRightChild(index);
if (rightchild >= h.size)
rightchild = leftchild;
if (h.array[index] > h.array[leftchild])
index = leftchild;
if (h.array[index] > h.array[rightchild])
index = rightchild;
return index;
}
void reorganizeIntHeapArray (IntHeapArray *h, int index) {
int father, leftchild, child;
father = findFather(index);
while (index > 0 && h->array[index] < h->array[father]) {
swapInt(&h->array[index], &h->array[father]);
index = father;
}
leftchild = findLeftChild(index);
while (leftchild < h->size && index != minFatherChilds(*h, index)) {
child = minFatherChilds(*h, index);
swapInt(&h->array[index], &h->array[child]);
index = child;
}
}
void enqueueIntHeapArray (IntHeapArray *h, int val) {
h->array[h->size] = val;
h->size++;
reorganizeIntHeapArray(h, h->size - 1);
}
Here is the calling from the main script:
#include <stdio.h>
#include "heap.h"
void printIntArray (int *a, int dim) {
int i;
printf("[");
for (i = 0; i < dim - 1; i++)
printf("%d, ", a[i]);
printf("%d]\n", a[dim-1]);
}
int main () {
IntHeapArray h;
int n, i, val;
printf("How many value would you like to add to the heap? ");
scanf("%d", &n);
h = newIntHeapArray(n);
printf("Insert values:\n");
for (i = 0; i < n; i++) {
scanf("%d", &val);
enqueueIntHeapArray(&h, val);
}
printf("This is your heap:\n");
printIntArray(h.array, h.size);
return 0;
}
The code compiles. Try it with this input: 6 3 8 9 2 0.
It will print: [3, 2, 0, 9, 6, 8] that is obviously wrong. But I don't really understand where I'm wrong.