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 Contents The ADT Heap An Array-Based Implementation of a Heap
A Heap Implementation of the ADT Priority Queue Heap Sort Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

3 The ADT Heap Definition A heap is a complete binary tree that either
is empty … or … it’s root Contains a value greater than or equal to the value in each of its children, and Has heaps as its subtrees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

4 Complete Binary Tree (1) A binary tree that is either full or full through the next-to-last level (2) The last level is full from left to right (i.e., leaves are as far to the left as possible) Complete Binary Tree

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

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

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

8 Unique?

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

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

11 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

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

13 The ADT Heap (a) A maxheap and (b) a minheap
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

14 View interface, Listing 17-1
The ADT Heap UML diagram for the class Heap View interface, Listing 17-1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

15 Array-Based Implementation of a Heap
(a) Level-by-level numbering of a complete binary tree; (b) its array-based implementation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

16 Algorithms for Array-Based Heap Operations
(a) Disjoint heaps after removing the heap’s root; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

17 Algorithms for Array-Based Heap Operations
(b) a semiheap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

18 Algorithms for Array-Based Heap Operations
(c) the restored heap View algorithm to make this conversion, Listing 17-A Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

19 Reheap Down bottom

20 Algorithms for Array-Based Heap Operations
The array representation the heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

21 Algorithms for Array-Based Heap Operations
The array representation of the semiheap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

22 Algorithms for Array-Based Heap Operations
The array representation of the restored heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

23 Algorithms for Array-Based Heap Operations
Recursive calls to heapRebuild Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

24 Algorithms for Array-Based Heap Operations
Insertion into a heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

25 Reheap Up bottom

26 Algorithms for Array-Based Heap Operations
Pseudocode for add Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

27 The Implementation The header file for class ArrayMaxHeap
An array-based implementation of the ADT heap View Listing 17-2. This heap is a maxheap. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

28 The Implementation Definition of constructor
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

29 The Implementation (a) The initial contents of an array; (b) the array’s corresponding complete binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

30 The Implementation Transforming an array into a heap
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

31 The Implementation Transforming an array into a heap
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

32 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

33 The Implementation Method heapCreate
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

34 The Implementation Method peekTop
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

35 Heap Implementation of the ADT Priority Queue
Using a heap to define a priority queue results in a more time-efficient implementation Listing 17-3 contains a header file for a class of priority queues. View implementation, Listing 17-4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

36 Heap Sort Heap sort partitions an array into two regions
View heap sort algorithm, Listing 17-B Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

37 Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

38 Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

39 Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

40 Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013


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

Similar presentations


Ads by Google