Lecture_8_-_Priority_queue_and_Heap_-_single_slide

# Lecture_8_-_Priority_queue_and_Heap_-_single_slide -...

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

1 Priority Queue

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

View Full Document
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
3 Readings b Required [Carrano and Prichard] ch11

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

View Full Document
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.
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

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

View Full Document
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.
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?

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

View Full Document
8 Heap Heap is the most approriate data structure for realizing the ADT priority queue.
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

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

View Full Document
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
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

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

View Full Document
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)
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

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

View Full Document
14 Re-establish heap property 9 15 7 8 3 5 1 2 4
15 Re-establish heap property 15 9 7 8 3 5 1 2 4 bubble up

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

View Full Document
16 Remove the max item 12 8 9 4 3 5 7 2 1
17 Re-establish heap property 1 8 9 4 3 5 7 2

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

View Full Document
18 Re-establish heap property 9 8 7 4 3 5 1 2 bubble down
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

### 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
Ask a homework question - tutors are online