Academics · Study Skills · A tree stored in a plain list

A whole tree
in one row of boxes.

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 data structures visualizer

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.

What you get

Academics · Study SkillsOpen to everyone · no sign-in

What you finish with
The levels, leaves and internal items of a binary heap of n items, the parent and children of any position and a table of heap sizes.
Who it is for
High school and early college computer science students, and the families, teachers and tutors who help them with trees and priority queues.
Format
Browser calculator for a phone, tablet or computer, by touch or keyboard. Prints a summary; downloads a spreadsheet (.csv); export and import a file.
Access
Open to everyone with the link: no account, no sign-in, nothing to buy and nothing to install. Everything you type stays in this browser; nothing is sent to Caveat.
Where it lives
The Study Skills space in Caveat Academics.
Support
Questions, a correction or a request: team@caveatacademic.com.

How to use it

  1. Name the heap.
  2. Type the number of items.
  3. Type a position in the array.
  4. Read the levels, leaves, parent and children.
  5. Try other heap sizes in the table.

Explore a heap

How the figures are worked out

Positions in an array stand for a tree.

The worked example: a heap of 11 items, position 5Levels4to hold 11 itemsParent of 525 ÷ 2, downChildren of 510, 112 × 5 and 11Leaves611 − 5The worked example: a heap of 11 items, position 5Levels4to hold 11 itemsParent of 525 ÷ 2, downChildren of 510, 112 × 5 and 11Leaves611 − 5
The tree has four levels, and position 5 sits under position 2 with children at 10 and 11.

A tree in an array

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.

Why the height is small

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.

Sources

  1. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to Algorithms (4th ed.). MIT Press: Chapter 6, Heapsort, on heaps stored as arrays with parent and child index formulas.
  2. Knuth, D. E. (1998). The Art of Computer Programming, Volume 3: Sorting and Searching (2nd ed.). Addison-Wesley: heaps and heapsort.
  3. College Board. AP Computer Science A Course and Exam Description: arrays, array lists and searching and sorting. apcentral.collegeboard.org.
  4. Any introductory data structures text: binary heaps and priority queues.

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.

Written with AI, then checked before it is published. How we checkFound a mistake? Report it and we fix it, or refund you.Cancel anytime.