We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24
Advanced Structures for Priority
Queues and Their Extensions
Queue • Like Stack, Queue is a linear structure • The order is First In First Out (FIFO). • Example : any queue of consumers for a resource where the consumer that came first is served first. • The difference between stacks and queues is in removing. • In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. Operations on Queue: Operations on Queue: • Mainly the following four basic operations are performed on queue: • Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition. • Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition. • Front: Get the front item from queue. • Rear: Get the last item from queue. Implementation: Linked List • In a Queue data structure, we maintain two pointers, front and rear. The front points the first item of queue and rear points to last item. • enQueue() This operation adds a new node after rear and moves rear to the next node. • deQueue() This operation removes the front node and moves front to the next node. • Time Complexity: Time complexity of both operations enqueue() and dequeue() is O(1) as we only change few pointers in both operations. There is no loop in any of the operations. Priority Queue • Priority Queue is an extension of queue with following properties. • Every item has a priority associated with it. • An element with high priority is dequeued before an element with low priority. • If two elements have the same priority, they are served according to their order in the queue. • A typical priority queue supports following operations. • insert(item, priority): Inserts an item with given priority. • getHighestPriority(): Returns the highest priority item. • deleteHighestPriority(): Removes the highest priority item. How to implement priority queue? 1. Using Array: • insert() operation can be implemented by adding an item at end of array in O(1) time. • getHighestPriority() operation can be implemented by linearly searching the highest priority item in array. This operation takes O(n) time. • deleteHighestPriority() operation can be implemented by first linearly searching an item, then removing the item by moving all subsequent items one position back. 2. Using Linked List • Using Linked List, time complexity of all operations remains same as array. • The advantage with linked list is deleteHighestPriority() can be more efficient as we don’t have to move items. 3. Using Heaps:
• Heap is generally preferred for priority queue implementation
• In a Binary Heap, getHighestPriority() can be implemented in O(1) time, • insert() can be implemented in O(Logn) time and • deleteHighestPriority() can also be implemented in O(Logn) time. Heap Data Structure
• A Heap is a special Tree-based data structure in which the tree is a
complete binary tree. Generally, Heaps can be of two types: • Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. • Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. Binary Heap
• A Binary Heap is a Binary Tree with following properties.
1. It’s a complete tree. This property of Binary Heap makes them suitable to be stored in an array. 2. A Binary Heap is either Min Heap or Max Heap. In a Min Binary Heap, the key at root must be minimum among all keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree. Max Binary Heap is similar to Min Heap. How is Binary Heap represented? • A Binary Heap is a Complete Binary Tree. • A Binary Heap is typically represented as an array. • The root element will be at Arr[0]. Heap Implementation Initialization
Swap Function Heap Implementation Queue Empty
Queue Full Heap Implementation Heap Sort Function Heap Implementation Insert Function Heap Implementation Remove Function Heap Implementation Heapify Function