Heaps. Heaps. A heap is a complete binary tree.

Similar documents
Priority Queues (Heaps)

Data Structure Lecture#12: Binary Trees 3 (Chapter 5) U Kang Seoul National University

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

Heaps. Heaps Priority Queue Revisit HeapSort

CE 221 Data Structures and Algorithms

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Priority Queues (Heaps)

Programming II (CS300)

CMSC 341. Binary Heaps. Priority Queues

CS350: Data Structures Heaps and Priority Queues

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

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

Priority queues. Priority queues. Priority queue operations

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

Priority Queues Heaps Heapsort

CS165: Priority Queues, Heaps

Heaps, Heap Sort, and Priority Queues.

COMP : Trees. COMP20012 Trees 219

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CSE 214 Computer Science II Heaps and Priority Queues

Programming II (CS300)

Priority Queues. Chapter 9

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Priority Queues Heaps Heapsort

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

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

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

CSE 2123: Collections: Priority Queues. Jeremy Morris

CMSC 341 Lecture 14: Priority Queues, Heaps

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

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

Priority Queues and Binary Heaps

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

Algorithms and Data Structures

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

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

Heap sort. Carlos Moreno uwaterloo.ca EIT

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

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

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

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

9. Heap : Priority Queue

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

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

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

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

Appendix A and B are also worth reading.

The beautiful binary heap.

Module 2: Priority Queues

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

Lecture Notes on Priority Queues

Algorithms and Data Structures

Priority Queues 1 / 15

Chapter 6 Heap and Its Application

Module 2: Priority Queues

CSCI2100B Data Structures Heaps

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

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

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Chapter 6 Heapsort 1

Heapsort. Why study Heapsort?

Data Structures and Algorithms

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Priority Queues, Binary Heaps, and Heapsort

Properties of a heap (represented by an array A)

Binary Heaps. CSE 373 Data Structures Lecture 11

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

arxiv: v3 [cs.ds] 18 Apr 2011

Heaps Goodrich, Tamassia. Heaps 1

Binary Heaps in Dynamic Arrays

Heapsort. Heap data structure

The number of nodes, N, in a binary tree of height, h is: 2 N 2 The height of a binary tree is: h =

Data Structures and Algorithms for Engineers

Exam Data structures DIT960/DAT036

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

Data Structures Lesson 9

Figure 1: A complete binary tree.

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

CS302 - Data Structures using C++

Transcription:

A heap is a complete binary tree. 1 A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. A min-heap is defined similarly. 97 Mapping the elements of a heap into an array is trivial: if a node is stored at index k, then its left child is stored at index 2k+1 and its right child at index 2k+2 90 79 83 81 42 55 73 21 83 84 0 1 2 3 4 5 6 7 8 9 10 11 97 84 90 79 83 81 42 55 73 21 83

Building a Heap 2 The fact that a heap is a complete binary tree allows it to be efficiently represented using a simple array. Given an array of N values, a heap containing those values can be built, in situ, by simply sifting each internal node down to its proper location: - start with the last internal node - swap the current internal node with its larger child, if necessary - then follow the swapped node down - continue until all internal nodes are done 74 73 79 90 90 73 * 79 74 81 * * 81 74 73 79 90 81 90 79 74 81 73 *

Heap Class Interface We will consider a somewhat minimal maxheap class: public class BinaryHeap<T extends Comparable<? super T>> { 3 private static final int DEFCAP = 10; private int size; private T [] elems; // default array size // # elems in array // array of elems public BinaryHeap() {... // construct heaps in public BinaryHeap( int capacity ) {... // various ways public BinaryHeap( T [ ] items ) {... public boolean insert( T D ) {... public T findmax() {... public T deletemax() {... public void clear() {... public boolean isempty() {... private void siftdown( int hole ) {... private void buildheap() {... public void printheap() {... // insert new elem // get maximum element // remove maximum element // reset heap to empty // is heap empty? // siftdown algorithm // heapify // linear display

buildheap and siftdown As described earlier: 4 private void buildheap() { // first elem is stored at index 1, not 0 for ( int idx = size / 2; idx > 0; idx-- ) siftdown( idx ); QTP: Why is idx initialized this way? private void siftdown(int hole) { int child; T tmp = elems[ hole ]; for ( ; hole * 2 <= size; hole = child ) { Determine which child node is larger child = hole * 2; if ( child!= size && elems[ child + 1 ].compareto( elems[ child ] ) > 0 ) child++; if ( elems[ child ].compareto( tmp ) > 0 ) elems[ hole ] = elems[ child ]; else break; elems[ hole ] = tmp; If child is larger than parent, it must move up Finally, put starting value in right place

Cost of siftdown private void siftdown(int hole) { int child; T tmp = elems[ hole ]; for ( ; hole * 2 <= size; hole = child ) { In the worst case, we perform two element comparisons 5 child = hole * 2; if ( child!= size && elems[ child + 1 ].compareto( elems[ child ] ) > 0 ) child++; if ( elems[ child ].compareto( tmp ) > 0 ) elems[ hole ] = elems[ child ]; else break; elems[ hole ] = tmp; In a complete binary tree of N nodes, the number of levels is at most 1 + log(n). Since each non-terminating iteration of the loop moves the target value a distance of 1 level, the loop will perform no more than log(n) iterations. Thus, the worst case cost of SiftDown() is Θ(log N). and one element copy per iteration That s 2log(N) comparisons and log(n) swaps.

Cost of buildheap Suppose we start with a complete binary tree with N nodes; the number of steps required for sifting values down will be maximized if the tree is also full, in which case N = 2 d -1 for some integer d = log N. For example: 6 42 level 0: 2 0 nodes, can sift down d - 1 levels 83 21 level 1: 2 1 nodes, can sift down d - 2 levels 90 55 81 level 2: 2 2 nodes, can sift down d - 3 levels 97 37 73 84 83 95 62 17 We can prove that in general, level k of a full and complete binary tree will contain 2 k nodes, and that those nodes are d k 1 levels above the leaves. Thus

Cost of buildheap 7 In the worst case, the number of comparisons BuildHeap() will require in building a heap of N nodes is given by: Comparisons d 1 d 1 d 1 k k 22 d k 1 2d 1 2 2 k0 d d 1 2 N log N 2 2 k0 k0 k2 k1 Since, at worst, there is one move for each two comparisons, the maximum number of element moves is N log N + 1. Hence, building a heap of N nodes is Θ(N) in both comparisons and element moves.

Deleting the Root of a Heap We will see that the most common operation on a heap is the deletion of the root node. The heap property is maintained by sifting down 8 public T deletemax() { if ( isempty( ) ) throw new RuntimeException( ); Check for empty heap T maxitem = elems[ 1 ]; elems[ 1 ] = elems[ size ]; size--; siftdown( 1 ); return maxitem; Save the root (at index 1), replace it with the last leaf, shrink the heap by one node. Sift the new root down to restore the heap property. QTP: Why is the last leaf chosen as the replacement for the root?

Example of Root Deletion Given the initial heap: 9 97 84 90 79 83 81 83 42 55 73 21 83 84 90 79 83 81 42 55 73 21 83 84 90 79 83 81 In a heap of N nodes, the maximum distance the root can sift down would be log N - 1. 42 55 73 21

Heap Sort A list can be sorted by first building it into a heap, and then iteratively deleting the root node from the heap until the heap is empty. If the deleted roots are stored in reverse order in an array they will be sorted in ascending order (if a max heap is used). 10 public static void heapsort(integer[] List, int Sz) { BinaryHeap<Integer> tosort = new BinaryHeap<Integer>(List, Sz); int Idx = Sz - 1; while (!tosort.isempty() ) { List[Idx] = tosort.deletemax(); Idx--;

Cost of Deleting the Roots Recalling the earlier analysis of building a heap, level k of a full and complete binary tree will contain 2 k nodes, and that those nodes are k levels below the root level. So, when the root is deleted the maximum number of levels the swapped node can sift down is the number of the level from which that node was swapped. Thus, in the worst case, for deleting all the roots 11 Comparisons 2 d 1 k1 2N k2 k 4 d 1 k1 k2 k1 4 log N 2log N 4N d 2 2 d 1 1 As usual, with Heap Sort, this would entail essentially the same number of element moves.

Cost of Heap Sort Adding in the cost of building the heap from our earlier analysis, 12 TotalComparisons 2N 2log N 2N log N 2log N 2N log N 2N 4N and... TotalSwaps N log N N So, in the worst case, Heap Sort is (N log N) in both swaps and comparisons.

Priority Queues A priority queue consists of entries, each of which contains a key field, called the priority of the entry. 13 Aside from the usual operations of creation, clearing, tests for full and empty, and reporting its size, a priority queue has only two operations: - insertion of a new entry - removal of the entry having the largest (or smallest) key Key values need not be unique. If not, then removal may target any entry holding the largest value.

Representation Representation of a priority queue may be achieved using: - a sorted list, but - an unsorted list, but - a max-heap 14 Priority queues may be used to manage prioritized processes in a time-sharing environment, time-dependent simulations, and even numerical linear algebra.