Brian989
Brian989

Reputation: 15

Passing a heap as an argument

I tried to make several functions which passes a heap as a parameter through the code below. However, it did not turned out to be what I expected it to be.

#include<stdio.h>

void upHeap_min2 (int *heap, int index)
{
    if (index == 0)
        return;

    int parentIdx = getParentIdx(index);
    if (heap[index] < heap[parentIdx])
    {
        int temp = heap[index];
        heap[index] = heap[parentIdx];
        heap[parentIdx] = temp; 

        upHeap_min2(heap, parentIdx);
    }
}

void pushValue (int *heap, int count, int value)
{
    count++;
    heap[count] = value;
    upHeap_min2(heap, count);   
}

void view(int *heap, int count)
{
    printf("Values inside heap: ");
    for (int i = 0; i < count; i++)
    {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

int main()
{
    int heapDemo[101];
    int count = -1;
    pushValue(heapDemo, count, 30);
    pushValue(heapDemo, count, 20);
    pushValue(heapDemo, count, 40);
    pushValue(heapDemo, count, 90);
    pushValue(heapDemo, count, 10);

    view(heapDemo, count);

    return 0;
}

Function to get parent index:

int getParentIdx (int index)
{
    return (index-1)/2;
}

The code above should have printed

10 20 40 90 30

But instead it printed nothing. I have also thought to pass it as a double pointer as well but i did not work. Does this mean I cannot pass a heap as a parameter (which means i have to declare the heap as a global variable) or there is another way to do this?

Upvotes: 1

Views: 199

Answers (2)

Josep
Josep

Reputation: 162

This is a valid solution, let me explain

#include<stdio.h>

void upHeap_min2 (int *heap, int index)
{
    if (index == 0)
        return;

    int parentIdx = getParentIdx(index);
    if (heap[index] < heap[parentIdx])
    {
        int temp = heap[index];
        heap[index] = heap[parentIdx];
        heap[parentIdx] = temp; 

        upHeap_min2(heap, parentIdx);
    }
}
int getParentIdx (int index)
{
    return (index-1)/2;
}

void pushValue (int *heap, int *count, int value)
{
    *count = *count + 1;
    heap[*count] = value;
    upHeap_min2(heap, *count);   
}

void view(int *heap, int *count)
{
    printf("Values inside heap: ");
    for (int i = 0; i <= *count; i++)
    {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

int main()
{
    int heapDemo[101];
    int conter = -1; //new var
    int *count = &conter;
    pushValue(heapDemo, count, 30);
    pushValue(heapDemo, count, 20);
    pushValue(heapDemo, count, 40);
    pushValue(heapDemo, count, 90);
    pushValue(heapDemo, count, 10);

    view(heapDemo, count);

    return 0;
}

The main error is that you were passing counter as simple variable, and you should do it as a pointer, this causes that counter never get incremented, so I created a pointer to that variable and now counter is incremented and it prints what you expect. In addition I have modified the body of the for you were using to print the values

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51815

Your pushValue function takes the count argument by value (which means the function receives a copy of the data), so it is never modified in the main function. Instead, you should pass count as a pointer, and (thus) will need to dereference it inside the function:

void pushValue(int* heap, int* count, int value)
{
    ++(*count);
    heap[*count] = value;
    upHeap_min2(heap, *count);
}

Then, in main, you should call it using the address of count:

    pushValue(heapDemo, &count, 30); // And similarly for the other calls

Also, the loop in your view function stops one short of the end. Change the loop limit to i <= count. (This function uses count but doesn't modify it, so passing by value is OK.):

void view(int* heap, int count)
{
    printf("Values inside heap: ");
    for (int i = 0; i <= count; i++) {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

Feel free to ask for further clarification and/or explanation.

Upvotes: 3

Related Questions