Unformatted text preview:

Lecture 9 Sorting II Heaps 6 006 Fall 2009 Lecture 9 Sorting II Heaps Lecture Overview Priority Queues Lecture 9 Sorting II Heaps Sorting II Heaps Lecture 9 Heaps 6 006 Fall 2009 6 006 Fall 2009 Lecture 9 9 Sorting Sorting II II Heaps Heaps Lecture Heapsort Lecture Overview Lecture Overview Readings CLRS 2 1 Priority Queues Priority Queues Heaps 2 2 2 3 6 1 6 2 Heaps Heapsort Heapsort 6 3 and 6 4 Priority Queues Readings Readings This is anCLRS abstract datatype implementing a set S of elements each associated with a key 2 1 2 2 2 3 6 1 6 2 6 3 and 6 4 2 1 2 2 2 3 6 1 6 2 6 3 and 6 4 Supports CLRS the following operations Priority Queues Priority Queues insert S x insert element x into set S This is an abstract datatype implementing a set S of elements each associated with a key This is an abstract datatype implementing a set S of elements each associated with a key max S return element of S with largest key Supports the following operations Supports the following operations extract max S return element of S with largest key and remove it from S insert S x insert element x into set S insert element x intoof set S increaseinsert S key S x x k increases the value x s key to new value k max S return element of S withelement largest key max S return element of S with largest key assumed to be ofasS large as current extract max S return element with largest key andvalue remove it from S Heaps extract max S increase key S x k increase key S x k return element of S with largest key and remove it from S increases the value of element x s key to new value k increases the value of element x s key to new value k assumed to be as large as current value assumed to be as large as current value Heaps An implementation of a priority queue It is an array object visualized as a nearly complete Heaps An implementation of a priority queue It is an array object visualized as a nearly complete binary tree An implementation of a priority queue It is an array object visualized as a nearly complete binary tree binary tree Heap Property The key of a node is than the keys of its children e g Figure 1 Heap Property The key of a node is than the key of its parent node e g Figure 1 Heap Property The key of a node is than the key of its parent node e g Figure 1 1 16 1 16 2 14 2 14 4 5 4 8 5 77 8 8 8 2 2 9 9 44 6 6 99 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 3 10 3 7 7 33 16 14 10 8 7 9 16 14 10 8 7 9 1 1 Figure 1 Binary Heap Figure 1 Binary Heap Figure 1 Binary Heap NOTE For convenience the first index in the array is 1 NOTE For convenience the first index in the array is 1 NOTE For convenience the first index in the array is 1 1 1 1 10 10 3 2 4 1 3 2 4 1 Lecture 9 Sorting II Heaps 6 006 Fall 2009 Visualizing an Array as a Tree root of tree first element in the array corresponding index 1 node with index i parent i b 2i c returns index of node s parent e g parent 5 2 left i 2i returns index of node s left child e g left 4 8 right i 2i 1 returns index of node s right child e g right 4 9 Note no pointers required Height of a binary heap O log2 n Heap Size Variable For flexibility we may only need to consider the first few elements of an array as part of the heap The variable heap size denotes the number of items of the array that are part of the heap A 1 A heap size Max Heaps vs Min Heaps Max Heaps satisfy the Max Heap Property for all i A i max A left i A right i If left i or right i is undefined replace A left i respectively A right i by In particular if node i has no children the property is trivially satisfied Everything we describe applies to the construction and operation of Min Heaps satisfying the Min Heap Property for all i A i min A left i A right i If left i or right i is undefined replace A left i respectively A right i by In particular if node i has no children the property is trivially satisfied Operations with Heaps build max heap produce a max heap from unordered input array in O n max heapify correct a single violation of the heap property at the root of a subtree in O log n heapsort sort an array of size n in O n log n via the use of heaps insert extract max O lg n Max Heapify A i Assume that the trees rooted at left i and right i are max heaps If element A i violates the max heap property correct violation by trickling element A i down the tree making the subtree rooted at index i a max heap See Figure 2 then read the pseudocode below l left i r right i 2 Lecture 9 Sorting II Heaps 6 006 Fall 2009 if l heap size A and A l A i then largest l else largest i if r heap size A and A r A largest then largest r if largest 6 i then exchange A i and A largest MAX HEAPIFY A largest Example Build Max Heap A A 1 n converted to a max heap Observation Elements A bn 2c 1 n are all leaves of the tree why 2i n for i bn 2c 1 Build Max Heap A heap size A length A O n times for i b length A 2c downto 1 O log n time do Max Heapify A i O n log n overall See Figure 3 for an example NOTE The trivial analysis of the algorithm noted above shows that the running time is O n log n Observe however that Max Heapify only takes O 1 time for the nodes that are one level above the leaves and in general O for the nodes that are levels above the leaves Using this observation it can be shown that the overall time for Build Max Heap A is O n 3 Lecture 9 Sorting II Heaps 6 006 Fall 2009 1 16 2 4 8 7 3 10 1 9 8 2 6 9 5 7 14 MAX HEAPIFY A 2 heap size A 10 10 3 4 1 16 10 3 2 14 4 8 7 3 10 1 9 8 2 …


View Full Document

MIT 6 006 - Lecture 9: Sorting II: Heaps

Documents in this Course
Quiz 1

Quiz 1

7 pages

Quiz 2

Quiz 2

12 pages

Quiz 2

Quiz 2

9 pages

Quiz 1

Quiz 1

10 pages

Quiz 2

Quiz 2

11 pages

Quiz 1

Quiz 1

12 pages

Graphs

Graphs

27 pages

Load more
Loading Unlocking...
Login

Join to view Lecture 9: Sorting II: Heaps and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 9: Sorting II: Heaps and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?