0

i am trying to implement an ADT of maxheap in c. i am having trouble with the heapify function.

this is my h file

typedef struct t_MaxHeap* MaxHeap;
typedef struct t_Node* Node;
typedef void *element;

typedef element (*copyFunc)(element);
typedef void (*PrintFunc)(element);
typedef void (*freeFunc)(element);
typedef element(*compareFunc)(element,element);
typedef element (*containFunc)(element);

this is my struct (which is located in the c file)

#include "MaxHeap.h"

struct t_Node
{
    element value;
};

struct  t_MaxHeap{
    int size;
    int capcity; //const
    char *name;
    copyFunc copyFunction;
    freeFunc freeFunction;
    PrintFunc printFunction;
    containFunc containFunction;
    compareFunc compareFunction;
    Node* array;
};

im creating the heap with no problem.. then i defined a swap function which will be used in the heapify..

void swap (MaxHeap new_MaxHeap,Node n1,Node n2 )
{
    Node temp= n1;
     n1=n2;
     n2=temp;
}

now for the heapify function i have problem with the heapify function and im not sure how to fix it.

void heap_maxify(MaxHeap new_MaxHeap,element i)
{
    element largest=i;
    element a=new_MaxHeap->array[2*i];
    element b=new_MaxHeap->array[a+1];

    element b;
    b=a+1;
    if(a <=new_MaxHeap->size && new_MaxHeap->compareFunction(new_MaxHeap->array[largest],new_MaxHeap->array[a])==1)//1 means  a is bigger then largest
    {
        largest=a;
    }
    if(b <=new_MaxHeap->size && new_MaxHeap->compareFunction(new_MaxHeap->array[largest],new_MaxHeap->array[b])==1) //1 means b is bigger then largest
        {
            largest=b;
        }

    if (largest !=i){
        swap(new_MaxHeap,i,largest);
    }

}

i think my two if statement are good.. the problem is with setting a as the element which is in index 2i and b which is in index 2i+1 from the currrent element

  • 1
    What is `Node`? Please show the relevant typedefs. Your swap function only swaps copies of values, parameters are passed in C by value. – KamilCuk Dec 14 '19 at 00:10
  • @kamilcuk i posted the struct of the d_node which is simply a generic node which i defined in my H file.. i will add it so you can see.. i also believe it does not swap copy because i set them up to be pointers in the h file but if im wrong please tell me – c00kie_monster Dec 14 '19 at 16:54
  • There are many problems with your code, and my gut tells me there are many more. I kind-of doubt that you "are creating the heap with no problem". And in your `swap` functions you are swapping _copies_ of values of pointers, not the pointers themselves nor the values. The `a <=new_MaxHeap->size` and `b <=new_MaxHeap->size` is comparing a `void *` with `int` using `<=`, it's most probably invalid. What exactly is your question? – KamilCuk Dec 14 '19 at 17:03
  • @kamilcuk what you mention is exactly my problem. for max heap i need to do heapify.. which compares the current element with his left child (2i) right child(2i+1).. but like you say im trying to compare element with int because i dont know how to get the element which is in index 2i and 2i+1. and thank you for your comments on the swap function. i will fix it. – c00kie_monster Dec 14 '19 at 19:17
  • `i dont know how to get the element` - It depends on how you allocated it. The `MacHeap` is a pointer and `array` inside `t_MaxHeap` is a double pointer, it's hard to predict what data structure are you modeling with it (2D array? array of pointer to elements? Why so many pointers?). I (strongly) suggest to remove pointers from typedefs. Pointer typedef is considered bad style and make the code hard to debug, especially for newcomers. Och, I didn't see that, the `new_MaxHeap->array[a+1];` is indexing the array using a `void*` pointer value - this is definitely very invalid. – KamilCuk Dec 14 '19 at 19:30

0 Answers0