# Thread: Converting a list to a heap... (probably easy!)

1. ## Converting a list to a heap... (probably easy!)

I've been presented with the question...

"Convert the following list into a heap: 12 19 33 26 29 35 22"

I figure that this can be accomplished a few ways.
1) moved into memory that is dymanically allocated
2) pushed onto a stack (a specific implementation of step I presume)

Am I correct in this assumption?

Mike

2. If the only operations required is to create a heap, I'd just dynamically create an array (size = 7 in your example) and then treat that as if it were a heap by inserting the values in the correct slot.

3. So would that be
Code:
` int array = malloc(sizeof(int)*7);`
right?

Thanks,
Mike

4. No. It'd be

int *array = malloc(sizeof(int) * 7);

However, the logic may be easier if you defined a structure with left and right pointers along with the int value. There's many ways to implement a heap. Do a search for "data structures heap". You'll find far better illustrations than what we can provide you here.

5. Usually though it's best to implement it with
int* array = malloc(sizeof(int) * 8); and leave the array[0]
unused. This is so that you can define
PARENT(i) = i / 2, LEFT_CHILD(i) = i * 2, RIGHT_CHILD(i) = i * 2 +1. Copy the list to the array. And then use the standard
algorithm to build a heap from a ordinary array.