of 28 /28
1 Chapter 8 Chapter 8 Priority Queues Priority Queues
• date post

22-Dec-2015
• Category

## Documents

• view

228

1

TAGS:

Embed Size (px)

### Transcript of 1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based...

• Slide 1
• 1 Chapter 8 Priority Queues
• Slide 2
• 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline
• Slide 3
• 3 O(1) (worst case O(n)) O(n) O(1) O(1) or O(n) O(n) or O(1) O(n) Implementations for Priority Queues
• Slide 4
• 4 A heap is a binary tree whose root contains the minimum value in the tree and whose subtrees are heaps. Alternately, A heap is a binary tree whose values are in ascending order on every path from the root to the leaf. Examples of heaps: Generally speaking, we like to keep our heaps complete Whats a Heap?
• Slide 5
• 5 Because of the loosened requirements on ordering, heaps are easier to maintain in a consistent state, and therefore more efficient than other data structures for implementing priority queues. Advantages of a Heap
• Slide 6
• 6 Insertion
• Slide 7
• 7 Insertion
• Slide 8
• 8 Upheap
• Slide 9
• 9 Removal The root is the smallest element Therefore, getFirst and remove always access the root Heap removal: 1.Replace the root with the element in the last filled position 2.Fix the heap by bubbling the root down
• Slide 10
• 10 Removal (cont)
• Slide 11
• 11 Downheap
• Slide 12
• 12 Downheap (cont)
• Slide 13
• 13 Big-O for Heaps Insertion add element to next position bubble element up Deletion remove root and swap in most recently added element percolate element down = O(1) = height of tree = O(logn)
• Slide 14
• 14 Vector/Array based Heaps It is possible to efficiently store a complete binary tree in an array The following provides a mapping between array elements and the nodes of the tree: The element at index 0 is the root The left child of a node at index i is stored at index 2i+1 The right child is stored at 2i+2 The parent of a node at index i is at index floor((i-1)/2) For example:
• Slide 15
• 15 Vector based Heaps - Insertion General Algorithm 1. Get index of next open spot and add element to it 2. While index isnt the root a) Calculate index of parent b) If parent is bigger than element swap and set index to parent c) Else break;
• Slide 16
• 16 Insertion (cont)
• Slide 17
• 17 Insertion (cont2)
• Slide 18
• 18 Insertion (cont3)
• Slide 19
• 19 Insertion (cont4)
• Slide 20
• 20 Vector based Heaps - Deletion General Algorithm 1. Save value of 1 st element 2. Move value of last element to 1 st position, set idx to index of root 3. While a child of idx is smaller than idx a) swap value stored at idx with smallest child b) set idx to index of smallest child
• Slide 21
• 21 Deletion (cont)
• Slide 22
• 22 Deletion (cont2)
• Slide 23
• 23 Deletion (cont3)
• Slide 24
• 24 Deletion (cont4)
• Slide 25
• 25 Deletion (cont5)
• Slide 26
• 26 We can use a priority queue to sort stuff: 1. Insert the elements one at a time into the priority queue. 2. Remove the elements 1 at a time from the priority queue and place them into an array. 3. Runtime? Priority Queues (Heaps) and Sorting
• Slide 27
• 27 Does every subtree of a heap satisfy the properties of a heap? Is every heap an array? Is every array a heap? What is the height of a heap? What are the maximum and minimum numbers of elements in a heap of height h? Is an array that is sorted in ascending order a heap? Write an algorithm that will turn an array into a heap. Complexity? Write an equals method for a vector based heap. Complexity?Questions???
• Slide 28
• 28 Given an array A, how can we turn it into a heap? Simple: Elements A[ n/2 +1] through A[n] are leaves, so they can be considered one-element heaps. Call Heapify in a bottom-up manner starting at index n/2 . Running time : easy analysis: O(nlgn) better analysis (proof omitted): O(n) BuildHeap