Copyright ©2012 by Pearson Education, Inc. All rights reserved

Slides:



Advertisements
Similar presentations
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Advertisements

An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
COMP5712 Tutorial 4. 2 Using an Array to Represent a Heap When a binary tree is complete – Can use level-order traversal to store data in consecutive.
A Heap Implementation Chapter Chapter Contents Reprise: The ADT Heap Using an Array to Represent a Heap Adding an Entry Removing the Root Creating.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Transforming Infix to Postfix
Heaps. 2 A complete binary tree Nodes contain Comparable objects Each node contains no smaller (or no larger) than objects in its descendants Maxheap.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Heapsort Based off slides by: David Matuszek
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Heap Chapter 9 Objectives Define and implement heap structures
Trees Chapter 15.
Priority Queues and Heaps
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
An Introduction to Sorting
Heapsort.
Chapter 16 Tree Implementations
Heap Chapter 9 Objectives Upon completion you will be able to:
Binary Heaps Text Binary Heap Building a Binary Heap
Dr. David Matuszek Heapsort Dr. David Matuszek
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Binary Tree Application Operations in Heaps
Priority Queues.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Computer Science 2 Heaps.
Chapter 16 Tree Implementations
List Implementations that Use Arrays
A Robust Data Structure
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Heapsort.
Ch. 12 Tables and Priority Queues
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
Priority Queues.
Priority Queues.
Heapsort.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Heapsort.
A Binary Search Tree Implementation
Heapsort.
CO 303 Algorithm Analysis and Design
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
EE 312 Software Design and Implementation I
List Implementations that Use Arrays
Presentation transcript:

Copyright ©2012 by Pearson Education, Inc. All rights reserved A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Reprise: The ADT Heap Using an Array to Represent a Heap Adding an Entry Removing the Root Creating a Heap Heap Sort Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Use an array to represent a heap Add an entry to an array-based heap Remove the root of an array-based heap Create a heap from given entries Sort an array by using a heap sort Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Reprise: The ADT Heap A heap: Complete binary tree Nodes contain Comparable objects A maxheap: Object in each node greater than or equal to the objects in node’s descendants Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Reprise: The ADT Heap Interface considered in Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array to Represent a Heap Begin by using array to represent complete binary tree. Complete tree is full to its next-to-last level Leaves on last level are filled from left to right Locate either children or parent of any node by performing simple computation on node’s number Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-1 (a) A complete binary tree with its nodes numbered in level order; (b) its representation as an array Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 1 If an array contains the entries of a heap in level order beginning at index 0, what array entries represent a node’s parent, left child, and right child? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 1 If an array contains the entries of a heap in level order beginning at index 0, what array entries represent a node’s parent, left child, and right child? The node at array index i • Has a parent at index (i - 1)/2, unless the node is the root (i is 0). • Has any children at indices 2i + 1 and 2i + 2. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Array to Represent a Heap View code of class MaxHeap, Listing 26-1 Data fields: Array of Comparable heap entries Index of last entry in the array A constant for default initial capacity of heap Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-2 The steps in adding 85 to the maxheap in Figure 26-1a Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-2 The steps in adding 85 to the maxheap in Figure 26-1a Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-2 The steps in adding 85 to the maxheap in Figure 26-1a Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 2 What steps are necessary to add 100 to the heap in Figure 26-2c? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 2 What steps are necessary to add 100 to the heap in Figure 26-2c? Place 100 as a right child of 80. Then swap 100 with 80, swap 100 with 85, and finally swap 100 with 90. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-3 A revision of the steps shown in Figure 26-2, to avoid swaps Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-3 A revision of the steps shown in Figure 26-2, to avoid swaps Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-4 An array representation of the steps in Figure 26-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-4 An array representation of the steps in Figure 26-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-5 The steps to remove the entry in the root of the maxheap in Figure 26-3d Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-5 The steps to remove the entry in the root of the maxheap in Figure 26-3d Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 6 What steps are necessary to remove the root from the heap in Figure 26-5d? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 6 What steps are necessary to remove the root from the heap in Figure 26-5d? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-6 The steps that transform a semiheap into a heap without swaps Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-6 The steps that transform a semiheap into a heap without swaps Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-7 The steps in adding 20, 40, 30, 10, 90, and 70 to an initially empty heap Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-7 The steps in adding 20, 40, 30, 10, 90, and 70 to an initially empty heap Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-7 The steps in adding 20, 40, 30, 10, 90, and 70 to an initially empty heap Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 8 If an array represents a heap, and lastIndex is the index of the heap’s last leaf, show why the index of the first nonleaf closest to the end of the array is lastIndex/2 . Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 8 If an array represents a heap, and lastIndex is the index of the heap’s last leaf, show why the index of the first nonleaf closest to the end of the array is lastIndex/2 . If a node at index i has children, they are at indices 2 i and 2 i + 1. The node at lastIndex/2 then has a child at lastIndex. Since this child is the last leaf, any nodes beyond the one at lastIndex/2 cannot have children and so must be leaves. Thus, the node at lastIndex/2 must be the nonleaf closest to the end of the array. Alternatively, examine some complete trees and notice that the desired nonleaf is the parent of the last child. This child is at index lastIndex of the array representation, so its parent has index lastIndex/2 . Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-8 the steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-8 the steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 26-8 the steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap Complexity Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-9 A trace of heap sort Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-9 A trace of heap sort Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 26-9 A trace of heap sort Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Efficiency   Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 9Trace the steps that the method heapSort takes when sorting the following array into ascending order: 9 6 2 4 8 7 5 3. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 9Trace the steps that the method heapSort takes when sorting the following array into ascending order: 9 6 2 4 8 7 5 3. 9 6 2 4 8 7 5 3 Original array 9 8 7 4 6 2 5 3 After repeated calls to reheap 3 8 7 4 6 2 5 9 After swap 8 6 7 4 3 2 5 9 After reheap 5 6 7 4 3 2 8 9 After swap 7 6 5 4 3 2 8 9 After reheap 2 6 5 4 3 7 8 9 After swap 6 4 5 2 3 7 8 9 After reheap 3 4 5 2 6 7 8 9 After swap 5 4 3 2 6 7 8 9 After reheap 2 4 3 5 6 7 8 9 After swap 4 2 3 5 6 7 8 9 After reheap 3 2 4 5 6 7 8 9 After swap 3 2 4 5 6 7 8 9 After reheap 2 3 4 5 6 7 8 9 After swap 2 3 4 5 6 7 8 9 Done Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved