Nepedia
Back to Discover
Advanced Reader
💻

Heap (data structure)

technology Maturity 11-13 Next article →

In computer science, a heap is a specialized tree-based data structure. It is designed to organize information so that the most important item is always easy to find. This importance is known as priority. A heap must always follow a specific rule called the heap property. In a max heap, every parent node must have a value greater than or equal to its children. In a min heap, the parent node must have a value less than or equal to its children.

Max-Heap-new
Max-Heap-new

The node at the very top of the tree is called the root node. This root always holds the item with the highest or lowest priority.

Heaps are often used to implement an abstract data type called a priority queue. While a priority queue is a concept for managing tasks by importance, a heap is a very efficient way to make it work. It is important to note that a heap is not a fully sorted structure. Instead, it is considered partially ordered. The heap relation only applies to the relationship between a node and its parents or grandparents. There is no required ordering between siblings or cousins in the tree.

Heap-as-array
Heap-as-array

A common version of this structure is the binary heap. In a binary heap, the data forms a complete binary tree. This means every level of the tree is filled, making the tree as short as possible. For a heap with N nodes, the height is always log base 2 of N. This mathematical property makes the heap very efficient for certain tasks. The binary heap was introduced by J. W. J. Williams in 1964. He developed it specifically to support the heapsort algorithm, which is a method for sorting data.

Computers usually implement heaps using an array rather than a complex tree of pointers. This is efficient because it requires no additional memory beyond the space used to store the keys. The tree structure is implicit, meaning it is hidden within the way the array is accessed. Each element in the array represents a node. The relationship between parents and children is determined by their indices. If a parent node is located at index i, its children are at indices 2i and 2i + 1. Its own parent can be found at index i divided by 2. This simple math allows the computer to move through the tree quickly.

To keep the heap property intact, the structure must undergo balancing operations. When a new element is inserted, it is placed in the first available free space at the end of the array. If this breaks the heap property, the computer performs a sift-up operation. This moves the node up the tree until it reaches the correct level, much like a sieve. When the root node is extracted, the last element in the heap is moved to the root position. To restore order, the computer performs a sift-down operation. This moves the new root down the tree until the heap property is reestablished.

There are several specific operations that can be performed on a heap. Basic operations include find-max or find-min, which is also called a peek. You can also use insert to add a new key, or extract-max to remove the highest priority item. Another efficient operation is replace, which removes the root and inserts a new key in one step. This is faster than a separate pop and push because it only requires one balancing step. For building a heap, the heapify operation can turn an existing array into a heap. You can also merge two heaps together using a process called meld or union.

Heaps are essential for many advanced computer science applications. They are used in heapsort, which is a highly efficient sorting method because it works in-place. Heaps are also vital for selection algorithms, allowing constant time access to the minimum or maximum element. In graph algorithms, heaps help reduce running time. For example, Dijkstra's algorithm uses heaps to find the shortest path between points. They are also used in K-way merges to combine many sorted data streams into one. Many programming languages, including C++, Java, Python, and Rust, include built-in support for heap structures.

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