Heaps and Priority Queues Study Notes
Priority Queue Abstract Data Type (ADT)
A priority queue serves as a collection that stores items.
Each item within the priority queue is a pair consisting of a key and a value: .
Main methods of the Priority Queue ADT include:
: This method inserts an item with key and value into the collection.
: This method removes and returns the specific item that has the smallest key within the collection.
Additional methods for management include:
: Returns the number of items stored.
: Checks if the priority queue contains any items.
Heaps and Their Properties
A heap is a specialized binary tree designed to store keys at its nodes.
A heap must satisfy two primary properties:
Heap-Order Property: For every internal node (excluding the root node), the key of node must be greater than or equal to the key of its parent: .
Complete Binary Tree Property: Let represent the height of the heap.
For every depth level , there must be exactly nodes present.
At the final depth level , all internal nodes must be positioned to the left of the external nodes.
The Last Node: In a heap structure, the "last node" is defined as the rightmost node at the maximum depth of the tree.
Height of a Heap
Theorem: A heap that stores keys has a height of .
This logarithmic relationship is demonstrated by the following examples:
Structure of the levels:
Depth : node.
Depth : nodes.
Depth : nodes.
The total number of keys scales with height such that height remains logarithmic relative to the number of nodes.
Implementation of Priority Queues using Heaps
Heaps are specialized structures with limited operations that effectively implement the Priority Queue ADT.
Every internal node in the heap stores a item.
The data structure keeps track of the position of the "last node" to facilitate efficient insertions and removals.
Types of Heaps:
Min-Heaps: The root contains the minimum key (e.g., items like , , , , ).
Max-Heaps: The root contains the maximum key.
Heap Insertion and the Upheap Algorithm
The insertion algorithm for adding a new key into a heap follows three distinct steps:
Find the insertion node : This is the location that will become the new last node of the heap.
Store the key: Place the new key at node .
Restore Heap-Order: Execute the Upheap algorithm to ensure every child key remains greater than or equal to its parent key.
Upheap Algorithm (also known as sift-up, bubble-up, percolate-up, or heapify-up):
The heap-order property may be violated immediately after insertion.
The algorithm restores the property by swapping the new key upward along the path toward the root.
Termination conditions: The key reaches the root, or it reaches a node whose parent has a key smaller than or equal to .
Efficiency: Because the height of the heap is , the upheap algorithm runs in time.
Max Heap Insertion Example
Consider a Max Heap where the rule is .
Initial set of keys includes , , (at level 1), and , , , (at level 2), with , , at level 3.
Process for inserting a new key :
Create a new node for .
Link the node as the last child (as the right child of the parent of nodes , , and ).
Sift-up Step 1: Swap and because .
Sift-up Step 2: Swap and because .
The value stops because it is less than the root .
Heap Removal and the Downheap Algorithm
The item with the smallest key is consistently located at the root of a min-heap.
Removals are restricted to the root node only.
The removal algorithm consists of three steps:
Replacement: Replace the root key with the key of the current last node .
Removal: Remove node from the heap (it is no longer the last node).
Restore Heap-Order: Execute the Downheap algorithm to ensure all child keys are greater than or equal to their parent keys.
Downheap Algorithm (also known as sift-down):
The heap-order property is often violated after the root is replaced by a larger key from the bottom of the tree.
The algorithm restores order by swapping the key downward from the root.
Termination conditions: Key reaches a leaf node, or it reaches a node whose children all possess keys greater than or equal to .
Efficiency: Similar to upheap, downheap runs in time due to the tree's height.
Downheap Visual Example
Consider a removal operation on a heap containing items like , , , , , , , and others.
When the root is removed, it is replaced by the last node (e.g., ).
The Downheap process moves down the tree by comparing it against its children and swapping it with the smaller of the two children to maintain min-heap property.
Array-Based Heap Implementation
A heap with keys can be represented efficiently using an array of length .
Links between nodes are not explicitly stored as pointers.
Mapping nodes to indices:
The root is at index .
For a node at index , its left child is at index and its right child is at index .
For a node at index , its parent is at index (using integer division).
In the provided diagram, the nodes map to array indices respectively.
Comprehensive Trace Example: Insertion and Removal
Task: Insert keys into an empty min-heap in order.
Step-by-Step Insertion:
Insert : Root is .
Insert : is the left child of .
Insert : is the right child of .
Insert : is the left child of .
Insert : is placed as the right child of . Upheap: is smaller than , so they swap. Now is at the level below the root, and is its child. Upheap: is smaller than , so they swap. is now the root.
Insert : is the left child of . Upheap: is smaller than , so swap and .
Insert : is the right child of (which is the new position of 's predecessor).
Insert : is the left child of . Upheap: is smaller than , so swap and .
Insert : is placed as the right child of . Upheap: swaps with , then with , then with to become the root.
Step-by-Step Removal (from end of insertion state):
Identify the root to remove (key ).
The last node is replaced into the root position. If current last node is , move to the root.
Downheap: Compare root with children and . Swap with the smallest child ().
Downheap: Compare at its new position with its children and . Swap with the smaller child ().
The process continues until the heap-order property is fully satisfied.