ansme
ansme

Reputation: 463

How to implement heap with recursion

I've been trying to implement heap(this one is max heap) with recursion in "C" language but the problem is I'm not getting an appropriate output from it, In the code below the insert function takes the input data and the address of the array which is created for heap as an argument and then checks if the data is greater than it's parent node or not,and if the data turns out to be greater then it replaces the smaller value which is placed on the index of the parent with the higher value which is placed on the index of the data and it repeats the process until it satisfies the property of max heap or until it reaches the root, Here the index of parent is represented as heap[i-1]/2 and the index of the newly added data is just "i" i.e heap[i] Suppose I input 20 and after that if I input a larger value like 30 then the output should be 30 20 but instead I only get the output as 30 and If decide to add another larger value (say 40) then it replaces the 30 and the output becomes only 40. here's the code ->

int i = 0;
void insert(int heap[],int data)
{
    int j = 0;
    if(i == 0)
        {
            heap[i] = data;
        }
        else if(heap[(i-1)/2] < data)
        {
            heap[i] = heap[(i-1)/2];
            i = (i-1)/2;
            insert(heap,data);

        }
}
void display(int heap[],int size)
{
    system("cls");
    for(int i=0,j=1;i<size;i++,j++)
    {
        printf("%d.) %d\n",j,heap[i]);
    }
}

void main()
{
    int heap[5],men,data,c;
    while(1)
    {
        system("cls");

        printf("1.) Insert\n");
        printf("2.) Display\n");
        printf("3.) exit\n");
        printf("Enter your choice : ");
        scanf("%d",&men);
        switch(men)
        {
            case 1 : printf("Enter data : ");
                     scanf("%d",&data);
                     heap[i] = data;
                     insert(heap,data);
                     i++;
                     printf("%d successfully added to heap!",data);
                     break;

            case 2 : display(heap,i);
                    break;

            case 3 : exit(0);
            break;

            default : printf("Invalid choice!");
                      while((c=fgetc(stdin))!='\n'){}
                      break;            
        }
        getch();
    }

}

Upvotes: 0

Views: 842

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 133975

The problem with your insert method is that you never add anything to the array, except when i==0.

The algorithm for adding an item to a min heap is:

Add new item to the end of the array.
While the new item is smaller than its parent
    swap new item with parent item

Translated to pseudo code and optimized a bit, that becomes:

count = count + 1
i = count - 1
parent = (i-1)/2
while (data < a[parent])
    a[i] = a[parent])
    i = parent
    parent = (i-1)/2
a[i] = data

The recursive version is pretty much the same.

Your code never increases the count, so the heap can't grow.

Upvotes: 1

Related Questions