Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *



Similar documents
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)

6 March Array Implementation of Binary Trees

Binary Heaps. CSE 373 Data Structures

Binary Heap Algorithms

Converting a Number from Decimal to Binary

Analysis of Binary Search algorithm and Selection Sort algorithm

Cpt S 223. School of EECS, WSU

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

CS711008Z Algorithm Design and Analysis

Full and Complete Binary Trees

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

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

Algorithms and Data Structures

Analysis of Algorithms I: Binary Search Trees

From Last Time: Remove (Delete) Operation

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

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)

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

CS473 - Algorithms I

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:

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

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

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

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

Chapter 14 The Binary Search Tree

Big Data and Scripting. Part 4: Memory Hierarchies

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

Exam study sheet for CS2711. List of topics

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

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

Sample Questions Csci 1112 A. Bellaachia

Symbol Tables. Introduction

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

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

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees 3/20/14

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

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

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

Why? A central concept in Computer Science. Algorithms are ubiquitous.

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Binary Search Trees CMPSC 122

Introduction to Data Structures and Algorithms

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

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

Analysis of Algorithms I: Optimal Binary Search Trees

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

Ordered Lists and Binary Trees

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Algorithms Chapter 12 Binary Search Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Algorithms. Margaret M. Fleck. 18 October 2010

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

The ADT Binary Search Tree

TREE BASIC TERMINOLOGIES

Binary search algorithm

Physical Data Organization

Quick Sort. Implementation

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

Why Use Binary Trees?

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.

Binary Search Trees. Ric Glassey

Approximation Algorithms

A Comparison of Dictionary Implementations

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Search Trees (BST)

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

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

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

External Memory Geometric Data Structures

Data Structures. Algorithm Performance and Big O Analysis

Persistent Binary Search Trees

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

Data Structures and Algorithms

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

Algorithms and Data Structures

Persistent Data Structures and Planar Point Location

Fundamental Algorithms

Data Warehousing und Data Mining

Databases and Information Systems 1 Part 3: Storage Structures and Indices

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Analysis of a Search Algorithm

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Unit Storage Structures 1. Storage Structures. Unit 4.3

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

AP Computer Science AB Syllabus 1

Lecture Notes on Binary Search Trees

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

PES Institute of Technology-BSC QUESTION BANK

Lecture Notes on Binary Search Trees

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Data Structure [Question Bank]

Transcription:

Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest priority) What data structures have we seen that has an O(log n) worst-case runtime to add? O(log n) often suggests a balanced binary tree (not necessarily a search tree). If we can peek at the highest priority in O(1) runtime in the worst case, where must be the highest priority item? At the root. Where would you expect to find the nd highest priority item? A child of the root. Does it matter in which subtree, left or right, that the nd highest item is? No. The only requirement is that it should be the highest priority item in its subtree. Binary heaps have two properties: 1. Shape property: A heap is a complete binary tree all levels are fully filled, except possibly the bottom level, which may be partially filled from left to right. The 7 smallest heap shapes: * * * * * * * / * * * * * * * * * * * / / * * * * * * * * * *. Order property: parent <= children, if highest priority is the minimum (min- heap) parent >= children, if highest priority is the maximum (max- heap) We ll consider min- heaps. Max- heaps are similar.

Which are min-heaps? 1 15 3 7 \ 14 18 5 6 9 10 / \ 7 17 5 11 17 Wrong shape 18-17 out of order Yes! Wrong shape What are all possible min-heaps on elements 1,, 3, 4? How many min-heaps are there? What shape can the tree have? Only 1 shape. What value(s) can the root have? 1 Can 4 be a child of the root? Yes. 1 1 1 3 3 4 / / / 4 4 3 Exercise: How many min- heaps are there with elements 1,, 3, 4, 5? For heap with n elements, where can we find the nd smallest element? At level 1 Will the 3 rd smallest always be at level 1? No.

Add an element to a heap: 6 15 19 1 35 18 / 15 3 36 To add 3 to the above heap: 1. Maintain shape property first.. Then restore order property. Step 1: Shape property: Where must the new element go to keep the tree complete? Add 3 as next leaf (child of 1). Ignore that it might violate the order property. 6 15 19 1 35 18 5 3 36 3 Step : Order property: To where must we move the new element? "Heapify" 3 up, by swapping with its parent until the parent is less than or equal to it. 3 15 Swap 3 & 1 Swap 3 & 6 19 6 35 18 5 3 36 1 What is the worst-case runtime for add? Problem: How can I find where to put the new element? Problem: How do we find the parent of a child?

Remove minimum element from the heap: 6 15 19 1 35 18 / 5 3 36 Step 1: Maintain the shape property: What element should we use to replace the root we just removed? Take bottom rightmost element (36) and put it at the root. 36 6 15 19 1 35 18 5 3 Step : Restore the order property second. Where must we move the new root? "Heapify" new root down until it is less than its parent. Which node should be the new root? The smaller child. 6 1 15 Swap 36 & 6 Swap 36 & 1 19 36 35 18 5 3 What is the worst-case runtime for remove? Problem: How can I find the element to move to the root?

Suppose we number the nodes of the binary heap as follows. Do you see a relationship between a node and its children? A node and its parent? 1 3 4 5 6 7 8 9 10 11 1 13 14 15 For a node numbered i the left child is numbered *i the right child is numbered *i + 1 the parent is numbered i/. (integer division) Using this indexing we can store a tree in an array (starting at index 1). Exercise: What would be the index of the left and right child and parent of node numbered i, if the numbering started at 0 instead of 1. For example, for the binary heap above, after removing, the heap would be store in the array as follows: 5 6 7 8 9 6 1 15 19 36 35 18 5 3 Notice that the elements are placed in the array one level after the next. Where do I find the place to put the next leaf? Easy, put at the end of the array. Why didn't we use arrays to implement a binary tree? There would be gaps in the array whenever there was no corresponding node, and for unbalanced trees the gaps could be huge compared to the number of nodes. But binary heaps are complete trees and the array is filled fully. Example: Build binary heap with 9 1 8 5 15 Add 9 0 1 9 Add 1 0 1 9 1 9 / 1

Add 8 0 1 3 9 1 8 Swap 9 & 8 0 1 3 8 1 9 8 1 9 Add 5 8 1 9 5 Swap 1 & 5 8 5 9 1 Swap 8 & 5 5 8 9 1 5 8 9 / 1 Add 15 5 5 8 9 1 15 5 8 9 1 15 Add 5 6 5 8 9 1 15 Swap 9 & 5 6 5 8 1 15 9 Swap 5 & 5 6 8 5 1 15 9 8 5 / 1 15 9

Example: Repeatedly remove the minimum until empty. 8 5 / 1 15 9 Remove 5 6 8 5 1 15 9 Replace with 9 5 9 8 5 1 15 Swap 9 with 5 5 5 8 9 1 15 5 8 9 1 15 Remove 5 5 5 8 9 1 15 Replace 5 with 15 15 8 9 1 Swap 15 with 8 8 15 9 1 Swap 15 with 1 8 1 9 15 Swap 15 with 1 8 1 9 / 15 Remove 8 8 1 9 15 Replace 5 with 15 0 1 3 15 1 9 Swap 15 with 9 0 1 3 9 1 15 9 1 15 And so on.

What is the height of the binary heap? O(log n) ALWAYS Runtime: isempty: O(1) peekmin: add: best: expected: worst: O(1) O(1) sometime add a large element O(log n) O(log n) removemin: O(log n) always move a large element to the root

Heap Sort If remove all the values from a heap, what order will the values be removed? From smallest to largest. We just invented Heapsort Heap sort Runtime: 1. Build a heap: n* O(log n). Repeatedly remove the minimum: n * O(log n) always Total: O(n log n) - best, expected, and worst case What other sort has the same runtime? merge sort What is the disadvantage of merge sort? It is not an in- place sort. It needs an auxiliary array. BUT We can run heap sort in- place! Recall how we implement Insertion Sort: We used the front part of the array to hold the sorted data. After each iteration of the outer loop, we added one more element to the sorted part. We will use the same idea to build a heap. This time we will build a max- heap. The reason for a max- heap will become evident. 1. Build a max- heap of size 1, size, size 3,... adding each successive element in the array. The first part of the array is the heap and grows. The rest of the array is the source of more data to add.. Remove the maximum repeatedly from the heap. Each time we remove an element from the heap, the heap becomes one smaller, and we the use newly available space in the array to put the maximum just removed. That is, we remove the maximum from the heap and put in the last index, then remove next largest from the heap and put it in the second- to- last index, and so on. In particular, swap the last element in the heap with the first, and then heapify down the first element to restore the order property. The first part of the array is the max- heap and the rest of the array is the largest data elements in ascending order. Note that we use a max- heap, so that the maximum is at the end of the array.