COMP : DATA STRUCTURES 2/27/14. Are binary trees satisfying two additional properties:

Similar documents
Priority Queues & Heaps

Priority Queues & Heaps

Comparison Sorts. EECS 2011 Prof. J. Elder - 1 -

Midterm Review. EECS 2011 Prof. J. Elder - 1 -

Priority Queues & Heaps

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :51 PM

ECE250: Algorithms and Data Structures Trees

Midterm Review. EECS 2011 Prof. J. Elder - 1 -

Maps, Hash Tables and Dictionaries

Maps and Hash Tables. EECS 2011 Prof. J. Elder - 1 -

File Systems: Fundamentals

Chapter 8: Recursion

Optimization Strategies

Mojdeh Nikdel Patty George

ISO Stand Alone Remittance Messages. Introduced in April 2014

Frequency-dependent fading bad for narrowband signals. Spread the narrowband signal into a broadband signal. dp/df. (iii)

Support Vector Machines

History. Year 9 Home Learning Task

Supreme Court of Florida

Constraint satisfaction problems. Lirong Xia

Uninformed search. Lirong Xia

CS 5523: Operating Systems

Contents. Bibliography 121. Index 123

A Bloom Filter Based Scalable Data Integrity Check Tool for Large-scale Dataset

VOTING DYNAMICS IN INNOVATION SYSTEMS

Issue Overview: How the U.S. elects its presidents

New Hampshire Secretary of State Electronic Ballot Counting Devices

Audit of Political Engagement

Barrington Heights Homeowners Association ASSOCIATION MEMBERSHIP MEETING AND VOTING RULES (Civil Code Section ) Effective October 16, 2008

UPDATE ON INTEGRATED PLANNING AND THE PPP

Platform independent proc interface

IN THE THIRTEENTH JUDICIAL CIRCUIT HILLSBOROUGH COUNTY, FLORIDA. ADMINISTRATIVE ORDER S (Supersedes Second Amendment to Local Rule 3)

Text UI. Data Store Ø Example of a backend to a real Could add a different user interface. Good judgment comes from experience

IEEE PES Substations Committee

CHIEF JUDGE TRAINING. May 15, 2018 Primary

Factors influencing Latino immigrant householder s participation in social networks in rural areas of the Midwest

Minimum Spanning Tree Union-Find Data Structure. Feb 28, 2018 CSCI211 - Sprenkle. Comcast wants to lay cable in a neighborhood. Neighborhood Layout

Media Resource Centre Program Guidelines Production Initiative (PIP) & Livecast

Batch binary Edwards. D. J. Bernstein University of Illinois at Chicago NSF ITR

c M. J. Wooldridge, used by permission/updated by Simon Parsons, Spring

Social welfare functions

Large Group Lesson. Introduction Video This teaching time will introduce the children to what they are learning for the day.

Title: Adverserial Search AIMA: Chapter 5 (Sections 5.1, 5.2 and 5.3)

LECTURE 10 Labor Markets. April 1, 2015

Understanding True Position with MMC in Calypso. Last Updated: 9/15/2014 True Position with MMC 1

Homework 7 Answers PS 30 November 2013

Get Started with your UKnight Interactive Assembly Site First Steps. v.1.0

Complexity of Manipulating Elections with Few Candidates

Chapter 5 Work Laws And Responsibilities Worksheet Answers

Voting Criteria April

Concurrent Programing: Why you should care, deeply. Don Porter Portions courtesy Emmett Witchel

The optical memory card is a Write Once media, a written area cannot be overwritten. Information stored on an optical memory card is non-volatile.

PLASTICA. Martin. Levelling Components. Made in Italy

Political Districting for Elections to the German Bundestag: An Optimization-Based Multi-Stage Heuristic Respecting Administrative Boundaries

Estimating the Margin of Victory for Instant-Runoff Voting

INSTRUCTIONS FOR ASSESSMENT OF THE ELECTION PROCESS

Update ,000 Missing Jobs: Wisconsin s Lagging Sectors

POSITIVE POLITICAL THEORY

SOE Handbook on Certifying Candidate Petitions

Servilla: Service Provisioning in Wireless Sensor Networks. Chenyang Lu

Tengyu Ma Facebook AI Research. Based on joint work with Rong Ge (Duke) and Jason D. Lee (USC)

Polydisciplinary Faculty of Larache Abdelmalek Essaadi University, MOROCCO 3 Department of Mathematics and Informatics

ICA Discussion Paper of Possible Futures. for. The International CPTED Association

Thinking back to the Presidential Election in 2016, do you recall if you supported ROTATE FIRST TWO, or someone else?

Acts of Piracy demand the death penalty Page - 1

Liberalism and Neoliberalism

The HeLIx + inversion code Genetic algorithms. A. Lagg - Abisko Winter School 1

Chief Electoral Officer Directives for the Counting of Ballots (Elections Act, R.S.N.B. 1973, c.e-3, ss.5.2(1), s.87.63, 87.64, 91.1, and 91.

Each location has a minimum of 5 workers appointed by political parties for bi-partisan representation

Voting System: elections

MARK SCHEME for the October/November 2014 series 0460 GEOGRAPHY. 0460/42 Paper 4 (Alternative to Coursework), maximum raw mark 60

L During. f!y DALLAS, TEXAS - 11nft+'~ VICE PRESIDENT HUBERT PRESIDENT'S CLUB BRIEFING ~ May 17, 1965

TITLE 17 REFUSE AND TRASH DISPOSAL 1 REFUSE

Approval Voting Theory with Multiple Levels of Approval

Enriqueta Aragones Harvard University and Universitat Pompeu Fabra Andrew Postlewaite University of Pennsylvania. March 9, 2000

CS 4407 Algorithms Greedy Algorithms and Minimum Spanning Trees

MOTIONS TO REOPEN GUIDE

Photon Identification in the Future. Andrew Askew Florida State University

INK/DOCTORING CUPS. For all hermetic standard pad printing machines. Ink/doctoring cup 1-, 2-, 3-colour

CHAPTER 23: DETENTION BASIN STANDARDS Introduction and Goals Administration Standards Standard Attachments 23.

Event Based Sequential Program Development: Application to Constructing a Pointer Program

The Commission s proposals on Legal Aid. ECBA Conference Warsaw, 26 April 2014

A constraint based dependancy parser for Sanskrit

Comparison of the Psychometric Properties of Several Computer-Based Test Designs for. Credentialing Exams

Ordinance Register 2009 ORDINANCE NO

INSTRUCTION SHEET. 1. Place left upright and right upright with hooks facing down to the ground. (Ref Picture #1)

Summary Offences (Tagging and Graffiti Vandalism) Amendment Bill

Popular Attitudes toward Democracy in Tanzania: A Summary of Afrobarometer Indicators,

BORING KITS from Ø 6 mm up to 210 mm

Teacher s Guide LAWCRAFT EXTENSION PACK STEP BY STEP INSTRUCTIONS

City of Orillia Tabulator Instructions

Vocabulary Activity 7

Kit Potentiometer Rotational Analog Displacement Sensor

General Provisions. Chapter 5. Regulators' logos and the Key facts logo

Strengthen Stewardship With Electronic Giving

Rules for the Election of Directors

Cluster Analysis. (see also: Segmentation)

Isle of Wight Council s Enforcement Policy on the Use of Fixed Penalty Notices for Environmental Offences

Carrier Bags Act (Northern Ireland) 2014

EXAMINATION 3 VERSION B "Wage Structure, Mobility, and Discrimination" April 19, 2018

Transcription:

BINARY HEAPS Two Additional Properties 9 Binary Heaps Are binary trees satisfying two additional properties: Ø Structure property: Levels are filled in order, left to right Also known as complete binary trees All levels (except possibly bottom) completely filled Bottom level filled from left to right Ø Heap order property: Key in parent of X key in X Only defined for Comparable objects Where would the minimum be? Where would the maximum be? 10 Copyright by Jasleen Kaur 1

Are These Binary Heaps? 11 Binary Heaps: Structural Properties How many nodes does a binary heap with height h have? Ø 2 h Ø 2 h+1 1 What is the height of a binary heap with N nodes? Ø floor(log 2 N) 12 Copyright by Jasleen Kaur 2

Binary Heaps: Array Implementation Structure property è heaps are complete binary trees Ø No holes in any level (including bottom) Ø Levels are filled in order (left-to-right) Good match for array implementation Ø Without wasting space Root at A[0] A[x] has left child at A[2x+1] A[x] has right child at A[2x+2] A[x] has parent at A[(x-1)/2] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 A 13-21 13 21 16 16 24 24 31 19 31 19 68 65 68 26 65 32 26 32 13 Binary Heaps: Array Implementation Structure: Ø Root at A[0] Ø A[x] has left child at A[2x+1] Ø A[x] has right child at A[2x+2] Ø A[x] has parent at A[(x-1)/2] All are bit-shift operations Optimization move everything right by 1 Ø Root at A[1] Ø A[x] has left child at A[2x] Ø A[x] has right child at A[2x+1] Ø A[x] has parent at A[x/2] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 A 13-21 13 21 16 16 24 24 31 19 31 19 68 65 68 26 65 32 26 32 14 Copyright by Jasleen Kaur 3

Binary Heap: Class Structure public class BinaryHeap<AnyType extends Comparable<? Super AnyType>> { private AnyType[] array; private int currentsize; public BinaryHeap(int capacity) { currentsize = 0; array = (AnyType[]) new Comparable[capacity+1]; public AnyType min() {... public void insert(anytype x) {... public AnyType deletemin() {... public AnyType isempty() { return currentsize == 0; public AnyType isfull(){return currentsize == array.length-1; 15 Binary Heaps: Basic Operations Required operations: min, insert, deletemin min: easy! Ø Just read the element stored at the root! // PRE: heap is not empty public AnyType min() { return array[1]; Ø Run-time? Also easy to perform the remaining two operations: Ø Mainly ensure that the two heap properties are maintained 16 Copyright by Jasleen Kaur 4

Binary Heaps: insert To insert element X to heap: Ø Create a hole at next available location To maintain structure property Ø If heap-order allows, placed X in hole Ø Else, bubble hole up toward the root Until X can be placed in hole Percolate up strategy Example: insert 14 17 Implementing insert // PRE: heap is not full public void insert(anytype x) { // Create hole currentsize++; int hole = currentsize; // Percolate up array[0] = x; for( ; x.compareto(array[hole/2]) < 0; hole = hole/2) array[hole] = array[hole/2]; array[hole] = x; Complexity of insert? Ø O(log N) arrays help with O(1) moves between levels 18 Copyright by Jasleen Kaur 5

Binary Heaps: deletemin Finding the min is easy how to remove it? Ø When min is removed, hole is created at root Ø Since heap size reduce by 1, last element X must be moved somewhere Unlikely that X can be moved to hole at root, though Ø Percolate down the hole By sliding smaller of the hole s children into it Repeat until X can be placed in the hole 19 Binary Heaps: deletemin è effectively, place X in correct spot, along a path from root containing minimum children 20 Copyright by Jasleen Kaur 6

Implementing deletemin // PRE: heap is not empty public AnyType deletemin() { // min value to be returned AnyType minitem = array[1]; // Move last item to root array[1] = array[currentsize]; currentsize--; // And percolate it down to the right place percolatedown(); return minitem; 21 Implementing percolatedown private void percolatedown() { tmp = array[1]; int hole = 1; int child; // smaller child of array[hole] Complexity of deletemin? for( ; hole*2 <= currentsize; hole = child) { child = hole*2; if (child!= currentsize && array[child+1].compareto(array[child]) < 0 ) child++; if (array[child].compareto(tmp) < 0) array[hole] = array[child]; else break; array[hole] = tmp; 22 Copyright by Jasleen Kaur 7

Binary Heaps: Other Operations Basic operations Ø min: O(1) Ø insert: O(log N) Ø deletemin: O(log N) Additional operations: Ø decreasekey(p, Δ) lower the value at position p by Δ lower value, then percolate up to maintain heap order Ø increasekey(p, Δ) increase value at position p by Δ Percolate down to maintain heap order Ø delete(p) remove node at position p decreasekey(p, infinity); deletemin(); Ø buildheap(): build heap from an initial collection of items How? 23 Binary Heaps: buildheap Use N successive calls to insert: Ø Worst-case O(N log N) Can we do better? Ø Place N items in an unsorted array While maintaining the structure property Ø For all nodes i = N/2,, 1, percolate them down one-by-one 150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130 24 Copyright by Jasleen Kaur 8

Binary Heaps: buildheap For all nodes i = N/2,, 1, percolate them down one-by-one 25 Binary Heaps: buildheap For all nodes i = N/2,, 1, percolate them down one-by-one 26 Copyright by Jasleen Kaur 9