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

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
1 Heaps and Priority Queues Heap Definition and Operations Using Heaps –Heap sort –Priority queues Implementing heaps –With links –With arrays –Analysis.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
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.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Heaps & Priority Queues
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
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.
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Source: Muangsin / Weiss
Chapter 21 Heaps and Priority Queues
ADT Heap data structure
Heapsort Heap & Priority Queue.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
CHAPTER 11: Priority Queues and Heaps
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Chapter 21 Heaps and Priority Queues
Presentation transcript:

Chapter 15 Heaps

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate how a heap can be used to solve problems Examine various heap impmentations Compare heap implementations

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Heaps A heap is a binary tree with two added properties: – It is a complete tree – For each node, the node is less than or equal to both the left child and the right child This definition described a minheap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Heaps In addition to the operations inherited from a binary tree, a heap has the following additional operations: – addElement – removeMin – findMin

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.1 The operations on a heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.1

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.2 UML description of the HeapADT

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The addElement Operation The addElement method adds a given element to the appropriate location in the heap A binary tree is considered complete if all of the leaves are level h or h-1 where h = log 2 n and n is the number of elements in the tree

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The addElement Operation Since a heap is a complete tree, there is only one correct location for the insertion of a new node – Either the next open position from the left at level h – Or the first position in level h+1 if level h is full

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The addElement Operation Once we have located the new node in the proper position, then we must account for the ordering property We simply compare the new node to its parent value and swap the values if necessary We continue this process up the tree until either the new value is greater than its parent or the new value becomes the root of the heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.3 Two minheaps containing the same data

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.4 Insertion points for a heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.5 Insertion and reordering in a heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The removeMin Operation The removeMin method removes the minimum element from the heap The minimum element is always stored at the root Thus we have to return the root element and replace it with another element

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The removeMin Operation The replacement element is always the last leaf The last leaf is always the last element at level h

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.6 Examples of the last leaf in a heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved The removeMin Operation Once the element stored in the last leaf has been moved to the root, the heap will have to reordered This is accomplished by comparing the new root element to the smaller of its children and the swapping them if necessary This process is repeated down the tree until the element is either in a leaf or is less than both of its children

Copyright © 2005 Pearson Addison-Wesley. All rights reserved FIGURE 15.7 Removal and reordering in a heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Heap Sort Given the ordering property of a heap, it is natural to think of using a heap to sort a list of objects One approach would be to simply add all of the objects to a heap and then remove them one at a time in ascending order

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Heap Sort Insertion into a heap is O(log n) for any given node and thus would O(n log n) for n nodes to build the heap However, it is also possible to build a heap in place using an array Since we know the relative position of each parent and its children in the array, we simply start with the first non-leaf node in the array, compare it to its children and swap if necessary

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Heap Sort However, it is also possible to build a heap in place using an array Since we know the relative position of each parent and its children in the array, we simply start with the first non-leaf node in the array, compare it to its children and swap if necessary

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Heap Sort We then work backward in the array until we reach the root Since at most, this will require us to make two comparisons for each non-leaf node, this approach is O(n) to build the heap We will revisit the analysis of HeapSort at the end of the chapter

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Priority Queue A priority queue is a collection that follows two ordering rules: – Items which have higher priority go first – Items with the same priority use a first in, first out method to determine their ordering A priority queue could be implemented using a list of queues where each queue represents items of a given priority

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Priority Queue Another solution is to use a minheap Sorting the heap by priority accomplishes the first ordering However, the first in, first out ordering for items with the same priority has to be manipulated

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Using Heaps: Priority Queue The solution is to create a PriorityQueueNode object that stores the element to be placed on the queue, the priority of the element and the arrival order of the element Then we simply define a compareTo method for the PriorityQueueNode class that first compares priority then arrival time The PriorityQueue class then extends the Heap class and stores PriorityQueueNodes

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.2

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.2 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.3

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.3 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.3 (cont.)

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Implementing Heaps with Links A linked implementation of a minheap would simply be an extension of our LinkedBinaryTree class However, since we need each node to have a parent pointer, we will create a HeapNode class to extend our BinaryTreeNode class we used earlier

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Listing 15.4

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Implementing Heaps with Links The addElement method must accomplish three tasks: – Add the new node at the appropriate location – Reorder the heap – Reset the lastNode pointer to point to the new last node

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the addElement Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the addElement Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the addElement Operation The addElement operation makes use of two private methods: – getNextParentAdd that returns a reference to the node that will be the parent of the new node – heapifyAdd that reorders the heap after the insertion

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the addElement Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the addElement Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation The removeMin operation must accomplish three tasks: – Replace the element stored in the root with the element stored in the last leaf – Reorder the heap if necessary – Return the original root element

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation Like the addElement operation, the removeMin operation makes use of two private methods – getNewLastNode that returns a reference to the new last node in the heap – heapifyRemove that reorders the heap after the removal

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved LinkedHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Implementing Heaps with Arrays An array implementation of a heap may provide a simpler alternative In an array implementation, the location of parent and child can always be calculated Given that the root is in postion 0, then for any given node stored in position n of the array, its left child is in position 2n + 1 and its right child is in position 2(n+1) This means that its parent is in position (n- 1)/2

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Implementing Heaps with Arrays Like the linked version, the addElement operation for an array implementation of a heap must accomplish three tasks: – Add the new node, – Reorder the heap, – Increment the count by one The ArrayHeap version of this method only requires one private method, heapifyAdd which reorders the heap after the insertion

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayHeap - the addElement Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayHeap - the addElement Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayHeap - the removeMin Operation The removeMin operation must accomplish three tasks: – Replace the element stored at the root with the element stored in the last leaf – Reorder the heap as necessary – Return the original root element Like the addElement operation, the removeMin operation makes use of a private method, heapifyRemove to reorder the heap

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved ArrayHeap - the removeMin Operation

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Analysis of Heap Implementations The addElement operation is O(log n) for both implementations The removeMin operation is O(log n) for both implementations The findMin operation is O(1) for both implementations

Copyright © 2005 Pearson Addison-Wesley. All rights reserved Analysis of Heap Implementations One might conclude then that HeapSort is O(log n) since both adding and removing elements is O(log n) Keep in mind that for HeapSort, we must perform both operations for each of n elements Thus the time complexity of HeapSort is 2*n*log n or O(n log n)