CS Data Structure: Heaps.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

What else can we do with heaps? Use the heap for sorting. Heapsort!
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
RemoveMin We must take the value from the root node and return it to the user. Then we must remove the node. Easy array implementation: –Take the last.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps, Heapsort, and Priority Queues
Priority Queues and Heaps
October 30th – Priority QUeues
Hashing Exercises.
Source: Muangsin / Weiss
Heap Sort Example Qamar Abbas.
Heaps.
Priority Queues (Heaps)
Introduction to Algorithms
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
ADT Heap data structure
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Priority Queues.
Heaps, Heapsort, and Priority Queues
i206: Lecture 14: Heaps, Graphs intro.
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
Priority Queues.
CSE332: Data Abstractions Lecture 4: Priority Queues
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
Priority Queues & Heaps
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
CO4301 – Advanced Games Development Week 4 Binary Search Trees
B-Trees.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Details At this point our heap can easily implement a priority queue given that we start with an empty queue and add and remove elements as necessary.
Heaps & Multi-way Search Trees
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Presentation transcript:

CS Data Structure: Heaps

Objectives Heaps Priority Queues Heap sort

Priority Queues In a number of applications a data structure that can produce the smallest item in a collection is useful. Obviously any sorted list can do the job. However, we may wish to have a data structure with the ability to add items at any time and remove the smallest item at any time. In the interest of efficiency a novel data structure can be utilized to produce good results. But first, let's be clear on exactly what is our target application.

007 Or take, for example, a queue of Qs: (That’s Q from the James Bond movies)

008 Perhaps we need to find the smallest in this queue of Qs.

Alternatives We might search for the smallest one: Or we might keep them in order:

Priority Queues A better example is a queue of Perhaps we wish to find the smallest document, and print it first. A better example is a queue of documents waiting to be printed. This is known as the Shortest Job First scheduling algorithm

Priority Queues Take for example a printer queue. 90 mins. 20 mins. The small jobs take less time to print. So it’s a better policy to print them first Take for example a printer queue. 90 mins. 20 mins. 5 secs. 5 mins. Simply printing ‘in order’ can cause bottle necks – maybe we need to pump up throughput!

Sound Unlikely? Finding or removing the smallest element is fast So, we need a data structure that has the following property: Finding or removing the smallest element is fast Now, exactly how might we implement this?

Data Structure: Heap Vocabulary Terms: binary heap, Queuing Use: used to implement a priority queue. Sorting Use: Heapsort uses a heap. Without qualification, the term “heap” refers to binary heap. So what is a heap? A heap is binary tree The binary tree is “completely filled” there are no gaps, except for the bottom level. A picture might help. . .

Completeness Incomplete Complete "Complete" is a stronger B B D E F G D E F G H I J H I J "Complete" is a stronger property than a BST since it does not permit gaps in any rows This missing node makes the tree "incomplete".

and Instead of the typical binary search tree relationship between node values (smaller to the left, etc.)... A heap has one simple rule: In a heap, for every node X with parent P, the key value in P is smaller than or equal to X Which forces the minimum value to be at the tree root node P X Y P<X P<Y X?Y

Basic Heap Properties Structure Property Order Property organize the data as a complete binary tree enables operation in log N time Order Property In a heap, for every node X with parent P, the key value in P is smaller than or equal to X Forces the minimum value to be the tree’s root Tree Operations may disturb the structure or the order or both properties must restore those properties

Heap: Array Implementation Heaps are essentially binary trees Their requirements and properties allow us to use a very clever implementation: We implement a binary tree using an Array!!! Consider a typical array: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

Heap: Array Implementation Now think of it this way: 00 Ignore this one 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

Heap: Array Implementation Now think of it this way: 00 Ignore this one 01 The lines are not real! I have drawn them in to help show "tree-like" nature of this arrangement! 02 03 04 05 06 07 08 09 10 11 12 13 14 15

Heap: Array Implementation Interesting math facts regarding indices: Note: The parent of any node i is given by i/2 (integer division) 01 Note: The right child of any node i is given by 2i+1 02 03 Note: The left child of any node i is given by 2i 04 05 06 07 08 09 10 11 12 13 14 15

Why not use arrays for all trees? Advantages of conventional dynamic tree implementation Can always add one more node easily Can move whole chunks of tree by simply manipulating pointers. Important for balanced tree algorithms Doesn't lose efficiency if tree not complete Typical tree usage does not call for “complete” trees! Advantages of array implementation of complete tree Space efficient: A node only holds data, no pointers Quick access to "next" available spot if size is maintained. O(1). Can find a node’s parent without lots of extra pointers and manipulation

“Dynamic” behavior of arrays achieved by reallocating Array Implementation For any element at array position i: left child is at ( 2i ) right child is at ( 2i+1 ) parent is at (int) (i / 2) 1 A 3 2 C B 4 5 6 7 D E F G “Dynamic” behavior of arrays achieved by reallocating larger array blocks 8 9 10 H I J A B D E F G H I J C 1 2 3 4 5 6 7 8 9 10 11 12 13 Can just skip this one

Priority Queue: Basic Behavior Priority Queue’s basic behavior: insert(item) "item" should be a data that does have an order property (like integers, strings (alphabetic ordering), etc) findMin() returns the minimum value without changing the priority queue deleteMin() delete and return the smallest item in the structure isEmpty() makeEmpty()

Priority Queue: Implementation An excellent choice is a heap. The heap was originally developed as part of a sorting algorithm called Heapsort Later, we will show how a heap can be used to perform the Heapsort

Insertion size = 26 A correct heap to start with... 2 10 26 18 21 29 27 57 20 47 37 69 69 29 58 A correct heap to start with... 95 85 90 31 94 55 98 71 79 91 86

the size, and place the 25 in that slot Insertion size = 27 2 Increment the size, and place the 25 in that slot 10 26 18 21 29 27 57 20 47 37 69 69 29 58 95 85 90 31 94 55 98 71 79 91 86 25 Insert 25

Insertion size = 27 Insert 25 2 10 26 18 21 29 27 57 20 47 37 69 69 29 58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 25 Insert 25

Insertion size = 27 Insert 25 2 10 26 18 21 29 27 57 20 47 37 69 25 29 58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 69 Insert 25

Insertion size = 27 Insert 25 2 10 26 18 21 25 27 57 20 47 37 69 29 29 58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 69 Insert 25

Insertion size = 27 Insert 25 2 10 25 18 21 26 27 57 20 47 37 69 29 29 58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 69 Insert 25

Insertion size = 27 2 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69

Will insert work starting with an empty heap? Question? Will insert work starting with an empty heap?

removeMin We must take the value from the root node and return it to the user. Then we must remove the node. Easy array implementation: Take the last element in the heap and put it in root Then call heapify!

heapify Heapify is called on a node. It checks that the heap property is being maintained. If not, it fixes it. 42 27 36 i 2i 2i+1 27 42 36 i 2i 2i+1 If a fix was made, then heapify calls itself recursively on the value that was swapped down i 27 Details such as checking to make sure elements 2i and 2i+1 are in the heap are omitted 2i 2i+1 42 36

removeMin size = 27 2 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69

removeMin save returnValue: 2 size = 27 2 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69

removeMin returnValue: 2 size = 27 69 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69

removeMin returnValue: 2 size = 26 69 Decrement the size 10 25 18 21 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69

removeMin returnValue: 2 size = 26 69 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86

removeMin returnValue: 2 size = 26 Heapify 69 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 69 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 69 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 18 25 69 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 18 25 69 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 18 25 20 21 26 27 57 69 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 18 25 20 21 26 27 57 69 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 Heapify 10 18 25 20 21 26 27 57 31 47 37 69 29 29 58 95 85 90 69 94 55 98 71 79 91 86 Heapify

removeMin returnValue: 2 size = 26 10 18 25 20 21 26 27 57 31 47 37 69 29 29 58 95 85 90 69 94 55 98 71 79 91 86

removeMin size = 26 10 18 25 20 21 26 27 57 31 47 37 69 29 29 58 95 85 90 69 94 55 98 71 79 91 86

Details isEmpty makeEmpty Simply tests size makeEmpty Can just set size to zero. At this point our heap can easily implement a priority queue given that we start with an empty queue and add and remove elements as necessary. If we were given an array of numbers it might be useful to turn the array into a heap "in-place"

BuildHeap Given an array of numbers how do we convert it into a heap. We could iterate through the array and insert each number into heap but this requires us to have space big enough for twice the quantity of numbers we have. We want to convert in place. Note: We will assume that element 0 is not used. Don't confuse this with heapify. BuildHeap will use heapify and is used to create the Heap

BuildHeap Assume that we have the number of elements in a variable called size Iterate index from element arraySize/2 down to index 1 heapify(index)

BuildHeap size = 26 87 43 68 6 77 33 9 11 19 99 6 23 89 2 14 1 5 27 35 7 42 12 71 3 67 22

BuildHeap size = 26 Start at element arraySize/2 87 43 68 6 77 33 9 11 13 11 19 99 6 23 89 2 14 Start at element arraySize/2 1 5 27 35 7 42 12 71 3 67 22

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 23 89 2 14 1 13 11 19 99 6 23 89 2 14 1 5 27 35 7 42 12 71 3 67 22 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 23 22 2 14 1 13 11 19 99 6 23 22 2 14 1 5 27 35 7 42 12 71 3 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 23 22 2 14 1 13 11 19 99 6 23 22 2 14 1 5 27 35 7 42 12 71 3 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 3 22 2 14 1 13 11 19 99 6 3 22 2 14 1 5 27 35 7 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 3 22 2 14 1 13 11 19 99 6 3 22 2 14 1 5 27 35 7 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 3 22 2 14 1 13 11 19 99 6 3 22 2 14 1 5 27 35 7 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 7 6 3 22 2 14 1 5 13 11 19 7 6 3 22 2 14 1 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 7 6 3 22 2 14 1 5 13 11 19 7 6 3 22 2 14 1 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 7 6 3 22 2 14 1 5 13 11 19 7 6 3 22 2 14 1 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 1 19 7 6 3 22 2 14 11 5 13 1 19 7 6 3 22 2 14 11 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 9 1 19 7 6 3 22 2 14 11 5 13 1 19 7 6 3 22 2 14 11 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 2 1 19 7 6 3 22 9 14 11 5 13 1 19 7 6 3 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 33 2 1 19 7 6 3 22 9 14 11 5 13 1 19 7 6 3 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 33 22 9 14 11 5 13 1 19 7 6 33 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 33 22 9 14 11 5 13 1 19 7 6 33 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 23 22 9 14 11 5 13 1 19 7 6 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 23 22 9 14 11 5 13 1 19 7 6 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 77 23 22 9 14 11 5 13 1 19 7 77 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 77 23 22 9 14 11 5 13 1 19 7 77 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 12 23 22 9 14 11 5 13 1 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 12 23 22 9 14 11 5 13 1 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 1 6 3 2 6 19 7 12 23 22 9 14 11 5 13 6 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 1 6 3 2 6 19 7 12 23 22 9 14 11 5 13 6 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 1 6 3 2 5 19 7 12 23 22 9 14 11 6 13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 68 1 6 3 2 5 19 7 12 23 22 9 14 11 6 13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 2 1 6 3 68 5 19 7 12 23 22 9 14 11 6 13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 2 1 6 3 68 5 19 7 12 23 22 9 14 11 6 13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 2 1 6 3 9 5 19 7 12 23 22 68 14 11 6 13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 43 2 1 6 3 9 5 19 7 12 23 22 68 14 11 6 13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 1 2 43 6 3 9 5 19 7 12 23 22 68 14 11 6 13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 1 2 43 6 3 9 5 19 7 12 23 22 68 14 11 6 13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 1 2 5 6 3 9 43 19 7 12 23 22 68 14 11 6 13 43 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 1 2 5 6 3 9 43 19 7 12 23 22 68 14 11 6 13 43 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 1 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43 13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 87 1 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43 13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 87 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43 13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 87 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43 13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 5 2 87 6 3 9 6 19 7 12 23 22 68 14 11 43 13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 5 2 87 6 3 9 6 19 7 12 23 22 68 14 11 43 13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 5 2 6 6 3 9 87 19 7 12 23 22 68 14 11 43 13 87 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 5 2 5 6 3 9 87 19 7 12 23 22 68 14 11 43 13 87 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 heapify 1 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87 43 13 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89 heapify

BuildHeap size = 26 1 5 2 5 6 3 9 13 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89

Use the heap for sorting. Heapsort! What else can we do? Use the heap for sorting. Heapsort!

Basic Idea Given an unsorted array we use BuildHeap to convert it into a heap While heap is not empty: removeMin - remember that replaces the top element with the last element of the “heap/array” and then heapifies The heap (logical concept) is one smaller but the array (physical concept) hasn't changed Put the item just removed in the element just after the logical “end” of the heap At conclusion the array is sorted

HeapSort after BuildHeap size = 26 1 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89

HeapSort size = 26 removeMin 1 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89

HeapSort size = 26 removeMin 89 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89

HeapSort size = 25 removeMin = 1 Not in heap now 2 5 3 5 6 22 9 11 19 7 12 23 89 68 14 Not in heap now 87 43 27 35 99 42 77 71 33 67

HeapSort size = 25 2 5 3 5 6 22 9 11 19 7 12 23 89 68 14 87 43 27 35 99 42 77 71 33 67 1 heap is 1 element smaller, smallest element is at end of array

HeapSort size = 25 Now do it again! 2 5 3 5 6 22 9 11 19 7 12 23 89 68 14 87 43 27 35 99 42 77 71 33 67 1 Now do it again!

HeapSort size = 24 3 5 9 5 6 22 14 11 19 7 12 23 89 68 67 87 43 27 35 99 42 77 71 33 2 1

HeapSort size = 23 5 5 9 11 6 22 14 33 19 7 12 23 89 68 67 87 43 27 35 99 42 77 71 3 2 1

HeapSort size = 22 5 6 9 11 7 22 14 33 19 42 12 23 89 68 67 87 43 27 35 99 71 77 5 3 2 1

HeapSort size = 21 6 7 9 11 7 22 14 33 19 42 12 23 89 68 67 87 43 27 35 99 71 5 5 3 2 1

HeapSort size = 20 7 7 9 11 12 22 14 33 19 42 71 23 89 68 67 87 43 27 35 99 6 5 5 3 2 1

HeapSort size = 19 7 11 9 19 12 22 14 33 27 42 71 23 89 68 67 87 43 99 35 7 6 5 5 3 2 1

HeapSort size = 18 9 11 14 19 12 22 35 33 27 42 71 23 89 68 67 87 43 99 7 7 6 5 5 3 2 1

HeapSort size = 17 11 12 14 19 42 22 35 33 27 99 71 23 89 68 67 87 43 9 7 7 6 5 5 3 2 1

HeapSort size = 16 12 19 14 27 42 22 35 33 43 99 71 23 89 68 67 87 11 9 7 7 6 5 5 3 2 1

HeapSort size = 15 14 19 22 27 42 23 35 33 43 99 71 87 89 68 67 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 14 19 27 22 33 42 23 35 67 43 99 71 87 89 68 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 13 22 27 23 33 42 68 35 67 43 99 71 87 89 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 12 23 27 35 33 42 68 89 67 43 99 71 87 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 11 27 33 35 43 42 68 89 67 87 99 71 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 10 33 42 35 43 71 68 89 67 87 99 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 9 35 42 68 43 71 99 89 67 87 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 8 42 43 68 67 71 99 89 87 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 7 43 67 68 87 71 99 89 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 6 67 71 68 87 89 99 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 5 68 71 99 87 89 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 4 71 87 99 89 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 3 87 89 99 71 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 2 89 99 87 71 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

HeapSort size = 1 99 89 87 71 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1

Summary – you should now … Heaps Priority Queues Heapsort