Download - 1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.

Transcript

11

Chapter 8Chapter 8Priority QueuesPriority Queues

22

• Priority Queues•Implementations

• Heaps• Priority queues and heaps

•Vector based implementation of heaps

•Skew heaps

OutlineOutline

33

O(1) (worst case O(n)) O(n) O(n)

O(n) O(1) O(1)

O(1) or O(n) O(n) or O(1) O(n) or O(1)

O(n) O(n) O(n)

Implementations for Priority QueuesImplementations for Priority Queues

44

• 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®

What’s a Heap?What’s a Heap?

55

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 HeapAdvantages of a Heap

66

InsertionInsertion

77

InsertionInsertion

88

UpheapUpheap

99

RemovalRemoval

• 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 position2. Fix the heap by bubbling the root down

1010

Removal (cont)Removal (cont)

1111

DownheapDownheap

1212

Downheap (cont)Downheap (cont)

1313

Big-O for HeapsBig-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)

= O(1)= height of tree = O(logn)

= height of tree = O(logn)

1414

Vector/Array based HeapsVector/Array based HeapsIt 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:

1515

Vector based Heaps - InsertionVector based Heaps - Insertion

General Algorithm1. Get index of next open spot and add element to it2. While index isn’t the root

a) Calculate index of parentb) If parent is bigger than element swap and set index to parentc) Else break;

1616

Insertion (cont)Insertion (cont)

1717

Insertion (cont2)Insertion (cont2)

1818

Insertion (cont3)Insertion (cont3)

1919

Insertion (cont4)Insertion (cont4)

2020

Vector based Heaps - Vector based Heaps - DeletionDeletion

General Algorithm

1. Save value of 1st element

2. Move value of last element to 1st 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

2121

Deletion (cont)Deletion (cont)

2222

Deletion (cont2)Deletion (cont2)

2323

Deletion (cont3)Deletion (cont3)

2424

Deletion (cont4)Deletion (cont4)

2525

Deletion (cont5)Deletion (cont5)

2626

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 SortingPriority Queues (Heaps) and Sorting

2727

• 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???Questions???

2828

• 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)

BuildHeapBuildHeap