The most appropriate way to implement a heap is with an array rather than a linked list, why is this?
I don't completely comprehend why this is? is it because it is easier to traverse?
The most appropriate way to implement a heap is with an array rather than a linked list, why is this?
I don't completely comprehend why this is? is it because it is easier to traverse?
It doesn't make any sense at all to implement a heap as a linked list. (The most common kinds of) heaps are inherently binary trees. You can store a heap in an array because it's easy to compute the array index of a node's children: the children of the node at index K live at indices 2K+1 and 2K+2 if indices start at 0 (or at indices 2K and 2K+1 if indices start at 1). It's massively more efficient to find the Kth element of an array than the Kth element of a linked list.
Advantages of storing a heap as an array rather than a pointer-based binary tree include the following.
array based implementations are much easier to work with, as they can be iterated through internally making element updates and destruction much simpler. However they only work on heaps with a defined number of children on each node (like binary heaps). If you are doing something like a pairing heap you will need pointers.