Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS Data Structures Chapter 17 Heaps Mehmet H Gunes

Similar presentations


Presentation on theme: "CS Data Structures Chapter 17 Heaps Mehmet H Gunes"— Presentation transcript:

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

2 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

3 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.

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

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

6 Unique?

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

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

9 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

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

11 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]

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

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

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

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

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

17 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

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

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

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

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

22 Reheap Down bottom

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

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

25 Reheap Up bottom

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

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

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

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

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

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

32

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

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

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

36 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

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

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

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

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

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

42 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

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

44 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.

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

46 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

47 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.

48 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

49 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

50 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

51 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

52 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

53 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

54 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

55 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

56 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

57 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

58 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

59 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

60 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

61 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

62 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

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

64 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

65 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

66 template <class ItemType >
void HeapSort ( ItemType values[], int numValues ) // Post: Sorts array values[ 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); }

67 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 * ; rightChild = root * ;

68 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 ) ;

69 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

70 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) )

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

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

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


Download ppt "CS Data Structures Chapter 17 Heaps Mehmet H Gunes"

Similar presentations


Ads by Google