Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Priority Queues

2 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 – passenger, todo list What is a Priority Queue?

3 Priority Queue ADT Operations size() isEmpty() insertItem(k, e) – insert element e with key k minElement() – return ref to min element minKey() – return const ref to smallest key removeMin() – remove and return element with smallest key

4 Example insertItem(5, A) insertItem(9, C) insertItem(3, B) insertItem(7, D) minElement() minKey() removeMin() size() minElement() removeMin() isEmpty()

5 The Comparator Pattern Suppose we want a priority queue of “Appointment” objects How do we determine that one appointment is before another? Need to compare Appointment objects –operator overloading versus standard function

6 Implementation Using an array? Using a linked list? Running time of insertItem/removeMin?

7 Sorting Can you sort a list of numbers using a priority queue? Algorithm?

8 Heaps A heap is a priority queue implemented with a binary tree Heap-Order Property: In a heap T, for every node v other than the root, the key stored at v is greater than or equal to the key stored at v’s parent Complete Binary Tree Property: A heap

9 Heaps Heap-Order Property: In a heap T, for every node v other than the root, the key stored at v is greater than or equal to the key stored at v’s parent

10 Heaps Complete Binary Tree Property: A heap T with height h is a complete binary tree, that is, levels 0,1,2,…,h-1of T have the maximum number of nodes possible and all the internal nodes are to the left of the external nodes in level h-1. What does this give us?

11 Example Heap 7 9 4 5610 1115 root

12 Heap Implementation Insertion algorithm 7 9 4 5610 1115 root 5 new_node

13 Up-Heap Bubbling while new node is smaller than its parent swap parent and node Running time?

14 Heap Implementation Deletion algorithm 5 9 4 5610 117 root 15 delete 4

15 Down-Heap Bubbling if right child is null if left child is smaller than current swap left and current if both children not null swap current with smallest child

16 Vector Implementation Children are positions index*2 and index*2+1 Implementation of insert and remove? 0 1 2 3 4 5 6 7

17 Heap Sort Algorithm to use a heap to sort a list of numbers Running time?


Download ppt "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."

Similar presentations


Ads by Google