4

I'm working on a min heap implementation and am really new to the concept.

Using this as reference:
https://www.geeksforgeeks.org/building-heap-from-array/
https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/

I modified the code and came up with:

(this is the code I'm having problems with, all other code is irrelevant to my problem, at least so I think)

#define LCHILD(x) (2 * (x)) + 1
#define RCHILD(x) (2 * (x)) + 2
#define PARENT(x) ((x) - 1) / 2
typedef struct {
    int key;
    Event *element; // Assume NULL for this example
} Node;
void swap(Node **x, Node **y) {
    Node *temp = *x;
    *x = *y;
    *y = temp;
}
void heapify(void *pq, int n, int i) {
    Node **node = (Node**) pq;

    int smallest = i; // Initialize smallest as root 
    int left = LCHILD(i);
    int right = RCHILD(i); // right = 2*i + 2 
  
    if (left < n && (node[left])->key < (node[smallest ])->key) 
        smallest = left; 
  
    if (right < n && (node[right])->key < (node[smallest ])->key) 
        smallest = right; 
  
    if (smallest != i) { 
        swap(&node[i], &node[smallest ]); 
        heapify(node, n, smallest ); 
    } 
} 

int extractKey(void *pq, int *n) {
    Node **node = (Node**) pq;

    int minElement = (node[0])->key;

    node[0] = node[*n - 1];
    *n = *n - 1;

    heapify(pq, *n, 0);
    return minElement;
}
void insert(void *pq, int key, void *element, int *n) {
    Node **node = (Node**) pq;

    node[*n]->key = key;
    node[*n]->element = element;
    *n = *n + 1;

    int i = *n - 1;
    while ( (i != 0) && (node[PARENT(i)]->key > node[i]->key) ) {
        swap(&node[PARENT(i)], &node[i]);
        i = PARENT(i);
    }
}
  
int main() { 

    Node **pq = malloc (100 * sizeof(Node*));
    int i;
    for(i = 0; i < 100; i++) {
        pq[i] = malloc(sizeof(Node));
        pq[i]->element = malloc(sizeof(Event));
    }
  
    int n = 0; // heap size
  
    insert(pq, 0, NULL, &n);
    printHeap(pq, n); 
    
    insert(pq, 5, NULL, &n);
    printHeap(pq, n); 
    
    insert(pq, 1, NULL, &n);
    printHeap(pq, n); 
    
    insert(pq, 50, NULL, &n);
    printHeap(pq, n); 

    extractKey(pq, &n);
    printHeap(pq, n); 
    
    insert(pq, 10, NULL, &n);
    printHeap(pq, n); 

    return 0;
}

OUTPUT:

Array representation of heap is:
0 
Array representation of heap is:
0 5 
Array representation of heap is:
0 5 1 
Array representation of heap is:
0 5 1 50 
Array representation of heap is:
1 5 50 
Array representation of heap is:
1 5 10 10 // What happened here?

This only happens when I extract the minimum node and then add a new one. If I don't extract a node, then it works perfectly fine. Am I missing something?

EDIT 1: This is the print function I'm using. Forgot to add it in the initial post

(It's a modified version from the one found here:
https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/)

void printHeap(void *pq, int n) {
    Node **node = (Node**) pq;

    printf("Array representation of heap is:\n");
  
    for (int i = 0; i < n; ++i) { 
        printf("%d ", node[i]->key);
    }
     printf("\n");
} 

EDIT 2: I did some more testing. Here's what I got:


Inserted some print statements:
void insert(void *pq, int key, void *element, int *n) {
    Node **node = (Node**) pq;
    if(*n > 0) {
       printf("node[%d] = %d\n", *n-1, node[*n-1]->key);
    }

    node[*n]->key = key;
    printf("node[%d] = %d\n", *n, node[*n]->key);
    if(*n > 0) {
       printf("node[%d] = %d\n", *n-1, node[*n-1]->key);
    }
    node[*n]->element = element;
    *n = *n + 1;

    // move up until the heap property satisfies
    int i = *n - 1;
    while ( (i != 0) && (node[PARENT(i)]->key > node[i]->key) ) {
        swap(&node[PARENT(i)], &node[i]);
        i = PARENT(i);
    }
}

OUTPUT:

node[0] = 0
Array representation of heap is:
0 
node[0] = 0
node[1] = 5
node[0] = 0
Array representation of heap is:
0 5 
node[1] = 5
node[2] = 1
node[1] = 5
Array representation of heap is:
0 5 1 
node[2] = 1
node[3] = 50
node[2] = 1
Array representation of heap is:
0 5 1 50 
Array representation of heap is:
1 5 50 
node[2] = 50
node[3] = 10
node[2] = 10 // Huh? it should be 50
Array representation of heap is:
1 5 10 10 
KraveXL
  • 305
  • 3
  • 12
  • Gotta say, I'm glad you found the problem, and honestly, be proud. This was very well presented, and showed a *lot* of effort to root out the problem on your end (an increasingly rare quality in todays copy/paste world of "I found this on the internet, have no idea how it is supposed to work, but it doesn't; fix it." world. Worthy of the upticks it received. *Thank you*. – WhozCraig Feb 24 '21 at 23:59

1 Answers1

3

The problem is the line node[0] = node[*n - 1]; in extractKey. That is setting two of your node pointers to the same value, so you no longer have 100 unique node pointers. (As a consequence, it is also leaking memory.) Changing the line to swap(&node[0], &node[*n - 1]); should solve the problem.

Ian Abbott
  • 15,083
  • 19
  • 33
  • Thank you so much! I should of though of that! I'm not worried about memory leaks right now, I'm just trying to learn the concepts. – KraveXL Feb 24 '21 at 21:33
  • I was going over this carefully as a heap lesson for myself. Good catch! – Michael Dorgan Feb 24 '21 at 21:37
  • You should *always* worry about memory leaks, @KraveXL. Exercise good discipline in this area from the beginning, so that you have few (or no) leaks to find and fix later. Make a habit of it even in throwaway programs, to minimize the risk of forgetting when it matters. – John Bollinger Feb 24 '21 at 21:37
  • @JohnBollinger You have a point there. I'm usually more careful when I'm building something important. But I guess it's a good idea not to make a habit of scrappy programming etiquette. – KraveXL Feb 24 '21 at 21:42