Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Similar documents
Heaps and Priority Queues

Priority Queues 3/19/14

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

Priority Queues & Heaps. Chapter 9

Data Structures and Algorithms " Priority Queues!

Priority Queues Goodrich, Tamassia. Priority Queues 1

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps

Heaps. 2/13/2006 Heaps 1

Heaps Goodrich, Tamassia. Heaps 1

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Stores a collection of elements each with an associated key value

Priority Queues and Heaps. Heaps and Priority Queues 1

Priority queues. Priority queues. Priority queue operations

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

Priority queues. Priority queues. Priority queue operations

Data Structures Lecture 7

Priority Queues. Reading: 7.1, 7.2

Elementary Data Structures 2

Priority Queues. INFO0902 Data Structures and Algorithms. Priority Queues (files à priorités) Keys. Priority Queues

Chapter 2: Basic Data Structures

Data Structures and Algorithms

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

Algorithms and Data Structures

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4.

Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019

Programming II (CS300)

Programming II (CS300)

9. Heap : Priority Queue

Sorting and Searching

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Sorting and Searching

Data Structures and Algorithms

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CE 221 Data Structures and Algorithms

Priority Queues and Binary Heaps

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Binary Trees, Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

CS350: Data Structures Heaps and Priority Queues

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

Properties of a heap (represented by an array A)

Priority Queues and Heaps. Heaps of fun, for everyone!

Binary Heaps in Dynamic Arrays

CSCI2100B Data Structures Heaps

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Algorithms and Data Structures

Heaps, Heap Sort, and Priority Queues.

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CS/COE 1501

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

Topics on the Midterm

Module 2: Priority Queues

Module 2: Priority Queues

Algorithms and Data Structures

DataStruct 8. Heaps and Priority Queues

Partha Sarathi Manal

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

B-Trees and External Memory

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Dictionaries. 2/17/2006 Dictionaries 1

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

B-Trees and External Memory

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Data Structures and Algorithms for Engineers

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Binary heaps (chapters ) Leftist heaps

The priority is indicated by a number, the lower the number - the higher the priority.

CSE 214 Computer Science II Heaps and Priority Queues

Module 2: Priority Queues

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Priority Queues, Binary Heaps, and Heapsort

Entry and Priority Queue ADTs

Chapter 10: Search Trees

CMSC 341 Lecture 14: Priority Queues, Heaps

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

csci 210: Data Structures Priority Queues and Heaps

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

DATA STRUCTURES AND ALGORITHMS

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Heapsort. Heap data structure

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Topic: Heaps and priority queues

Merge Sort Goodrich, Tamassia Merge Sort 1

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Transcription:

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods 1 2 Given : Stack A, Stack B 3 // based on requirement b will be reverse of a 4 add first(e) 5 { 6 A. push(e) ; 7 } 8 remove first(e) 9 { 10 A. pop() 11 } 12 add last(e) 13 { 14 B.push(e) ; 15 } 16 remove last () 17 { 18 B.pop() ; 19 } Running Time=constant Problems 1

Let T be a tree with n positions. Define the lowest common ancestor(lca) between two positions p and q as the lowest point in T that has both p and q as descendants. Given two positions p and q, describe an efficient algorithm for finding the LCA of p and q. What is the running time of your algorithm? Problems 2

Priority Queues and Readings - Chapter 9

Design ADT Question! Scheduler for simulated CPU Jobs " each job has a priority " from among all jobs waiting to be processed in a time slice, the CPU must work on a job with highest priority " each job has a unique id and an attribute length that indicates the time required to process the job " once a job is scheduled, it runs for the required amount of time " when a job finishes we should remove it from our collection " jobs can be submitted to the scheduler at any point in time Prority Queues 4

Priority Queue ADT! A priority queue stores a collection of entries! Each entry is a pair (key, value)! Main methods of the Priority Queue ADT " insert(k, v) inserts an entry with key k and value v " removemin() removes and returns the entry with smallest key, or null if the the priority queue is empty! Additional methods " min() returns, but does not remove, an entry with smallest key, or null if the the priority queue is empty " size(), isempty() 2014 Goodrich, Tamassia, Goldwasser Prority Queues 5

Example! A sequence of priority queue methods: 2014 Goodrich, Tamassia, Goldwasser Prority Queues 6

Total Order Relations! Keys in a priority queue can be arbitrary objects on which an order is defined! Two distinct entries in a priority queue can have the same key! Mathematical concept of total order relation " Comparability property: either x y or y x " Antisymmetric property: x y and y x x = y " Transitive property: x y and y z x z 2014 Goodrich, Tamassia, Goldwasser Prority Queues 7

Entry ADT! An entry in a priority queue is simply a keyvalue pair! Priority queues store entries to allow for efficient insertion and removal based on keys! Methods: " getkey: returns the key for this entry! As a Java interface: /** * Interface for a key-value * pair entry **/ public interface Entry<K,V> { } K getkey(); V getvalue(); " getvalue: returns the value associated with this entry 2014 Goodrich, Tamassia, Goldwasser Prority Queues 8

Comparator ADT! A comparator encapsulates the action of comparing two objects according to a given total order relation! The comparator is external to the keys being compared! When the priority queue needs to compare two keys, it uses its comparator! Primary method of the Comparator ADT! compare(x, y): returns an integer i such that " i < 0 if a < b, " i = 0 if a = b " i > 0 if a > b " An error occurs if a and b cannot be compared. 2014 Goodrich, Tamassia, Goldwasser Prority Queues 9

Example Comparator! Lexicographic comparison of 2-D points: /** Comparator for 2D points under the standard lexicographic order. */ public class Lexicographic implements Comparator { int xa, ya, xb, yb; public int compare(object a, Object b) throws ClassCastException { xa = ((Point2D) a).getx(); ya = ((Point2D) a).gety(); xb = ((Point2D) b).getx(); yb = ((Point2D) b).gety(); if (xa!= xb) return (xb - xa); else return (yb - ya); } }! Point objects: /** Class representing a point in the plane with integer coordinates */ public class Point2D { protected int xc, yc; // coordinates public Point2D(int x, int y) { xc = x; yc = y; } public int getx() { return xc; } public int gety() { return yc; } } 2014 Goodrich, Tamassia, Goldwasser Prority Queues 10

Sequence-based Priority Queue! Implementation with an unsorted list! Implementation with a sorted list 4 5 2 3 1 1 2 3 4 5! Performance: " insert takes O(1) time since we can insert the item at the beginning or end of the sequence " removemin and min take O(n) time since we have to traverse the entire sequence to find the smallest key! Performance: " insert takes O(n) time since we have to find the place where to insert the item " removemin and min take O(1) time, since the smallest key is at the beginning 2014 Goodrich, Tamassia, Goldwasser Prority Queues 11

Better Implementation of Priority Queue?

2 5 6 9 7

! A heap is a binary tree storing keys at its nodes and satisfying the following properties:! Heap-Order: for every internal node v other than the root, key(v) key(parent(v))! Complete Binary Tree: let h be the height of the heap " for i = 0,, h 1, there are 2 i nodes of depth i " at depth h 1, the internal nodes are to the left of the external nodes! The last node of a heap is the rightmost node of maximum depth heap property 9 Structural property 5 7 2 6 last node 2014 Goodrich, Tamassia, Goldwasser 14

Example of non-! Heap property violation! Structural property violation 2 2 5 6 5 6 4 7 9 7 15

Finding the minimum element! The element with the smallest key value always sits at the root of the heap. " If it is elsewhere, it would have a parent with a larger key, thus violating the heap property. " hence finding the minimum can be done in O(1) time 16

Height of a Heap! Theorem: A heap storing n keys has height O(log n) Proof: (we apply the complete binary tree property) " Let h be the height of a heap storing n keys " Since there are 2 i keys at depth i = 0,, h 1 and at least one key at depth h, we have n 1 + 2 + 4 + + 2 h 1 + 1 " Thus, n 2 h, i.e., h log n depth 0 1 h 1 h keys 1 2 2 h 1 1 2014 Goodrich, Tamassia, Goldwasser 17

Array-based Heap Implementation! We can represent a heap with n keys by means of an array of length n! For the node at index i " the left child is at index 2i + 1 " the right child is at index 2i + 2 " parent(i) is floor((i-1)/2)! Operation add corresponds to inserting at index n + 1! Operation remove_min corresponds to removing at index n 9 2 5 6 7 2 5 6 9 7 0 1 2 3 4 18

and Priority Queues! We can use a heap to implement a priority queue! We store a (key, element) item at each internal node! We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) 2014 Goodrich, Tamassia, Goldwasser 19

Insertion into a Heap! Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap! The insertion algorithm consists of three steps " Find the insertion node z (the new last node) " Store k at z " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (2, T) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 20

Insertion into a Heap! Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap! The insertion algorithm consists of three steps " Find the insertion node z (the new last node) " Store k at z " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (2, T) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 21

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 22

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 23

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 24

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (2,T) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 25

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (2,T) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 26

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (2,T) (5,A) (4,C) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 27

Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (2,T) (5,A) (4,C) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 28

Another View of Insertion! Enlarge heap! Consider path from root to inserted node! Find topmost element on this path with higher priority that of inserted element! Insert new element at this location by shifting down the other elements on the path (4,C) (2,T) (5,A) (6,Z) (5,A) (4,C) (15,K) (9,F) (7,Q) (20,B) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 29

Correctness of Upheap! The only nodes whose contents change are the ones on the path! Heap property may be violated only for children on these nodes! But new contents of these nodes only have lower priority! So heap property is not violated (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 30

Removal from a Heap! Method removemin of the priority queue ADT corresponds removing the root key from the heap! The removal algorithm consists of three steps " Replace the root key with the key of the last node w " Remove w " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (4, C) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 31

Removal from a Heap! Method removemin of the priority queue ADT corresponds removing the root key from the heap! The removal algorithm consists of three steps " Replace the root key with the key of the last node w " Remove w " Restore the heap-order property (discussed next) (4,C) (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 32

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 33

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 34

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (13,W) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 35

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 36

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 37

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (12,H) (7,Q) (20,B) (16,X) (25,J) (14,E) (11,S) 38

Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (12,H) (7,Q) (20,B) (16,X) (25,J) (14,E) (13,W) (11,S) 39

Correctness of Downheap! Downheap traces a path down the tree! For a node on the path (say j) - both key(left(j)) and key(right(j)) > key(j)! All elements on path have lower key values than their siblings! All elements on this path are moved up.! Hence the heap property is not violated. (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 40

Run Time Analysis! heap of n nodes has height O(log n)! insertion - Upheap move the element all the way to the top " O(log n) steps in worst case! removal Downheap move the root element all the way to a leaf " O(log n) steps in worst case 41

Array-based Heap Implementation! We can represent a heap with n keys by means of an array of length n! For the node at index i " the left child is at index 2i + 1 " the right child is at index 2i + 2 " parent(i) is floor((i-1)/2)! Operation add corresponds to inserting at index n + 1! Operation remove_min corresponds to removing at index n 9 2 5 6 7 2 5 6 9 7 0 1 2 3 4 42

Building a heap! Call Upheap procedure of the heap n times. " Upheap is O(log n) " inserting first element O(log 1) " inserting second element O(log 2) ".. " inserting the nth element O(log n) " so for the n insertions log 1+ log 2+ +log n = log n! = O(nlog n) 43

Merging Two! We are given two heaps and a key k! We create a new heap with the root node storing k and with the two heaps as subtrees 8 3 3 5 7 4 2 2 6! We perform Downheap to restore the heaporder property 8 5 2 4 6 3 4 8 5 7 6 2014 Goodrich, Tamassia, Goldwasser 44

Building a Heap Bottomup 16 15 4 12 6 7 23 20 45

Building a Heap Bottomup 25 9 11 17 16 15 4 12 6 7 23 20 46

Building a Heap Bottomup 15 4 6 17 16 25 9 12 11 7 23 20 47

Building a Heap Bottomup 5 8 15 4 6 17 16 25 9 12 11 7 23 20 48

Building a Heap Bottomup 4 6 15 5 7 17 16 25 9 12 11 8 23 20 49

Building a Heap Bottomup 14 4 6 15 5 7 17 16 25 9 12 11 8 23 20 50

Building a Heap Bottomup 4 5 6 15 9 7 17 16 25 14 12 11 8 23 20 51

Bottom-up Heap Construction! We can construct a heap storing n given keys in using a bottom-up construction with log n phases! In phase i, pairs of heaps with 2 i 1 keys are merged into heaps with 2 i+1 1 keys 2 i 1 2 i 1 2 i+1 1 2014 Goodrich, Tamassia, Goldwasser 52

Building a Heap Bottomup Analysis! Correctness: induction on i, all trees rooted at m > i are heaps! Running time: n calls to Downheap " Downheap O(log n) " so total running time O(nlog n)! We can provide a better bound O(n) " Idea for most of the time, Downheap works on smaller than n element heaps 53

Building a Heap Bottomup Analysis (2)! height of node: length of longest path from node to leaf! height of tree: height of root height h h-1 1 0 keys 1 2 2 h 1 2 h 54

Building a Heap Bottomup Analysis (3)! height of node: length of longest path from node to leaf! height of tree: height of root! time for Downheap(i) = O(height of subtree rooted at i)! Let us assume a complete binary tree " n = 2 h+1-1 55

Building a Heap Bottomup Analysis (4) 3 Total work for B 3*1 2 2 2*2 1 1 1 1 1*4 0 0 0 0 0 0 0 0 0*8 56

Building a Heap Bottomup Analysis (5)! 0 th level - 2 h nodes, no need to call Downheap! 1 st level - 2 h-1 nodes, Downheap - 1 swap! 2 nd level - 2 h-2 nodes, Downheap - 2 swaps! j th level - 2 h-j nodes, Downheap - j swaps! h th level 1 node, Downheap h swaps! So total number of swaps is h h j2 h j = j 2h j=0 j=0 " It can be shown that, this is bound by 2 (h+1) " 2 (h+1) = n-1! Therefore O(n) h = 2 h j 2 j 2 j j=0 57