Heaps. Data Structures and Other Objects Using C++

Similar documents
Heaps. A heap is a certain kind of complete binary tree.

Lecture 18 Heaps and Priority Queues

Complete Binary Trees

CSC212 Data Structure - Section FG

Preconditions and Postconditions. What are Preconditions and Postconditions? Example. Example

Linked Lists in Action

Preconditions and Postconditions

Recursive Thinking. A Car Object. A Car Object. A Car Object. To start the example, think about your favorite family car

Pointers and Dynamic Arrays

CSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York

Chapter 12 discusses several ways of storing information in an array, and later searching for the. Hash tables are a common

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

Types of Containers: Sequences

Lecture 9 Linked Lists

Template Functions. CHAPTER 6 Data Structures and Other Objects

Can you think of a situation where this would occur?

CSC212. Data Structure. Lecture 4 Container Classes. Instructor: George Wolberg Department of Computer Science City College of New York

Lectures 4 & 5 Container Classes

Object Oriented Programming

CSC212. Data Structure. Lecture 9 Templates, Iterators and STL. Instructor: George Wolberg Department of Computer Science City College of New York

Cosc 241 Programming and Problem Solving Lecture 21 (15/5/2017) Heaps

Priority Queues and Binary Heaps

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

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

CSCI2100B Data Structures Heaps

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based 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

Heaps. 2/13/2006 Heaps 1

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

CS350: Data Structures Heaps and Priority Queues

Binary Heaps. CSE 373 Data Structures Lecture 11

Binary Heaps in Dynamic Arrays

Heaps Goodrich, Tamassia. Heaps 1

Programming II (CS300)

Section 4 SOLUTION: AVL Trees & B-Trees

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

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

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

Binary heaps (chapters ) Leftist heaps

Heap sort. Carlos Moreno uwaterloo.ca EIT

Chapter 6 Heapsort 1

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

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

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

13.4 Deletion in red-black trees

Lecture Notes on Priority Queues

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

CS165: Priority Queues, Heaps

Stores a collection of elements each with an associated key value

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

13.4 Deletion in red-black trees

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

Lecture Notes for Advanced Algorithms

Sorting and Searching

Heapsort. Heap data structure

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

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

CH 8. HEAPS AND PRIORITY QUEUES

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

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

CH. 8 PRIORITY QUEUES AND HEAPS

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

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?

Sorting and Searching

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

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

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Topic: Heaps and priority queues

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

Programming Abstractions

9. Heap : Priority Queue

Properties of a heap (represented by an array A)

Heap Model. specialized queue required heap (priority queue) provides at least

Programming II (CS300)

CS 241 Analysis of Algorithms

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

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

CSCI Trees. Mark Redekopp David Kempe

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

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

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

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

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Heaps and Priority Queues

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

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Recall: Properties of B-Trees

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

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

Heap sort. Carlos Moreno uwaterloo.ca EIT

Transcription:

Heaps 1 Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two of the important heap algorithms. Data Structures and Other Objects Using C++ This lecture introduces heaps, which are used in the Priority Queue project of Chapter 11. The lecture includes the algorithms for adding to a heap (including reheapification upward), removing the top of a heap (including reheapification downward), and implementing a heap in a partially-filled array. Prior to this lecture, the students need a good understanding of complete binary trees. It would also help if they have seen binary search trees and the priority queue class.

Heaps 2 A heap is a certain kind of complete binary tree. A heap is a data structure with several applications, including a way to implement Priority Queues, as shown in Chapter 11. The definition of a heap is a special kind of complete binary tree. You probably recall that a complete binary tree requires that its nodes are added in a particular order...

Heaps A heap is a certain kind of complete binary tree. Root 3 When a complete binary tree is built, its first node must be the root. The first node of a complete binary tree is always the root...

Heaps 4 Complete binary tree. Left child of the root The second node is always the left child of the root....the second node is always the left child of the root...

Heaps 5 Complete binary tree. Right child of the root The third node is always the right child of the root....then the right child of the root...

Heaps 6 Complete binary tree. The next nodes always fill the next level from left-to-right....and so on. The nodes always fill each level from left-to-right...

Heaps 7 Complete binary tree. The next nodes always fill the next level from left-to-right....from left-to-right...

Heaps 8 Complete binary tree. The next nodes always fill the next level from left-to-right....from left-to-right...

Heaps 9 Complete binary tree. The next nodes always fill the next level from left-to-right....from left-to-right...

Heaps 10 Complete binary tree....and when a level is filled you start the next level at the left.

Heaps 11 45 A heap is a certain kind of complete binary tree. 22 4 19 Each node in a heap contains a key that can be compared to other nodes' keys. So, a heap is a complete binary tree. Each node in a heap contains a key, and these keys must be organized in a particular manner. Notice that this is not a binary search tree, but the keys do follow some semblance of order. Can you see what rule is being enforced here?

Heaps 12 45 A heap is a certain kind of complete binary tree. 22 4 19 The "heap property" requires that each node's key is >= the keys of its children The heap property requires that each node's key is >= to the keys of its children. This is a handy property because the biggest node is always at the top. Because of this, a heap can easily implement a priority queue (where we need quick access to the highest priority item).

Adding a Node to a Heap 13 45 ❶ Put the new node in the next available spot. ❷ Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 AAAAAAAA A AAAAAA AAAAAA AAAA42 AAAAAA A A 22 4 We can add new elements to a heap whenever we like. Because the heap is a complete binary search tree, we must add the new element at the next available location, filling in the levels from left-to-right. In this example, I have just added the new element with a key of 42. Of course, we now have a problem: The heap property is no longer valid. The 42 is bigger than its parent. To fix the problem, we will push the new node upwards until it reaches an acceptable location.

Adding a Node to a Heap 14 45 ❶ Put the new node in the next available spot. ❷ Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 AAAA AAAA AAAA 42 AAAAA 22 4 Here we have pushed the 42 upward one level, swapping it with its smaller parent. We can't stop here though, because the parent is still smaller than the new node 42.

Adding a Node to a Heap 15 45 ❶ Put the new node in the next available spot. ❷ Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 AAAA 42 AAAAA AAAAAAA 22 4 Can we stop now? Yes, because the 42 is less than or equal to its parent.

Adding a Node to a Heap 16 45 The parent has a key that is >= new node, or The node reaches the root. The process of pushing the new node upward is called reheapification upward. 19 42 22 4 In general, there are two conditions that can stop the pushing upward: 1. We reach a spot where the parent is >= the new node, or 2. We reach the root. This process is called reheapification upward (I didn't just make up that name, really).

Removing the Top of a Heap 17 45 ❶ Move the last node onto the root. 42 22 4 19 We can also remove the top node from a heap. The first step of the removal is to move the last node of the tree onto the root. In this example we move the onto the root.

Removing the Top of a Heap ❶ Move the last node onto the root. 42 AAAA AAAAA 18 22 4 19 Now the is on top of the heap, and the original root (45) is no longer around. But the heap property is once again violated.

Removing the Top of a Heap 19 ❶ Move the last node onto the root. ❷ Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 42 AAAA AAAAA 22 4 We'll fix the problem by pushing the out-of-place node downward. Perhaps you can guess what the downward pushing is called...reheapification downward.

Removing the Top of a Heap 20 42 ❶ Move the last node onto the root. ❷ Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 AAAA AAAAA AAAAAAA 22 4 When we push a node downward it is important to swap it with its largest child. (Otherwise we are creating extra problems by placing the smaller child on top of the larger child.) This is what the tree looks like after one swap. Should I continue with the reheapification downward?

Removing the Top of a Heap 42 ❶ Move the last node onto the root. ❷ Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 AAAA AAAA AAAA AAAAA 22 4 Yes, I swap again, and now the is in an acceptable location.

Removing the Top of a Heap 22 42 The children all have keys <= the out-of-place node, or The node reaches the leaf. The process of pushing the new node 19 downward is called reheapification downward. 22 4 Reheapification downward can stop under two circumstances: 1. The children all have keys that are <= the out-of-place node. 2. The out-of-place node reaches a leaf.

Implementing a Heap 42 We will store the data from the nodes in a partially-filled array. An array of data This slide shows the typical way that a heap is implemented. For the most part, there is nothing new here, because you already know how to implement a complete binary tree using a partially-filled array. That is what we are doing with the heap.

Implementing a Heap 24 42 Data from the root goes in the first location of the array. 42 An array of data Following the usual technique for implementing a complete binary tree, the data from the root is stored in the first entry of the array.

Implementing a Heap 25 42 Data from the next row goes in the next two array locations. 42 An array of data The next two nodes go in the next two locations of the array.

Implementing a Heap 26 42 Data from the next row goes in the next two array locations. 42 An array of data and so on.

Implementing a Heap 42 Data from the next row goes in the next two array locations. 42 An array of data We don't care what's in this part of the array. As with any partially-filled array, we are only concerned with the front part of the array. If the tree has five nodes, then we are only concerned with the entries in the first five components of the array.

Important Points about the Implementation 42 28 The links between the tree's nodes are not actually stored as pointers, or in any other way. The only way we "know" that "the array is a tree" is from the way we manipulate the data. 42 An array of data With this implementation of a heap, there are no pointers. The only way that we know that the array is a heap is the manner in which we manipulate it.

Important Points about the Implementation 42 29 If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. Formulas are given in the book. 42 [1] [2] [3] [4] [5] The manipulations are the same manipulations that you've used for a complete binary tree, making it easy to compute the index where various nodes are stored.

Summary 30 A heap is a complete binary tree, where the entry at each node is greater than or equal to the entries in its children. To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward. To remove the biggest entry, move the last node onto the root, and perform a reheapification downward. A quick summary...

31 Presentation copyright 1997 Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. THE END Feel free to send your ideas to: Michael Main main@colorado.edu