Heaps and priority queues are integral data structures in computer science, widely used for managing and organizing data efficiently. Understanding heaps, particularly binary heaps, and their application in priority queues is crucial for solving various computational problems. This article explores the core concepts of heaps, their types, operations, and their role in priority queues, along with practical examples and array representation.
Heaps: Structure and Properties
A heap is a specialized tree-based data structure that maintains a specific order among its nodes. The heap property ensures that if node X is a parent of node Y, then X and Y adhere to a predefined order, which is consistently applied throughout the tree.
Types of Heaps
- Binary Heap: The most commonly used heap is the binary heap, where each node has at most two children. This constraint simplifies the implementation and operation of the heap.
- Min Heap: In a min heap, the key at the root is the smallest value among all nodes. This property must hold true for all nodes recursively, meaning every parent node must be less than or equal to its children.
- Max Heap: In a max heap, the root key is the largest value. Similarly, every parent node must be greater than or equal to its children.
Properties of Binary Heaps
- Complete Binary Tree: A binary heap is always a complete binary tree, meaning all levels are fully filled except possibly for the last level, which is filled from left to right. This property ensures the binary heap has the smallest possible height, which is log2N\log_2 N for NN nodes. The completeness also allows binary heaps to be efficiently represented as arrays.
Priority Queue Using Binary Heap
A priority queue is an extension of a standard queue that supports elements with associated priorities. It ensures that elements with higher priorities are dequeued before those with lower priorities, and elements with the same priority are served according to their order of arrival.
Characteristics of a Priority Queue
- Priority-Based Ordering: Each item in a priority queue has an associated priority. An element with higher priority is dequeued before an element with lower priority.
- Order of Equal Priority Elements: If two elements have the same priority, they are served according to their order in the queue, typically based on their insertion order.
Operations on Binary Heap
Binary heaps support several key operations that are essential for priority queues:
- Insert (
insert(p)
): Adds a new element with priorityp
to the heap. This operation involves placing the new element at the end of the heap and then restoring the heap property by “shifting up” the element. - ExtractMax (
extractMax()
): Removes and returns the element with the highest priority (in a max heap) or the lowest priority (in a min heap). The root of the heap is replaced with the last element, and then the heap property is restored by “shifting down” the new root. - Remove (
remove(i)
): Removes an element at a specific position indicated by an iteratori
. This operation involves changing the element’s priority to a value that will ensure its removal, then performingextractMax
. - GetMax (
getMax()
): Retrieves the element with the highest priority (in a max heap) or the lowest priority (in a min heap) without removing it. This operation simply returns the value at the root of the heap. - ChangePriority (
changePriority(i, p)
): Updates the priority of an element at positioni
to a new priorityp
. Depending on whether the priority increases or decreases, the element may need to “shift up” or “shift down” to maintain the heap property.
Example of a Binary Max Heap
Consider a Binary Max Heap with the following structure:
45
/ \
31 32
/ \ / \
7 11 21 15
Inserting a New Node with Value 32
- Insert Node: Add the new node with value 32 to the leaf position, e.g., under the node with value 7.
markdown
45
/ \
31 32
/ \ / \
32 11 21 15
- Shift Up: The newly inserted node (32) violates the heap property. To restore it:
- Swap 32 with its parent (7).
- Continue swapping 32 with its new parent (31) if necessary.
Final Heap after Insert:
markdown45
/ \
32 32
/ \ / \
31 11 21 15
ExtractMax Operation
- Extract Max: Remove the root (45) and replace it with the last leaf (15).
markdown
15
/ \
32 32
/ \ / \
31 11 21
- Shift Down: Restore the heap property by swapping 15 with the larger of its children until the property is satisfied.
Final Heap after ExtractMax:
markdown32
/ \
31 32
/ \ /
15 11 21
ChangePriority Operation
- Change Priority: For example, increase the priority of node 11 to 35.
- Place 35 in the position of 11.
- Shift up until the heap property is restored.
Final Heap after ChangePriority:
markdown32
/ \
31 35
/ \ /
15 32 21
Remove Operation
- Remove Element: To remove an element, such as the one at index
i
, change its priority to a value higher than the current maximum, perform ashift up
, and thenextractMax
.
Array Representation of Binary Heap
Binary heaps can be efficiently represented as arrays due to their complete tree structure:
- Insertion: Add the new element to the end of the array (leftmost position at the last level).
- Extraction: Replace the root with the last element and adjust the heap.
Example Array Representation
For the Binary Max Heap shown earlier:
[
]- Parent of element at index
i
:floor((i - 1) / 2)
- Left child of element at index
i
:2 * i + 1
- Right child of element at index
i
:2 * i + 2
Conclusion
Heaps and priority queues are essential for managing dynamic sets of elements with associated priorities. Understanding binary heaps and their operations—insert, extract, remove, get max, and change priority—along with their array representation, equips developers with powerful tools for efficient data management and algorithm optimization. Mastery of these concepts is crucial for solving complex problems involving scheduling, task management, and priority-based operations.