Download presentation
Presentation is loading. Please wait.
Published byHeather McCoy Modified over 9 years ago
1
1 Heap Sort
2
A Heap is a Binary Tree Height of tree = longest path from root to leaf = (lgn) A heap is a binary tree satisfying the heap condition: At every node in a heap, the node value is >= all the values in its subtrees. A heap with heap_size elements can be represented as an array segment: A[1..heap_size]
3
3 Ordering of Nodes 16 1410 8 7 9 3 241 1 2 3 4 5 67 89 Each node is filled in order from left to right. All rows are filled except, possibly, the last row. Address of left child of node i => 2i Address of right child of node i => 2i+1 Address of parent of node i => Example: Left(4) = 8 Right(4) = 9 Parent(4) = 2
4
4 Heap properties 1.At every node in the heap, that node's value is greater than or equal to all the values in the subtrees of that node. 2.Heaps can be represented as an array. The elements in the array occur in the order of their heap addresses. A= 16 14 10 8 7 9 3 2 4 1 Note that the array is not sorted. Can guarantee that A[1] is the maximum value in the array.
5
5 Building a Heap _ To perform a heap sort, we must first build a heap out of our unsorted array. _To build a heap, we will use the function heapify, which creates a heap from a node i and two subtrees that are already heaps.
6
6 Heapify 16 314 15 6 9 11 782 1 i=2 3 4 5 67 89 10 Example: i = 2 Want to move the value, 3, to a position consistent with the heap property. In English: 1.Compare the value at node i with the values at each child node. Swap the value at node i with the largest of those values. 2.Repeat with the subtree that ith value was swapped into. Note: If the value at i is in the correct place, it will be swapped with itself and the algorithm is done.
7
7 Pseudocode for Heapify Heapify (A, i) lft Left(i) rt Right(i) if lft A[i] then largest lft else largest i if rt A[largest] then largest rt if largest != i then Swap( A, i, largest ); Heapify (A, largest);
8
8 Final Tree from example 16 1514 8 6 9 11 732 1 i=2 3 4 5 67 89 10 Can show that heapify runs in O(lgn) time. (Why?)
9
9 Building a Heap 1.Each leaf of a binary tree is already a heap of size 1. 2.Build-Heap will start with the highest addressed, non-leaf node and heapify it. It then repeats with each node addressed 1 less than the previous node. 3.In a binary tree, the leaf nodes have addresses: We want to heapify starting with node: Build-Heap(A) heap-size length[A] for i length[A] / 2 downto 1 do Heapify(A, i)
10
10 Example A= 4 7 16 3 27 9 10 34 1 25 4 716 3 27 9 10 34125 1 2 3 4 5 67 89 10 We will work this out in class.
11
11 Heap Sort In English: _ We know that the maximum value is at A[1] in a heap. _ We put that value at the end of the array, by swapping it with the last element of the heap. _ We then reduce the size of the heap by 1, so that the sorted element is no longer part of the heap. _ Heapify the new, smaller heap (A[1] is not necessarily in the correct position for a heap). This will put a new maximum at the top. _ Repeat this process until all the nodes have been sorted.
12
12 Pseudocode for Heapsort Heapsort(A) Build-Heap(A) for i length[A] downto 2 do Swap(A, 1, i) heap-size[A] heap-size[A] - 1 Heapify(A, 1) What is the running time of Heapsort? Must find running times of Build-Heap and Heapify first.
13
13 Recall Heapify Heapify( ) is a function that creates a heap from a node i and two subtrees that are already heaps. 16 314 15 6 9 11 782 1 i=2 3 4 5 67 89 10 Example: i = 2 Swap the value at node i with the largest of the values at child nodes. If value at node i is larger than those of children, stop. Otherwise, continue with the next level.
14
14 Running time of Heapify Maximum number of swaps in Heapify is the height of the heap. 1 3 4 5 67 89 10 2 Height of n element heap is: Therefore, the running time of heapify is O(lgn)
15
15 Running time of Build heap Recall that Build-Heap starts with the last element and repeatedly calls Heapify to create heaps from the bottom up. If the height of a node is given by the longest distance from a leaf to that node, then the number of nodes at a given height h is at most: Why? Build-Heap calls heapify for each node at a given height: number of nodes at height h cost of Heapify at height h T(n) = ?
16
16 Cost of Heap-Sort Heapsort(A) Build-Heap(A) for i length[A] downto 2 do Swap(A, 1, i) heap-size[A] heap-size[A] - 1 Heapify(A, 1) O(n) n n*const n*O(lgn) T(n) = O(n) + n*O(lgn) = O(nlgn)
17
17 Using a Heap in Priority Queues A priority queue is a queue in which the element with the highest value is retrieved first. WIPO: Whatever in, Priority out. Priority Queue Operations: Insert(S, x) Inserts element x into set S Maximum(S) Returns the element of S with the largest value (or key) Extract-Max(S) Removes and returns the element of S with the largest value. Heaps are useful data structures for priority queue functions.
18
18 Extracting the Maximum Idea: Extract the maximum element, reduce the heap size by 1, then heapify. Heap-Extract-Max(A) if heap_size[A] < 1 then error "heap underflow" max A[1]{ root value is max } A[1] A[heap_size[A]]{ put A[heap_size[A]] in root} heap_size[A] heap_size[A] - 1{ decrease heap size } Heapify(A, 1){ enforce heap property } return max
19
19 Example 16 12 8 3 2 1 3 4 5 We will work this through in class.
20
20 Heap-Insert Idea: To insert an element at the proper place in the heap, traverse the tree from leaf to root and find the correct place. HeapInsert(A, key) heap_size[A] heap_size[A] + 1{ increase size of heap } i heap-size[A] while i > 1 and A[Parent(i)] < key do A[i] A[Parent(i)] i Parent(i) A[i] key
21
21 Example 16 1410 8 7 9 3 241 1 3 4 5 67 89 Insert the number, 15. We will work through this in class.
22
22 Binary Heap: Definition _ Binary heap. _ Almost complete binary tree. –filled on all levels, except last, where filled from left to right _ Min-heap ordered. –every child greater than (or equal to) parent 06 14 78 18 81 7791 45 5347 64 84 99 83
23
23 Binary Heap: Properties _ Properties. _ Min element is in root. _ Heap with N elements has height = log 2 N . 06 14 78 18 81 7791 45 5347 64 84 99 83 N = 14 Height = 3
24
24 Binary Heaps: Array Implementation _ Implementing binary heaps. _ Use an array: no need for explicit parent or child pointers. –Parent(i) = i/2 –Left(i) = 2i –Right(i) = 2i + 1 06 14 78 18 81 7791 45 5347 64 84 99 83 1 2 3 4 56 7 8910 11 12 13 14
25
25 Binary Heap: Insertion _ Insert element x into heap. _ Insert into next available slot. _ Bubble up until it's heap ordered. –Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 64 84 99 83 42 next free slot
26
26 Binary Heap: Insertion _ Insert element x into heap. _ Insert into next available slot. _ Bubble up until it's heap ordered. –Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 5347 64 84 99 83 42 swap with parent
27
27 Binary Heap: Insertion _ Insert element x into heap. _ Insert into next available slot. _ Bubble up until it's heap ordered. –Peter principle: nodes rise to level of incompetence 06 14 78 18 81 7791 45 4247 64 84 99 83 42 53 swap with parent
28
28 Binary Heap: Insertion _ Insert element x into heap. _ Insert into next available slot. _ Bubble up until it's heap ordered. –Peter principle: nodes rise to level of incompetence _ O(log N) operations. 06 14 78 18 81 7791 42 4547 64 84 99 83 53 stop: heap ordered
29
29 Binary Heap: Decrease Key _ Decrease key of element x to k. _ Bubble up until it's heap ordered. _ O(log N) operations. 06 14 78 18 81 7791 42 4547 64 84 99 83 53
30
30 Binary Heap: Delete Min _ Delete minimum element from heap. _ Exchange root with rightmost leaf. _ Bubble root down until it's heap ordered. –power struggle principle: better subordinate is promoted 06 14 78 18 81 7791 42 4547 64 84 99 83 53
31
31 Binary Heap: Delete Min _ Delete minimum element from heap. _ Exchange root with rightmost leaf. _ Bubble root down until it's heap ordered. –power struggle principle: better subordinate is promoted 53 14 78 18 81 7791 42 4547 64 84 99 83 06
32
32 Binary Heap: Delete Min _ Delete minimum element from heap. _ Exchange root with rightmost leaf. _ Bubble root down until it's heap ordered. –power struggle principle: better subordinate is promoted 53 14 78 18 81 7791 42 4547 64 84 99 83 exchange with left child
33
33 Binary Heap: Delete Min _ Delete minimum element from heap. _ Exchange root with rightmost leaf. _ Bubble root down until it's heap ordered. –power struggle principle: better subordinate is promoted 14 53 78 18 81 7791 42 4547 64 84 99 83 exchange with right child
34
34 Binary Heap: Delete Min _ Delete minimum element from heap. _ Exchange root with rightmost leaf. _ Bubble root down until it's heap ordered. –power struggle principle: better subordinate is promoted _ O(log N) operations. 14 18 78 53 81 7791 42 4547 64 84 99 83 stop: heap ordered
35
35 Binary Heap: Heapsort _ Heapsort. _ Insert N items into binary heap. _ Perform N delete-min operations. _ O(N log N) sort. _ No extra storage.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.