Heapsort Lecture in class

Heapsort Lecture in class - Design & Analysis of Algorithms...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Design & Analysis of Algorithms Heapsort (Courtesy to George Bebis) University of Nevada, Reno Department of Computer Science & Engineering (Chapter 6, Appendix B.5) Picture 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Special Types of Trees Def: Full binary tree = a binary tree in which each node is either a leaf or has degree exactly 2. Complete binary tree = a binary tree in which all leaves are on the same level and all internal nodes have degree 2. Full binary tree 2 14 8 1 16 7 4 3 9 10 12 Complete binary tree 2 1 16 4 3 9 10
Background image of page 2
3 Definitions Height of a node = the number of edges on the longest simple path from the node down to a leaf Level of a node = the length of a path from the root to the node Height of tree = height of root node 2 14 8 1 16 4 3 9 10 Height of root = 3 Height of (2)= 1 Level of (10)= 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Useful Properties 2 14 8 1 16 4 3 9 10 Height of root = 3 Height of (2)= 1 Level of (10)= 2 Picture 24 height height 1 1 0 2 1 2 2 1 2 1 d d l d l n + + = - = = - - (see Ex 6.1-2, page 129)
Background image of page 4
5 The Heap Data Structure Def: A heap is a nearly complete binary tree with the following two properties: Structural property: all levels are full, except possibly the last one, which is filled from left to right Order (heap) property: for any node x Parent(x) x Heap 5 7 8 4 2 From the heap property, it follows that: “The root is the maximum element of the heap!” A heap is a binary tree that is filled in order
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Array Representation of Heaps A heap can be stored as an array A . Root of tree is A[1] Left child of A[i] = A[2i] Right child of A[i] = A[2i + 1] Parent of A[i] = A[ i/2 ] Heapsize[A] ≤ length[A] The elements in the subarray A[( n/2 +1) . . n] are leaves
Background image of page 6
7 Heap Types Max-heaps (largest element at root), have the max-heap property: for all nodes i , excluding the root: A[PARENT(i)] A[i] Min-heaps (smallest element at root), have the min-heap property: for all nodes i , excluding the root:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Adding/Deleting Nodes New nodes are always inserted at the bottom level (left to right) Nodes are removed from the bottom level (right to left) Picture 4
Background image of page 8
9 Operations on Heaps Maintain/Restore the max-heap property MAX-HEAPIFY Create a max-heap from an unordered array BUILD-MAX-HEAP Sort an array in place HEAPSORT Priority queues
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Maintaining the Heap Property Suppose a node is smaller than a child Left and Right subtrees of i are max-heaps To eliminate the violation: Exchange with larger child Move down the tree Continue until node is not smaller than children
Background image of page 10
11 Example MAX-HEAPIFY(A, 2, 10) A[2] violates the heap property A[2] A[4] A[4] violates the heap property A[4] A[9] Heap property restored
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/20/2011 for the course CIS 6930 taught by Professor Staff during the Fall '08 term at University of Florida.

Page1 / 43

Heapsort Lecture in class - Design & Analysis of Algorithms...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online