Nepedia
Back to Discover
Fluent Reader
💻

Heap (data structure)

technology Maturity 11-13 Next article →

A heap is a special way to organize information in a tree shape.

Max-Heap-new
Max-Heap-new

In computer science, this structure helps us manage items based on their importance. We call this importance a priority. A heap follows a strict rule called the heap property. In a max heap, every parent node has a value greater than or equal to its children. In a min heap, the parent node is less than or equal to its children. The very top node of this tree is called the root. The root always holds the item with the highest or lowest priority.

Heap-as-array
Heap-as-array

Heaps work through a step-by-step way of moving items to keep them in order. When you add a new item, you put it in the first empty space at the bottom. Then, you use a process called sift-up to move it toward the top. This is like using a sieve to let things settle into the right place. If you remove the root, you take the last item from the bottom and put it at the top. To fix the order, you use sift-down to move that item toward the bottom. This constant balancing ensures the most important item is always ready at the root.

This specific way of organizing data was introduced by J. W. J. Williams in 1964. He created the binary heap to help with a sorting method called heapsort. A binary heap is a special kind of tree where each node can have at most two children. It is designed to be a complete tree, which means it stays as short as possible. This short height makes finding the most important item very fast. Because it is so efficient, many programmers still use these rules today.

There are many real numbers and ways to measure how a heap works. For example, a heap with N nodes has a height of log base 2 of N. This mathematical rule shows how quickly the tree grows. Heaps are often built inside an array, which is a simple list of items. In an array, the parent and child nodes are found using their index numbers. If a parent is at index i, its children are at indices 2i and 2i plus 1. This math allows the computer to move through the tree without needing extra memory.

Heaps are useful for many things you might see in technology. They are the main way to build a priority queue, which manages tasks by importance. Heaps are also used in Dijkstra's algorithm to find the shortest path between points. This helps computers solve complex problems in graph algorithms. You can find heaps in many programming languages like C++, Java, and Python. They help everything from sorting lists to managing timer events work smoothly.

463 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.