Arrays as Heaps
Because finding the last node is so critical to the heap’s operations, and because we want to make sure that finding the last node is efficient, heaps are usually implemented using arrays.
While until now we always assumed that every tree consists of independent nodes connected to each other with links (just like a linked list), you’ll now see that we can also use an array to implement a heap. The heap itself can be an abstract data type that really uses an array under the hood.
The diagram shows how an array is used to store the values of a heap.
The way this works is that we assign each node to an index within the array. In this diagram, the index of each node is found in a square below the node. If you look carefully, you’ll ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access