Nepedia
Back to Discover
Independent Reader
💻

Heap (data structure)

technology Maturity 11-13 Next article →

A heap is a way to organize data in a tree shape.

Max-Heap-new
Max-Heap-new

A tree is a set of parts called nodes. These nodes connect to each other like branches. There are two main kinds of heaps. In a max heap, the biggest value stays at the top. This top node is called the root. In a min heap, the smallest value stays at the root.

Heap-as-array
Heap-as-array

Heaps are very fast at finding the best item. This is useful for a priority queue. A priority queue is a list where some items are more important than others. Heaps are also used in heapsort to sort lists. They help in graph algorithms too.

Computers often store heaps in an array. An array is a simple list of items. The tree shape is hidden inside the list. To keep the heap in order, we use a way called sifting. If we add a new item, we use sift-up. This moves the item up the tree. If we remove an item, we use sift-down. This moves an item down to its right spot. This is like using a sieve to sort things.

189 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (2)
File:Max-Heap-new.svg
Max-Heap-new.svg
File:Heap-as-array.svg
Heap-as-array.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.