CSE310 Project 2: Priority Queues
Build a command-driven min-heap priority queue in C++
Project Objective
Build a command-driven min-heap priority queue in C++ with modular design and dynamic memory management.
Key Components
- Modular design: separate files for modularity
- Dynamic memory management: Efficient heap operations
- Min-heap implementation: Priority queue data structure
- File I/O: Command processing from input files
- Exact output formatting: Precise result formatting
Commands
Complete Binary Trees
Foundation for efficient heap implementation
Definition
A complete binary tree is a binary tree where:
- All levels are filled except possibly the last
- The last level is filled from left to right
- No "gaps" in the structure
Key Properties
- Height: floor(log₂(n)) where n = number of nodes
- Nodes at level i: at most 2ᵢ
- Total nodes: 2ʰ to 2ʰ⁺¹-1 for height h
- Array storage: No wasted space needed
Complete Binary Tree Example
Array Representation
Advantages for Heaps
- No pointers needed - use array indexing
- Cache-friendly sequential memory access
- Simple parent-child relationships via arithmetic
What is a Heap?
A complete binary tree with the heap property
Heap Definition
A heap is a complete binary tree that satisfies the heap property.
Heap Property
A relationship between parent and child nodes that creates a consistent ordering throughout the tree.
Why Complete Binary Trees?
- Ensures efficient array storage
- Predictable structure for algorithms
- Optimal height characteristics
Heap Structure
Array Representation
Array Index Formulas (1-based indexing)
floor(i/2)2*i2*i + 1Note: For 0-based indexing, use parent = floor((i-1)/2), left = 2*i+1, right = 2*i+2
Max-Heap vs Min-Heap
Two types of heaps with different ordering properties
Max-Heap
Property: Parent ≥ All Children
Use Case: When you need the maximum element quickly
Root: Contains the largest element
Min-Heap
Property: Parent ≤ All Children
Use Case: When you need the minimum element quickly (Project 2)
Root: Contains the smallest element
Key Differences
Heap Insertion Operation
Step-by-step bubble-up process
Algorithm Steps
Total for n insertions: O(n log n)
Try Your Own Values
Heap Deletion Operation
Remove the root element and maintain heap property
Heap Array
Extracted Elements
Algorithm Steps
Heapsort Algorithm
In-place sorting using heap operations
Two-Phase Algorithm
Convert unsorted array into max-heap structure
Complexity: O(n) using bottom-up heapify
Swap root with last element, reduce heap size, heapify
Complexity: O(n log n)
Space Complexity: O(1) - in-place sorting
Advantages: Guaranteed performance, no recursion stack
Heapify vs Insertion - Complexity Analysis
Mathematical proof of why build-heap is O(n)
Mathematical Analysis
Build-Heap Complexity Proof
Method Comparison
Repeated Insertion Method
Complexity: O(n log n)
Insert n elements, each taking O(log n) time
Bottom-up Heapify Method
Complexity: O(n)
Work decreases exponentially with height
Concept Recap: Why a Heap = a Priority Queue
Min-heap priority queue with task scheduling example
Priority Queue Definition
A priority queue is an abstract data type that stores elements with priorities (numeric keys).
Core Operations
- You can insert new elements with a given priority
- You can extract the element with the highest or lowest priority efficiently
Project Context
You're building a min-heap priority queue → smallest key = highest priority.
Each heap node contains: ELEMENT { index, key, pos } where key represents the priority.
Heap Operations Efficiency
Heaps make priority queues efficient:
Visual Example: A Min-Heap Priority Queue
Scenario: Scheduling tasks with different urgency levels (smaller number = higher priority)