Download presentation
Presentation is loading. Please wait.
Published byΧρυσάνθη Αλιβιζάτος Modified over 6 years ago
1
Data Structures Heaps CIS265/506: Chapter 12 Heaps
2
Heap Sort Heap sort is an important O(NlogN) algorithm for internal sorting. It is an unusual method in that no space penalty is exacted for good performance. This method uses the concept of a heap, which is a special case of a binary tree. A heap can be thought of as a binary tree where (a) the root of every subtree is greater than either child (b) the heap is left-balanced: that is, as the heap grows, the nodes are added left-to-right CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 6
3
Heap Sort 52 52 29 40 40 29 17 21 20 Some Heaps CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 7
4
Heaps Although heaps can be built using a dynamic data structure such as a tree, they are usually built using arrays. There are no intrinsic heap routines in Java – you must write them yourself. Just as we had almost complete binary trees, we also have almost heaps. An almost heap is one where one or both of the children are greater than the root in any subtree. CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 8
5
Heap Sort Some Almost Heaps 52 52 29 40 54 29 31 33 20
CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 9
6
An Example We wish to convert an unsorted tree to an ascending (or min) heap, where the smallest element is the root of the heap. Consider an array of integers: 26, 5, 77, 1, 61, 11, 59, 15, 48, 19 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 10
7
Place these items in a tree:
1 26 3 2 77 5 6 4 5 7 1 61 11 59 8 9 10 15 48 19 Exchange the root in every subtree with the larger of the two children in that subtree. CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 11
8
The tree after we make the first round of adjustments
1 77 3 2 59 61 6 4 5 7 48 19 11 26 8 9 10 15 1 5 The root of the tree will now contain the largest element in the array. Swap this element with the element in position 10, & repeat step one, except do not use node 10. CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 12
9
The tree before we make the second round of adjustments
1 5 3 2 59 61 6 4 5 7 48 19 11 26 8 9 10 15 1 77 Exchange the root of every subtree with the larger of the two children in that subtree. CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 13
10
The tree after we make the second round of adjustments
1 1 3 2 59 48 6 4 5 7 15 19 11 26 8 9 10 5 61 77 Continue in this fashion - reducing the size of the problem by one every time the algorithm is run. CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
11
The tree after we make the third round of adjustments
1 5 3 2 1 48 6 4 5 7 15 19 11 26 8 9 10 59 61 77 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
12
The tree after we make the fourth round of adjustments
1 1 3 2 26 19 6 4 5 7 15 5 11 48 8 9 10 59 61 77 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
13
The tree after we make the fifth round of adjustments
1 1 3 2 11 19 6 4 5 7 15 5 26 48 8 9 10 59 61 77 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
14
The tree after we make the sixth round of adjustments
1 5 3 2 11 15 6 4 5 7 1 19 26 48 8 9 10 59 61 77 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
15
The tree after we make the seventh round of adjustments
1 1 3 2 11 5 6 4 5 7 19 15 26 48 8 9 10 59 61 77 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
16
The tree after we make the eighth round of adjustments
1 1 3 2 11 5 6 4 5 7 19 15 26 48 8 9 10 59 61 77 CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 14
17
Our array is now sorted - read from left to right, top to bottom
1 1 3 2 11 5 6 4 5 7 15 19 26 48 8 9 10 59 61 77 Our array is now sorted - read from left to right, top to bottom CIS265/506: Chapter 12 Heaps CIS265/506: Chapter 12 Heaps 15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.