A heap is a special tree-shaped data structure used to always keep track of the “best” (smallest or largest) element very efficiently. In most programming contexts, when people say “heap,” they are talking about this data structure or about a memory area used for dynamic allocation.

Heap in data structures

In computer science, a heap (data structure) is usually a complete binary tree that satisfies the heap property. That means each node is ordered relative to its children, but the tree as a whole is only partially ordered, not fully sorted.

  • In a max-heap , every parent’s key is greater than or equal to its children, so the maximum element is at the root.
  • In a min-heap , every parent’s key is less than or equal to its children, so the minimum element is at the root.
  • Heaps are typically stored in arrays, using index arithmetic to find parents and children instead of explicit pointers.

What heaps are used for

Heaps shine when a program needs to repeatedly access the current “top- priority” item while still allowing fast insertions.

  • They are a standard way to implement priority queues , where push and pop of the min/max element are both efficient.
  • They are used in heap sort , which repeatedly extracts the root to produce a sorted sequence.
  • Modern systems use heap-like structures inside features such as caching and autocomplete, where quick access to best candidates matters.

Heap vs. heap memory

The word “heap” also refers to a region of program memory used for dynamic allocation, which is different from the heap data structure.

  • Heap memory is a flexible memory area from which programs request and free variable-sized blocks at runtime.
  • The heap data structure is an abstract structure for ordering elements by priority; it may or may not live in heap memory.

So, “what is a heap?” usually means the tree-based priority structure in algorithms, but in systems/programming it can also mean the dynamically managed memory area.