Lecture_8_-_Priority_queue_and_Heap_-_single_slide

Lecture_8_-_Priority_queue_and_Heap_-_single_slide -...

Info iconThis preview shows pages 1–19. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Priority Queue
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline b What is the ADT priority queue? b What are the operations supported? b heap (max-heap, min-heap) s heapInsert (O(log N)) s heapDelete (O(log N)) s heapRebuild (O(log N)) s heapify (O(N)) b heapSort (O(N log N)) b STL priority queue
Background image of page 2
3 Readings b Required [Carrano and Prichard] ch11
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 What is a Priority Queue? A Special form of queue from which items are removed according to their designated priority and not the order in which they entered.
Background image of page 4
5 Examples b A “to-do” list with priorities b Scheduling jobs in OS b Queue at A&E of the hospital b Go to Takashimaya (1) b Play tennis (5) b Prepare CS1102C lecture slides (6) b Go to department tea (3) b
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Priority queue operations b Create an empty priority queue b Insert an item with a given key b Remove the item with maximum key b Determine whether a priority queue is empty.
Background image of page 6
7 Unsorted list implementation b Insertion : add the element to end of a list O(1) b Deletion : traverse the list to find the element of maximum key and remove it O(n) What are the running times if we use sorted list?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Heap Heap is the most approriate data structure for realizing the ADT priority queue.
Background image of page 8
9 Definition b A (binary) heap is a complete binary tree satisfies the heap property : s for every node v , the search key in v is greater than or equal to those in the children of v. 7 1 3 2 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Negative examples 12 8 9 4 3 6 7 2 1 5 12 8 7 4 3 5 9 2 1 not complete fail heap property
Background image of page 10
11 Compare heap with BST b Both are binary trees. b Difference s Heap maintains heap property . b It is not a search tree s BST maintains BST property . b It is a search tree
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Representation using arrays 12 8 9 4 3 5 7 2 1 12 8 9 4 3 5 7 2 1 0 1 2 3 4 5 6 7 8 left(i) = 2*i+1 right(i) = 2*i+2 parent(i) = floor((i-1)/2)
Background image of page 12
13 Insert an item 9 8 7 4 3 5 1 2 15 9 8 7 4 3 5 1 2 0 1 2 3 4 5 6 7 8 15 9
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Re-establish heap property 9 15 7 8 3 5 1 2 4
Background image of page 14
15 Re-establish heap property 15 9 7 8 3 5 1 2 4 bubble up
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 Remove the max item 12 8 9 4 3 5 7 2 1
Background image of page 16
17 Re-establish heap property 1 8 9 4 3 5 7 2
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
18 Re-establish heap property 9 8 7 4 3 5 1 2 bubble down
Background image of page 18
Image of page 19
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

Lecture_8_-_Priority_queue_and_Heap_-_single_slide -...

This preview shows document pages 1 - 19. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online