Binary Heaps and Heapsort

Similar documents
Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Converting a Number from Decimal to Binary

Binary Heap Algorithms

Ordered Lists and Binary Trees

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

From Last Time: Remove (Delete) Operation

Data Structures and Algorithms

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees CMPSC 122

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

6 March Array Implementation of Binary Trees

Cpt S 223. School of EECS, WSU

Algorithms and Data Structures

Algorithms and Data Structures

CS711008Z Algorithm Design and Analysis

1/1 7/4 2/2 12/7 10/30 12/25

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

PES Institute of Technology-BSC QUESTION BANK

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Data Structure [Question Bank]

Binary Heaps. CSE 373 Data Structures

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

CSE 326: Data Structures B-Trees and B+ Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Data Structure with C

Lecture Notes on Binary Search Trees

Data Structures. Level 6 C Module Descriptor

Exam study sheet for CS2711. List of topics

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Sample Questions Csci 1112 A. Bellaachia

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Data Structures and Data Manipulation

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Introduction to Data Structures and Algorithms

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

10CS35: Data Structures Using C

Full and Complete Binary Trees

Chapter 14 The Binary Search Tree

Big Data and Scripting. Part 4: Memory Hierarchies

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Lecture Notes on Binary Search Trees

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Binary Search Trees (BST)

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Why Use Binary Trees?

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

International Journal of Software and Web Sciences (IJSWS)

recursion, O(n), linked lists 6/14

Alex. Adam Agnes Allen Arthur

Binary Trees and Huffman Encoding Binary Search Trees

TREE BASIC TERMINOLOGIES

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Data Structures Using C++

Lecture 2 February 12, 2003

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Common Data Structures

DATA STRUCTURES USING C

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Algorithms Chapter 12 Binary Search Trees

Analysis of a Search Algorithm

Analysis of Algorithms I: Optimal Binary Search Trees

Binary search algorithm

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Lecture 12 Doubly Linked Lists (with Recursion)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures Fibonacci Heaps, Amortized Analysis

Quick Sort. Implementation

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

The ADT Binary Search Tree

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Algorithms. Margaret M. Fleck. 18 October 2010

Data Structures and Algorithms(5)

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

How To Create A Tree From A Tree In Runtime (For A Tree)

Persistent Binary Search Trees

CompSci-61B, Data Structures Final Exam

Algorithms and Data Structures Written Exam Proposed SOLUTION

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

Data Structures and Algorithms Written Examination

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Stacks. Linear data structures

Transcription:

CPSC 259: Data Structures and Algorithms for Electrical Engineers Binary Heaps and Heapsort Textbook Reference: Thareja first edition: Chapter 12, pages 501-506 Thareja second edition: Chapter 12, pages 361-365 Instructor: Dr. Hassan Khosravi September-December 2014 CPSC 259 Binary Heaps and Heapsort Page 1

Learning Goals! Provide examples of appropriate applications for priority queues and heaps.! Implement and manipulate a heap using an array as the underlying data structure.! Describe and use the Heapify ( Build Heap ) and Heapsort algorithms.! Analyze the complexity of operations on a heap. CPSC 259 Binary Heaps and Heapsort Page 2

CPSC 259: A journey filled with data structures, complexity, and algorithms! Simple data structure (arrays)! Pointers, struct, and dynamic memory allocation.! Computing the complexity of our algorithms! Linked lists, Stack and Queue! Recursion and applications! Binary trees and Binary search trees! Binary heaps and heapsorts CPSC 259 Binary Heaps and Heapsort Page 3

Priority Queues! Let s say we have the following tasks. 2 - Water plants 5 - Order cleaning supplies 1 - Clean coffee maker 3 - Empty trash 9 - Fix overflowing sink 2 - Shampoo carpets 4 - Replace light bulb 1 - Remove pencil sharpener shavings We are interested in finding the task with the highest priority quickly. CPSC 259 Binary Heaps and Heapsort Page 4

Priority Queues! A collection organized so as to permit fast access to and removal of the largest/smallest element! Prioritization is a weaker condition than ordering! Order of insertion irrelevant! Element with the highest priority (whatever that means) comes out next! Not really a queue: not a FIFO! Basic operations:! push() Adds an element to the collection! top() Looks at the element with the highest priority! pop() Removes this element CPSC 259 Binary Heaps and Heapsort Page 5

Heaps and Priority Queues! A priority queue is an abstract data type (ADT) that maintains a bag of items.! What is the difference between a set and a bag?! A set does not contain duplicates.! Two or more distinct items in a priority queue can have the same priority. If all items have the same priority, you might think a priority queue should behave like a queue, but it may not. We ll see why, shortly.! A binary heap can implement a priority queue, efficiently. CPSC 259 Binary Heaps and Heapsort Page 6

The heap property! A node has the heap property if the value in the node is as large as or larger than the values in its children 12 12 12 8 3 Blue node has heap property 8 12 Blue node has heap property 8 14 Blue node does not have heap property! All leaf nodes automatically have the heap property! A binary tree is a heap if all nodes in it have the heap property CPSC 259 Binary Heaps and Heapsort Page 7

SiftUp! Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child 12 8 14 Blue node does not have heap property 14 8 12 Blue node has heap property! This is sometimes called sifting up! Notice that the child may have lost the heap property CPSC 259 Binary Heaps and Heapsort Page 8

A binary heap can implement a priority queue, efficiently. A minimum binary heap is a nearly complete binary tree, such that the data in every node is less than or equal to the data in each of its children (i.e., left child and right child), and such that the node s left and right sub-trees are also minimum heaps. (There s also a maximum binary heap, where parent each child simply changes to parent each child.) 2 5 7 7 5 7 5 7 8 4 3 4 Min heap Max heap CPSC 259 Binary Heaps and Heapsort Page 9

It is important to realize that two binary heaps can contain the same data but the data may appear in different positions in the heap: 2 5 7 5 7 8 2 5 5 7 7 8 Both of the minimum binary heaps above contain the same data: 2, 5, 5, 7, 7, and 8. Even though both heaps satisfy all the properties necessary of a minimum binary heap, the data is stored in different positions in the tree. CPSC 259 Binary Heaps and Heapsort Page 10

Constructing a heap I! A tree consisting of a single node is automatically a heap! We construct a heap by adding nodes one at a time:! Add the node just to the right of the rightmost node in the deepest level! If the deepest level is full, start a new level! Examples: Add a new node here Add a new node here CPSC 259 Binary Heaps and Heapsort Page 11

Constructing a heap II! Each time we add a node, we may destroy the heap property of its parent node. To fix this, we sift up! But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent node! We repeat the sifting up process, moving up in the tree, until either! We reach nodes whose values don t need to be swapped (because the parent is still larger than both children), or! We reach the root! Maintaining the heap property after a new node has been added is called the reheapup process CPSC 259 Binary Heaps and Heapsort Page 12

Constructing a heap III 8 8 10 10 10 8 8 5 1 2 3 10 10 12 8 5 12 5 10 5 12 8 8 4 CPSC 259 Binary Heaps and Heapsort Page 13

Other children are not affected 12 12 14 10 5 14 5 12 5 8 14 8 10 8 10! The node containing 8 is not affected because its parent gets larger, not smaller! The node containing 5 is not affected because its parent gets larger, not smaller! The node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally CPSC 259 Binary Heaps and Heapsort Page 14

A sample heap! Here s a sample binary tree after it has been heapified 25 22 17 19 22 14 15 18 14 21 3 9 11! Notice that heapified does not mean sorted! Heapifying does not change the shape of the binary tree; this binary tree is still a nearly complete binary tree. CPSC 259 Binary Heaps and Heapsort Page 15

Clicker Question! Is the following binary tree a maximum binary heap? 25 22 17 19 23 16 15 14 18 21 5 9 6! A: It is a maximum binary heap! B: It is not a maximum binary heap! C: I don t know CPSC 259 Binary Heaps and Heapsort Page 16

Clicker Question (answer)! Is the following binary tree a maximum binary heap? 25 22 17 19 23 16 15 14 18 21 5 9 6! A: It is a maximum binary heap! B: It is not a maximum binary heap! C: I don t know CPSC 259 Binary Heaps and Heapsort Page 17

Clicker question! Which of the following statements are true of the tree shown below?! a. It is a binary search tree.! b. It is a complete tree.! c. It is a full tree.! d. It is a max heap.! e. More than one of the above statements is true. CPSC 259 Binary Heaps and Heapsort Page 18

Clicker question (answer)! Which of the following statements are true of the tree shown below?! a. It is a binary search tree.! b. It is a complete tree.! c. It is a full tree.! d. It is a max heap.! e. More than one of the above statements is true. CPSC 259 Binary Heaps and Heapsort Page 19

Clicker question! Which of the following statements are true of the tree shown below?! A: It is a binary search tree.! B: It is a nearly complete binary tree.! C: It is a complete tree.! D: It is a max heap.! E: (B) & (D) CPSC 259 Binary Heaps and Heapsort Page 20

Clicker question (Answer)! Which of the following statements are true of the tree shown below?! A: It is a binary search tree.! B: It is a nearly complete binary tree.! C: It is a complete tree.! D: It is a max heap.! E: (B) & (D) CPSC 259 Binary Heaps and Heapsort Page 21

CPSC 259 Admin notes! No Labs next week! Office hours to help you with take-home! Marika: Monday Nov 10 1:30-2:30! Giorgio: Thursday Nov 13 1:00 2:00! Pearl: Friday Nov 14 9:00 10:00! Lauren: Friday Nov 14 3:00 - :4:00! Lab4 in-lab and lab4 quiz grades are now available on Connect! Multiple choice quiz on heaps available on Connect CPSC 259 Binary Heaps and Heapsort Page 22

So, where were we! A priority queue is an abstract data type (ADT) that maintains a bag of items! push() Adds an element to the collection! top() Looks at the element with the highest priority! pop() Removes this element! A node has the heap property if the value in the node has higher priority than the values in its children! A minimum binary heap is a nearly complete binary tree, such that the data all nodes hold the heap property. CPSC 259 Binary Heaps and Heapsort Page 23

In-class exercise! Build a binary Max-heap using the following numbers, assuming numbers arrive one at a time, so you don t have the whole list to start with).! 2,7,26,25,19,17,1,90,3,36 90 36 17 25 26 7 1 2 3 19 CPSC 259 Binary Heaps and Heapsort Page 24

Removing the root! So we know how to add new elements to our heap.! We also know that root is the element with the highest priority.! But what should we do once root is removed?! Which element should replace root? CPSC 259 Binary Heaps and Heapsort Page 25

Removing the root! Notice that the largest number is now in the root! Suppose we discard the root: 11 22 17 19 22 14 15 18 14 21 3! How can we fix the binary tree so it is once again a nearly complete binary tree?! Solution: remove the rightmost leaf at the deepest level and use it for the new root CPSC 259 Binary Heaps and Heapsort Page 26 9 11

The reheapdown method! Our tree is now a nearly complete binary tree, but no longer a heap! However, only the root lacks the heap property 11 22 17 19 22 14 15 18 14 21 3! We can siftup the root 9! After doing this, one and only one of its children may have lost the heap property CPSC 259 Binary Heaps and Heapsort Page 27

The reheapdown method! Now the left child of the root (still the number 11) lacks the heap property 22 11 17 19 22 14 15 18 14 21 3 9! We can siftup this node! After doing this, one and only one of its children may have lost the heap property CPSC 259 Binary Heaps and Heapsort Page 28

The reheapdown method! Now the right child of the left child of the root (still the number 11) lacks the heap property: 22 22 17 19 11 14 15 18 14 21 3 9! We can siftup this node! After doing this, one and only one of its children may have lost the heap property but it doesn t, because it s a leaf CPSC 259 Binary Heaps and Heapsort Page 29

The reheapdown method! Our tree is once again a heap, because every node in it has the heap property 22 22 17 19 21 14 15 18 14 11 3 9! Once again, the largest (or a largest) value is in the root CPSC 259 Binary Heaps and Heapsort Page 30

In-class exercise! Build a binary Max-heap using the following numbers! 2,7,26,25,19,17,1,90,3,36 90 36 17 25 26 7 1 2 3 19! Now remove max and heapify 36 26 17 25 19 7 1 2 3 CPSC 259 Binary Heaps and Heapsort Page 31

Mapping into an array 25 22 17 19 22 14 15 18 14 21 3 9 11 0 1 2 3 4 5 6 7 8 9 10 11 12 25 22 17 19 22 14 15 18 14 21 3 9 11! Because of the heap's shape, node values can be stored in an array! Why can t we do the same with BSTs? CPSC 259 Binary Heaps and Heapsort Page 32

CPSC 259 Admin notes! No Labs this week! Extra Office hours to help you with the take-home! Marika: Monday Nov 10 1:30-2:30! Giorgio: Thursday Nov 13 1:00 2:00! Pearl: Friday Nov 14 9:00 10:00! Lauren: Friday Nov 14 3:00 - :4:00 CPSC 259 Binary Heaps and Heapsort Page 33

So, where were we! A priority queue is an abstract data type (ADT) that maintains a bag of items! push() Adds an element to the collection! top() Looks at the element with the highest priority! pop() Removes this element! A node has the heap property if the value in the node has higher priority than the values in its children! A minimum binary heap is a nearly complete binary tree, such that the data all nodes hold the heap property.! To add new node reheapup! To remove a node reheapdown CPSC 259 Binary Heaps and Heapsort Page 34

Adding an item to a heap! If a new item is added to the heap, use ReheapUp to maintain a max heap. /* This function performs the Reheapup operation on an array, to establish heap properties (for a subtree).!! PARAM: "data integer array containing the heap! top subscript of the root! bottom - subscript of the added element! */! void ReheapUp( int * data, int top, int bottom ){! if (bottom > top) {! int parent = getparent(bottom);! if (data[parent] < data[bottom]) {! swap( &data[parent], &data[bottom]);! ReheapUp(data, top, parent);! }! }! } CPSC 259 Binary Heaps and Heapsort Page 35

In-class exercise! For the max heap below draw the recursion tree of ReheapUp(data, 0, 12), where 25 is stored in data[0]! 25 22 17 19 22 14 15 18 14 21 3 9 26 ReheapUp(data, 0, 12) ReheapUp(data, 0, 5) ReheapUp(data, 0, 2) ReheapUp(data, 0, 0) CPSC 259 Binary Heaps and Heapsort Page 36

Removing an item from a heap! Use ReheapDown to remove an item from a max heap /* This function performs the ReheapDown operation on an array, to establish heap properties (for a subtree).!! PARAM: data integer array containing the heap! top subscript of the root! bottom subscript of the final elements in heap! */! void ReheapDown( int * data, int top, int bottom){! if (!isleaf(top, bottom)){ /* top is not a leaf */! int maxchild = getmaxchild(top) /* subscript of the! child having largest data value */!! if ( data[top] < data[maxchild] ){! swap( &data[top], &data[maxchild])! ReheapDown( data, maxchild, bottom);! }! }! }! CPSC 259 Binary Heaps and Heapsort Page 37

In-class exercise! For the max heap below draw the recursion tree of ReheapDown(data, 0, 10).! 9 22 17 19 22 14 15 18 14 21 3 ReheapDown(data, 0, 10) ReheapDown(data, 1, 10) ReheapDown(data, 4, 10) ReheapDown(data, 9, 10) CPSC 259 Binary Heaps and Heapsort Page 38

When performing either a ReheapUp or ReheapDown operation, the number of operations depends on the depth of the tree. Notice that we traverse only one path or branch of the tree. Recall that a nearly complete binary tree of height h has between 2 h and 2 h+1-1 nodes: F Time Complexity D J F D J H H K L M We can now determine the height of a heap in terms of the number of nodes N in the heap. The height is lg N. The time complexity of the ReheapUp and ReheapDown operations is therefore O(lg N). CPSC 259 Binary Heaps and Heapsort Page 39

Time Complexity Inserting an item into a binary heap (operation Push for a priority queue) would amount to performing a ReheapUp operation and therefore takes worst case O( lg N ) time. Removing the value element with the highest priority from a heap (operation pop from a priority queue) would amount to performing a ReheapDown operation and is O(lg N ). Recall that when a sorted or unsorted linear data structure (such as a linked list or an array) is used as the underlying concrete data type, then one or the other of Inserting and Removing Min is O(N). CPSC 259 Binary Heaps and Heapsort Page 40

The Heapsort Algorithm Motivation: In this section, we examine a sorting algorithm that guarantees worst case O( N lg N ) time. We will use a minimum binary heap to sort an array of data into descending order. Similarly, a maximum binary heap can be used to sort data into ascending order. (Or, in each case, you can simply read the resulting array backwards to get the reverse order; so, technically, it doesn t matter.) Recall that a minimum binary heap is a nearly complete binary tree such that the data in each node is less than or equal to the data in each of its children s nodes. Also, recall that because a binary heap is a nearly complete binary tree, it can be represented conveniently using an array. CPSC 259 Binary Heaps and Heapsort Page 41

The Heapsort algorithm The Heapsort algorithm consists of 2 phases: 1. [Heapify] Build a heap using the elements to be sorted. 2. [Sort] Use the heap to sort the data. We ll consider two algorithms for the heapify part CPSC 259 Binary Heaps and Heapsort Page 42

Heapify method 1! Adding the elements one add a time with a reheapup! See http://visualgo.net/heap.html /* This function builds a heap from an array.!! PARAM: data integer array (no order is assumed)! top - subscript of the root! bottom subscript of the final elements in heap! */! void Heapify1( int * data, int top, int bottom )! {! int index = 1;! while (index < bottom){! ReheapUp(data, top, index);! index ++;! }! }! CPSC 259 Binary Heaps and Heapsort Page 43

8 8 10 10 10 8 8 5 10 10 12 8 5 12 5 10 5 12 8 8! Complexity analysis:! we add each of n nodes and each node has to be sifted up, possibly as far as the root! Since the binary tree is a nearly complete binary tree, sifting up a single node takes O(lg N) time! Since we do this n times, Heapify1 takes n*o(lg N) time, that is, O(N lg N) time CPSC 259 Binary Heaps and Heapsort Page 44

! Heapify method 2! See http://visualgo.net/heap.html /* This function builds a heap from an array.!! PARAM: "data integer array (no order is assumed)! top - subscript of the root! bottom subscript of the final elements in heap! */! void Heapify2( int * data, int top, int bottom )! {! int index = subscript of last parent node in entire tree;! while (index => top){! /* go backwards from the last parent */! ReheapDown( data, index, bottom );! index --;! }! } CPSC 259 Binary Heaps and Heapsort Page 45

In-class exercise Example: Convert the following array to a min-heap: 8 9 7 3 2 5 0 1 To do so, picture the array as a nearly complete binary tree: 8 9 3 2 7 5 0 1 CPSC 259 Binary Heaps and Heapsort Page 46

8 8 9 7 9 0 1 2 5 0 1 2 5 7 3 3 8 0 1 0 1 5 3 2 5 7 3 2 8 7 9 9 CPSC 259 Binary Heaps and Heapsort Page 47

CPSC 259 Admin notes! Lab4 take-home grades are now available on Connect! Quiz 5 (last one) during labs next week! On Binary Search Trees, Binary Heaps, and Sorting! Quiz5 is more challenging than the previous ones! Recommended exercises on Binary Heaps will be posted later today! CPSC259 Final DEC 02, 12:00 PM OSBO A CPSC 259 Binary Heaps and Heapsort Page 48

So, where were we! A priority queue is an abstract data type (ADT) that maintains a bag of items! push(), top() pop()! A node has the heap property if the value in the node has higher priority than the values in its children! A binary heap is a nearly complete binary tree, such that the data all nodes hold the heap property.! To add a new node reheapup and to remove a node reheapdown! Heapsort, Heapify à two methods CPSC 259 Binary Heaps and Heapsort Page 49

Time complexity of Heapify2! We can determine the time complexity of the Heapify2 function by looking at the total number of times the comparison and swap operations occur while building the heap. Let us consider the worst case, which is! when the last level in the heap is full, and all of the nodes with high priorities are in the leafs.! We will colour all the paths from each node, starting with the lowest parent and working up to the root, each going down to a leaf node. The number of edges on the path from each node to a leaf node represents an upper bound on the number of comparison and swap operations that will occur while applying the ReheapDown operation to that node. By summing the total length of these paths, we will determine the time complexity of the Heapify2 function. CPSC 259 Binary Heaps and Heapsort Page 50

Time complexity of Heapify2 In the worse possible case how many swaps are going to take place? CPSC 259 Binary Heaps and Heapsort Page 51

Time complexity of Heapify2 Note that no edge is coloured more than once. Hence the work done by the Heapify2 function to build the heap can be measured in terms of the number of coloured edges. CPSC 259 Binary Heaps and Heapsort Page 52

Time complexity of Heapify2 Note that no edge is coloured more than once. Hence the work done by the Heapify2 function to build the heap can be measured in terms of the number of coloured edges. CPSC 259 Binary Heaps and Heapsort Page 53

Time complexity of Heapify2 Note that no edge is coloured more than once. Hence the work done by the Heapify2 function to build the heap can be measured in terms of the number of coloured edges. CPSC 259 Binary Heaps and Heapsort Page 54

Time complexity of Heapify2 Note that no edge is coloured more than once. Hence the work done by the Heapify2 function to build the heap can be measured in terms of the number of coloured edges. CPSC 259 Binary Heaps and Heapsort Page 55

Time complexity of Heapify2 Suppose H is the height of the tree, N is the number of elements in the tree, and E is the number of edges in the tree. How many edges are there in a nearly complete tree with N elements? N-1 Total number of coloured edges or swaps = E H E H = N - 1 - H = N 1 lg N T(n) = O (N) Hence, in the worst case, the overall time complexity of the Heapify2 algorithm is: O(N) CPSC 259 Binary Heaps and Heapsort Page 56

Having built the heap, we now sort the array: 9 1 3 2 0 5 8 7 0 1 5 3 2 8 7 9 Note: In this section, we represent the data in both binary tree and array formats. It is important to understand that in practice the data is stored only as an array. void Heapsort( int * data, int top, int bottom){! Heapify2(data, top, bottom); /* build the heap first */!! while (bottom>= 0) {! print(data[top]);! data[top] = data[bottom];! ReheapDown( data, top, bottom );! bottom --;! } CPSC 259 Binary Heaps and Heapsort Page 57

Time Complexity of Heapsort We need to determine the time complexity of the Heapify O(N) operation, and the time complexity of the subsequent sorting operation. The time complexity of the sorting operation once the heap has been built is fairly easy to determine. For each element in the heap, we perform a single swap and a ReheapDown. If there are N elements in the heap, the ReheapDown operation is O( lg N ), and hence the sorting operation is O( N lg N ). Hence, in the worst case, the overall time complexity of the Heapsort algorithm is: O(N) + O(N lg N) = O(N lg N) build heap from unsorted array essentially perform N RemoveMin s CPSC 259 Binary Heaps and Heapsort Page 58

Learning Goals revisited! Provide examples of appropriate applications for priority queues and heaps.! Implement and manipulate a heap using an array as the underlying data structure.! Describe and use the Heapify ( Build Heap ) and Heapsort algorithms.! Analyze the complexity of operations on a heap. CPSC 259 Binary Heaps and Heapsort Page 59