CS Data Structures Chapter 17 Heaps Mehmet H Gunes

Slides:



Advertisements
Similar presentations
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Advertisements

1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
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.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
C++ Plus Data Structures
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
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.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
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.
1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
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.
A Heap Implementation Chapter 26 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.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
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)
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
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.
Tables and Priority Queues
Heap Chapter 9 Objectives Define and implement heap structures
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Bohyung Han CSE, POSTECH
Chapter 16 Tree Implementations
Heap Chapter 9 Objectives Upon completion you will be able to:
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
Chapter 8 – Binary Search Tree
Chapter 10 Sorting and Searching Algorithms
Binary Tree Application Operations in Heaps
Tree Representation Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Computer Science 2 Heaps.
Chapter 16 Tree Implementations
C++ Plus Data Structures
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) A heap.
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.
Heaps and Priority Queues
Sorting Dr. Yingwu Zhu.
Heaps and Priority Queues
Chapter 10 Sorting Algorithms
Searching/Sorting/Searching
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Tables and Priority Queues
EE 312 Software Design and Implementation I
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CS 302 - Data Structures Chapter 17 Heaps Mehmet H Gunes Modified from authors’ slides

The ADT Heap A heap is a complete binary tree that either is Empty or … Whose root contains a value  each of its children and has heaps as its subtrees It is a special binary tree … different in that It is ordered in a weaker sense it will always be a complete binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: Its shape must be a complete binary tree. For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

Are these Both Heaps? T A C treePtr 50 20 18 30 10

Is this a Heap? tree 70 60 12 40 30 8 10

Unique?

Where is the Largest Element in a Heap Always Found? tree 70 60 12 40 30 8

We Can Number the Nodes Left to Right by Level This Way tree 70 60 1 12 2 40 3 30 4 8 5

And use the Numbers as Array Indexes to Store the Heap tree.nodes [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 70 60 1 40 3 30 4 12 2 8 5 tree

The ADT Heap (a) A maxheap and (b) a minheap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

With Array Representation For any node tree.nodes[index] its left child is in tree.nodes[index*2 + 1] right child is in tree.nodes[index*2 + 2] its parent is in tree.nodes[(index – 1)/2] Leaf nodes: tree.nodes[numElements/2] to tree.nodes[numElements - 1]

The ADT Heap UML diagram for the class Heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The ADT Heap An interface for the ADT heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The ADT Heap An interface for the ADT heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The ADT Heap An interface for the ADT heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Array-Based Implementation of a Heap A complete binary tree and its array-based implementation © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Algorithms for Array-Based Heap Operations Assume following private data members items: an array of heap items itemCount: an integer equal to the number of items in the heap maxItems: an integer equal to the maximum capacity of the heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Algorithms for Array-Based Heap Operations Disjoint heaps after removing the heap’s root © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Algorithms for Array-Based Heap Operations Recursive algorithm to transform semiheap to heap. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Algorithms for Array-Based Heap Operations Recursive algorithm to transform semiheap to heap. © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Algorithms for Array-Based Heap Operations © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Reheap Down bottom

Algorithms for Array-Based Heap Operations Recursive calls to heapRebuild © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Algorithms for Array-Based Heap Operations Adding 15 to a heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Reheap Up bottom

Algorithms for Array-Based Heap Operations Pseudocode for add © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation The header file for the class ArrayMaxHeap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation The header file for the class ArrayMaxHeap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation The header file for the class ArrayMaxHeap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Definition of method getLeftChildIndex © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Definition of the constructor © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Array and its corresponding complete binary tree © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Building a heap from an array of data © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation Transforming an array into a heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Building a Heap: i = 4 i = 3 i = 2 2 14 8 1 16 7 4 3 9 10 2 14 8 1 16 5 6 2 14 8 1 16 7 4 3 9 10 5 6 14 2 8 1 16 7 4 3 9 10 5 6 i = 1 i = 0 14 2 8 1 16 7 4 10 9 3 5 6 14 2 8 16 7 1 4 10 9 3 5 6 8 2 4 14 7 1 16 10 9 3 5 6

The Implementation C++ method heapCreate © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

The Implementation C++ method peekTop which tests for an empty heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Implementation of the ADT Priority Queue A header file for the class HeapPriorityQueue © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Implementation of the ADT Priority Queue An implementation of the class HeapPriorityQueue © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Implementation of the ADT Priority Queue An implementation of the class HeapPriorityQueue © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Implementation of the ADT Priority Queue Heap versus a binary search tree If you know maximum number of items in the priority queue, heap is the better implementation Finite, distinct priority values Many items likely have same priority value Place in same order as encountered © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Sort Heap sort partitions an array into two regions © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Sort: Recall that . . . A heap is a binary tree that satisfies these special SHAPE and ORDER properties: Its shape must be a complete binary tree. For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

The largest element in a heap is always found in the root node root 70 12 60 30 8 10 40

The heap can be stored in an array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 70 60 1 12 2 30 4 10 6 40 3 8 5

Heap Sort Approach First, make the unsorted array into a heap by satisfying the order property. Then repeat the steps below until there are no more unsorted elements. Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements. Reheap the remaining unsorted elements. This puts the next-largest element into the root position.

After creating the original heap values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 70 60 12 40 30 8 10 70 60 1 12 2 30 4 10 6 40 3 8 5

Swap root element into last place in unsorted array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 70 60 12 40 30 8 10 70 60 1 12 2 30 4 10 6 40 3 8 5

After swapping root element into it place values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 10 60 12 40 30 8 70 10 60 1 12 2 30 4 40 3 8 5 NO NEED TO CONSIDER AGAIN

After reheaping remaining unsorted elements values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 60 40 12 10 30 8 70 60 40 1 12 2 30 4 10 3 8 5

Swap root element into last place in unsorted array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 60 40 12 10 30 8 70 60 40 1 12 2 30 4 10 3 8 5

After swapping root element into its place values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 8 40 12 10 30 60 70 8 40 1 12 2 30 4 10 3 NO NEED TO CONSIDER AGAIN

After reheaping remaining unsorted elements values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 40 30 12 10 6 60 70 40 30 1 12 2 6 4 10 3

Swap root element into last place in unsorted array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 40 30 12 10 6 60 70 40 30 1 12 2 6 4 10 3

After swapping root element into its place values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 6 30 12 10 40 60 70 6 30 1 12 2 10 3 NO NEED TO CONSIDER AGAIN

After reheaping remaining unsorted elements values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 30 10 12 6 40 60 70 30 10 1 12 2 6 3

Swap root element into last place in unsorted array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 30 10 12 6 40 60 70 30 10 1 12 2 6 3

After swapping root element into its place values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 6 10 12 30 40 60 70 6 10 1 12 2 NO NEED TO CONSIDER AGAIN

After reheaping remaining unsorted elements values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 12 10 6 30 40 60 70 12 10 1 6 2

Swap root element into last place in unsorted array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 12 10 6 30 40 60 70 12 10 1 6 2

After swapping root element into its place values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 6 10 12 30 40 60 70 6 10 1 NO NEED TO CONSIDER AGAIN

After reheaping remaining unsorted elements values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 10 6 12 30 40 60 70 10 6 1

Swap root element into last place in unsorted array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 10 6 12 30 40 60 70 10 6 1

After swapping root element into its place values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 6 10 12 30 40 60 70 6 ALL ELEMENTS ARE SORTED

template <class ItemType > void HeapSort ( ItemType values[], int numValues ) // Post: Sorts array values[ 0 . . numValues-1 ] into // ascending order by key { int index ; // Convert array values[0..numValues-1] into a heap for (index = numValues/2 - 1; index >= 0; index--) ReheapDown ( values , index , numValues-1 ) ; // Sort the array. for (index = numValues-1; index >= 1; index--) Swap (values[0], values[index]); ReheapDown (values , 0 , index - 1); }

ReheapDown template< class ItemType > void ReheapDown ( ItemType values[], int root, int bottom ) // Pre: root is the index of a node that may violate the // heap order property // Post: Heap order property is restored between root and // bottom { int maxChild ; int rightChild ; int leftChild ; leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ;

if (leftChild == bottom) maxChild = leftChild; else if (leftChild <= bottom) // ReheapDown continued { if (leftChild == bottom) maxChild = leftChild; else if (values[leftChild] <= values [rightChild]) maxChild = rightChild ; maxChild = leftChild ; } if (values[ root ] < values[maxChild]) Swap (values[root], values[maxChild]); ReheapDown ( maxChild, bottom ) ;

Heap Sort: How many comparisons? In reheap down, an element is compared with its 2 children (and swapped with the larger). But only one element at each level makes this comparison, and a complete binary tree with N nodes has only O(log2N) levels. root 24 60 1 12 2 30 3 40 4 8 5 10 6 15 7 6 8 18 9 70 10

Heap Sort of N elements: How many comparisons? (N/2) * O(log N) compares to create original heap (N-1) * O(log N) compares for the sorting loop = O ( N * log N) compares total (reheap takes O(log N) )

Heap Sort A trace of heap sort, beginning with the heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Sort A trace of heap sort, beginning with the heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved

Heap Sort A trace of heap sort, beginning with the heap © 2017 Pearson Education, Hoboken, NJ.  All rights reserved