We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15
PRIORITY QUEUES (HEAPS)
• Priority Queue: A priority queue is an abstract
data type that operates similarly to a regular queue but with an added feature: each element has a priority associated with it. Elements are served based on their priority rather than just their order in the queue. In essence, higher priority elements are processed before lower priority ones. • Key Features: • Dynamic: Can insert elements at any time. • Priority-Based: Elements are removed based on priority rather than FIFO (first-in, first-out) order. • Efficient Access: Allows for efficient retrieval of the highest (or lowest) priority element. • Implementation of Priority Queues • Common Implementations: • Heaps: A common way to implement a priority queue is through a heap data structure, particularly a binary heap. • Unordered Lists: Elements can be added to a list, but finding and removing the highest priority element can be inefficient. • Ordered Lists: Maintaining a sorted list allows for efficient removal of the highest priority but can be costly for insertions. • Binary Search Trees: Can also be used to maintain order and allow for efficient retrieval. • Heaps • A heap is a specialized tree-based data structure that satisfies the heap property. Heaps come in two main types: • Max Heap: The parent node is greater than or equal to its child nodes. This means the highest priority element is always at the root. • Min Heap: The parent node is less than or equal to its child nodes, making the lowest priority element accessible at the root. • Properties of Heaps: • Complete Binary Tree: Heaps are often implemented as complete binary trees, meaning all levels are fully filled except possibly the last one, which is filled from left to right. • Heap Order Property: In a max heap, the value of each node is greater than or equal to the values of its children. In a min heap, it is less than or equal. Basic Operations • Insertion: • Add the new element at the end of the heap (maintaining the complete tree property). • "Bubble up" (or sift up) the new element to restore the heap property. • Find Maximum/Minimum: • The maximum (in a max heap) or minimum (in a min heap) element can be accessed directly from the root of the heap. • Deletion: • Remove the root element. • Replace the root with the last element in the heap. • "Bubble down" (or sift down) the new root to restore the heap property. • Heapify: • Convert an unordered array into a heap by repeatedly applying the sift-down operation. Complexity
• Insertion: O(logn) • Deletion: O(logn) • Accessing Max/Min: O(1) • Heapify: O(n) for building a heap from an unordered array. Applications of Priority Queues
• Task Scheduling: Operating systems use priority
queues for scheduling processes based on priority. • Graph Algorithms: Algorithms like Dijkstra's and Prim's use priority queues to manage the exploration of nodes based on cost or distance. • Event Simulation: Simulating events in order of their occurrence times. • Data Compression: Huffman coding uses priority queues to build trees based on frequency of characters. Structure Property of Heaps • The structure property of heaps is fundamental to their design and functionality. Let's break it down: 1. Complete Binary Tree • Definition: A heap is always represented as a complete binary tree. This means: – Every level of the tree is fully filled, except possibly for the last level, which is filled from left to right. • Implications: – This structure allows heaps to be efficiently stored in an array format, without needing pointers for child nodes. The relationships between parent and child nodes can be easily calculated using simple index arithmetic. 2. Array Representation • In a complete binary tree, the structure property allows for a straightforward array representation: • Indexing: – If a node is located at index i:
– This array representation makes it easy to traverse and
manipulate the heap without needing additional memory for pointers. 3. Heap Order Property • The heap also satisfies a specific order property based on whether it’s a max heap or a min heap: Max Heap: • Definition: In a max heap, for any given node n, the value of n is greater than or equal to the values of its children. – If n is a parent node, then n ≥ left child and n ≥ right child Min Heap:
• Definition: In a min heap, for any given node
n, the value of n is less than or equal to the values of its children. • If n is a parent node, then n ≤ left child and n ≤ right child Summary of Structure Properties
• Complete Binary Tree: Ensures efficient use of
space and supports array representation. • Array Representation: Simplifies access to parent and child nodes using index calculations. • Heap Order Property: Maintains the max or min condition, ensuring efficient retrieval of the highest or lowest priority element.