Academics · Study Skills · A tree stored in a plain list
A binary heap is a tree stored in a plain array: the root is position 1, and the children of position i sit at 2i and 2i + 1. Type the number of items and a position. The page gives the levels, the leaves, the internal items, the parent and the children of your position, and how far up the root is.
The worked example is a heap of 11 items, looking at position 5. Nothing you type leaves this browser.
Loading the calculator. If nothing appears, turn on JavaScript.
Positions count from 1. The example is made up.
Academics · Study SkillsOpen to everyone · no sign-in
Positions in an array stand for a tree.
Storing a complete binary tree in an array needs no pointers: the position of a parent or child is found by arithmetic. That is why a heap, which keeps its smallest or largest item at the root, is the usual way to build a priority queue. The AP Computer Science A course covers arrays and trees and this index arithmetic comes up in the study of heap sort.
Each level of a heap holds twice as many positions as the one above, so a million items fit in just 20 levels. Putting a new item in or taking the top item out moves it along one path of at most that many steps, which is why heap operations take time proportional to the logarithm of the size.
Figures worked from your own numbers. Nothing you type is sent to Caveat. The worked example is made up. Your entries stay in this browser. Support: team@caveatacademic.com.