Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Heaps CS 308 – Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
data ordered along paths from root to leaf
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CSE373: Data Structures & Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Priority Queues Linked-list Insert Æ Æ head head
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Chapter 8 – Binary Search Tree
C++ Classes and Data Structures Jeffrey S. Childs
Tree Representation Heap.
Heaps A heap is a binary tree.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
C++ Plus Data Structures
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Ch. 12 Tables and Priority Queues
Priority Queues & Heaps
Chapter 9 Priority Queues and Sets
Heaps and Priority Queues
CSI 1340 Introduction to Computer Science II
CS 367 – Introduction to Data Structures
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Priority Queue and Heap
Presentation transcript:

Priority Queues, Heaps CS3240, L. grewe 1

2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the shape and order property of a heap and implement a heap in a nonlinked tree representation in an array Implement a priority queue as a heap Compare the implementations of a priority queue using a heap, a linked list, and a binary search tree

3 Priority Queue An ADT in which only the item with the highest priority can be accessed

4 Priority Queue Items on a priority queue are made up of pairs Can you think of how a priority queue might be implemented as An unsorted list? An array-based sorted list? A linked sorted list? A binary search tree?

5 Heaps Heap A complete binary tree, each of whose elements contains a value that is greater than or equal to the value of each of its children Remember? Complete tree

6 Heaps Heap with letters 'A'.. 'J'

7 Heaps Another heap with letters 'A'.. 'J'

8 Heaps C A T treePtr Are these heaps? treePtr

9 Heaps treePtr Is this a heap?

10 Heaps treePtr Can we use this fact to implement an efficient PQ? Where is the largest element?

11 Heaps We have immediate access to highest priority item BUT if we remove it, the structure isn't a heap Can we "fix" the problem efficiently?

12 Heaps Shape property is restored. Can order property be restored?

13 Heaps How?

14 Heaps

15 Heaps template struct HeapType { void reheapDown(int root, int bottom); … ItemType* elements; // Dynamic array int numElements; }

16 Heaps root Number nodes left to right by level

17 Heaps tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] elements Use number as index

18 Heaps ReapDown(heap, root, bottom) if heap.elements[root] is not a leaf Set maxChild to index of child with larger value if heap.elements[root] < heap.elements[maxChild] Swap(heap.elements[root], heap.elements[maxChild]) ReheapDown(heap, maxChild, bottom) What variables do we need? Where are a node's children?

19 Heaps // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS void HeapType ::ReheapDown(int root, int bottom) // Pre: ItemType overloads the relational operators // root is the index of the node that may violate the // heap order property // Post: Heap order property is restored between root and // bottom { int maxChild ; int rightChild ; int leftChild ; leftChild = root * ; rightChild = root * ;

20 Heaps if (leftChild <= bottom)// ReheapDown continued { if (leftChild == bottom) maxChild = leftChld; else { if (elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else maxChild = leftChild; } if (elements[root] < elements[maxChild]) { Swap(elements[root], elements[maxChild]); ReheapDown(maxChild, bottom); }

21 Heaps What other operations might our HeapType need?

22 Heaps Add K: Where must the new node be put? Now what?

23 Heaps

24 void HeapType::ReheapUp(int root, int bottom) // Pre: ItemType overloads the relational operators // bottom is the index of the node that may violate // the heap order property. The order property is // satisfied from root to next-to-last node. // Post:Heap order property is restored between root and // bottom { int parent; if (bottom > root) { parent = ( bottom - 1 ) / 2; if (elements[parent] < elements[bottom]) { Swap(elements[parent], elements[bottom]); ReheapUp(root, parent); }

25 Heaps How can heaps be used to implement Priority Queues?

26 Priority Queues class FullPQ(){}; class EmptyPQ(){}; template class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item); private: int length; HeapType items; int maxItems; };

27 Priority Queues template PQType ::PQType(int max) { maxItems = max; items.elements = new ItemType[max]; length = 0; } template void PQType ::MakeEmpty() { length = 0; } template PQType ::~PQType() { delete [] items.elements; }

28 Priority Queues Dequeue Set item to root element from queue Move last leaf element into root position Decrement length items.ReheapDown(0, length-1) Enqueue Increment length Put newItem in next available position items.ReheapUp(0, length-1)

29 Priority Queues template void PQType ::Dequeue(ItemType& item) { if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); }

30 Priority Queues template void PQType ::Enqueue(ItemType newItem) { if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); }

31 Comparison of Priority Queue Implementations EnqueueDequeue HeapO(log 2 N) Linked ListO(N) Binary Search Tree BalancedO(log 2 N) SkewedO(N)