Implementing min Heap using an array [closed]

北城余情 提交于 2019-12-26 21:42:12

问题


I am trying to implement min Heap in C.

// Heaps:
// node i -> 2i and 2i+1 children
// 2i//2 = i and 2i+1//2 = i same parents

#include <stdio.h>
#include <stdlib.h>

#define max 100

int heap[max];
int size = 0;

void heapifyUp(){ // last element
    int i = size;

    while(i){ // 
        int parent = i/2;
        if (heap[parent]<heap[i]){
            int t = heap[parent];
            heap[parent] = heap[i];
            heap[i]=t;
        }        
    }
}

void heapifyDown(){ // top element
    int i = 0;

    while(i<=size){
        int c1 = 2*i;
        int c2 = 2*i + 1;
        int t = 0;
        if (heap[c1]>=heap[c2]){
            t = c2;
        }
        else{
            t = c1;
        }
        int temp = heap[i];
        heap[i] = heap[t];
        heap[t] = temp;
        i = t;
    }
}

void insert(int key){
    size = size + 1;
    heap[size] = key;
    heapifyUp();
}

int returnMin(){
    return heap[0];
}

int deleteMin(){
    int t = heap[0];
    heap[0] = heap[size];
    size = size - 1;
    heapifyDown();
    return t;
}

void printHeap(){

    int i = 0;
    while(i<=size){
        printf("%d",heap[i]);
        i = i + 1;
    }
}

int main(){
    insert(10);
    insert(20);
    insert(11);
    insert(7);
    insert(18);

    printHeap();
    printf("%d",deleteMin());

    insert(110);
    insert(-7);
    insert(15);

    printf("%d",deleteMin());
}

The issue is that when I run the program, I get no output and the program doesn't terminate.

I think i have implemented the logic correctly.

Using debugger with C is hard as I am on a Mac (doesn't support Codeblocks, never really understood how to use gdb, am just using the built-in gcc compiler on a text editor), so I am stuck in this issue.

Thanks for your help.


回答1:


Your code have some problems, and here are few of them:
1. As Jorge mentioned, in your heapifyUp() function you never go up, it just stays there and enters infinity loop.
2. In heapifyDown() function you have out of bounds problem. You need to check if its children are valid. And it has logic problem.
3. And also, please decide if you want to use 0 indexed array or 1 indexed array. Because some of your functions think it is 0 indexed and some think it is 1 indexed. I made corrections according to 1 indexed array. If you want I can change it to 0 indexed or it could be your homework.

I tried to do my best, but some corrections are still need to be made:

#include <stdio.h>
#include <stdlib.h>

#define max 100

int heap[max];
int size = 0;

void heapifyUp(){ // last element
    int i = size;

    while(1){ // 
        int parent = i/2;
        if (parent > 0 && heap[parent] > heap[i]){
            int t = heap[parent];
            heap[parent] = heap[i];
            heap[i]=t;
            i = parent;
        } else {
            break;
        }
    }
}

void heapifyDown(){ // top element
    int i = 1;

    while(i<size){
        int c1 = 2*i;
        int c2 = 2*i + 1;
        int t;
        if (c1 <= size) {
            t = c1;
        } else {
            break;
        }
        if (c2 <= size && heap[c1] > heap[c2]){
            t = c2;
        }

        if(heap[i] >= heap[t]) break;

        int temp = heap[i];
        heap[i] = heap[t];
        heap[t] = temp;
        i = t;
    }
}

void insert(int key){
    size = size + 1;
    heap[size] = key;
    heapifyUp();
}

int returnMin(){
    return heap[1];
}

int deleteMin(){
    int t = heap[1];
    heap[1] = heap[size];
    size = size - 1;
    heapifyDown();
    return t;
}

void printHeap(){
    int i = 1;
    while(i <= size){
        printf("%d ", heap[i]);
        i++;
    }
    printf("\n");
}

int main()
{
    insert(10);
    insert(20);
    insert(11);
    insert(7);
    insert(18);

    printHeap();
    printf("%d\n",deleteMin());

    insert(110);
    insert(-7);
    insert(15);

    printHeap();
    printf("%d\n",deleteMin());
    return 0;
}



回答2:


In heapifyUp you have a while(i) statement. "i" gets initialized to 1 and is never modified. And you don't have any "return" or "break" inside that loop. So the condition remains true forever.



来源:https://stackoverflow.com/questions/48872514/implementing-min-heap-using-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!