11. Heap
Description
A Heap is a tree-based data structure that implements a priority queue. The key characteristic of a heap is maintaining the heap property, which guarantees that every parent node satisfies a specific ordering relationship with respect to its child nodes.
There are two primary types of heaps:
- Max-Heap: The parent node is always greater than or equal to its children.
- Min-Heap: The parent node is always less than or equal to its children.
A heap is represented as a complete (or nearly complete) binary tree, where each node has at most two children. Because the tree is always balanced, insertion and extraction operations can be executed efficiently.
Types of Heaps
- Max-Heap: In a max-heap, the value of each parent node is always greater than or equal to the values of its children. Consequently, the maximum element is always located at the root (top) of the heap.
- Min-Heap: In a min-heap, the value of each parent node is always less than or equal to the values of its children. Thus, the minimum element is always located at the root of the heap.
Heap Operations
- Insert:
- An element is added to the end of the heap (maintaining the complete binary tree structure) and then "bubbled up" (
heapify-up) to restore the heap property.
- Extract-Max / Extract-Min:
- Max-Heap: The root node containing the maximum value is extracted. It is replaced by the last element in the heap, followed by a "sift down" (
heapify-down) operation to restore heap ordering. - Min-Heap: The root node with the minimum value is extracted, followed by the same re-balancing procedure.
- Peek:
- An operation that retrieves the value of the root node without removing it.
Algorithms Based on Heaps
- Heap Sort: Used to sort elements using a max-heap or min-heap. The algorithm builds a heap and iteratively extracts the maximum or minimum elements to form a sorted array.
- Dijkstra's Algorithm: Used to find the shortest path in a weighted graph by repeatedly extracting the vertex with the minimum distance.
- Huffman Coding: Used in data compression algorithms for optimal prefix coding, where min-heaps build the optimal encoding tree.
Primary Operation Complexities
- Insert: O(log n)
- Extract (Extract-Max / Extract-Min): O(log n)
- Peek (Root view): O(1)
- Build-Heap (from an unsorted array): O(n) using the bottom-up construction approach.
Heap Implementation
A heap can be efficiently stored using a standard array where parent-child relationships are calculated via zero-based index formulas:
- Parent index:
parent(i) = ⌊(i - 1) / 2⌋ - Left child index:
left(i) = 2i + 1 - Right child index:
right(i) = 2i + 2
Advantages
- Efficiency: Logarithmic time complexity O(log n) for insertions and extractions makes heaps optimal for priority queue implementations.
- Simplicity: Array-based tree representation eliminates pointer storage overhead and keeps algorithms straightforward to implement.
Disadvantages
- Partial Ordering: A heap only guarantees strict order for the root element relative to its descendants, not a fully sorted dataset.
- Full Sorting Overhead: Obtaining a completely sorted sequence requires repeated extractions, taking O(n log n) time (e.g., via Heap Sort).
All rights remain with the original author (Archiveskk). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me