I have the following code for binary heaps:
Code:
#define Parent(x) ((x - 1) / 2)
#define Left(x) ((2 * x) + 1)
#define Right(x) ((2 * x) + 2)
#define MAX 10
void swapElem(int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void insertHeap(Heap *h, int elem) {
if(h->size == MAX) return;
h->elem[h->size] = elem;
h->size++;
int idx = h->size - 1;
while(idx && h->elem[Parent(idx)] > h->elem[idx]) {
swapElem(h->elem + Parent(idx), h->elem + idx);
idx = Parent(idx);
}
}
But I don't understand the line:
Code:
swapElem(h->elem + Parent(idx), h->elem + idx);
I mean, h->elem is an array, how come h->elem + idx (for instance), produce a number? I'm adding idx to h->elem but h->elem is not a value it's an array.
What does it mean?