Log in Sign up
Back to Discover
💻

Heap (data structure)

technology Maturity 11-13

A heap is a special way to sort things.

Max-Heap-new.svg
Max-Heap-new.svg
It keeps the best thing at the top. This helps us find it fast. It is very useful for computers. Do you like to stay organized?
Heap-as-array.svg
Heap-as-array.svg

37 words

A heap is a way to group things.

Max-Heap-new.svg
Max-Heap-new.svg
It looks like a tree with many branches. In one kind, the biggest item stays at the top. In another kind, the smallest item stays at the top. This makes it easy to find the best item fast.

Computers use heaps to stay organized. They can add new items to the list. They can also take items away. If a new item goes in, it moves to the right spot. This is like using a sieve to sort things.

Heap-as-array.svg
Heap-as-array.svg
It is a very smart tool for computers.

97 words

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

Max-Heap-new.svg
Max-Heap-new.svg
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.svg
Heap-as-array.svg

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

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

Max-Heap-new.svg
Max-Heap-new.svg
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.svg
Heap-as-array.svg

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.

460 words

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.svg
Max-Heap-new.svg
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.svg
Heap-as-array.svg

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.

695 words
🖼️ Images & Media (2)
File:Max-Heap-new.svg
Max-Heap-new.svg
File:Heap-as-array.svg
Heap-as-array.svg
Up Next
💻
Heapsort
Technology
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.