08 - CIS 121 Spring 2010 Data Structures and Introduction...

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

View Full Document Right Arrow Icon
1 2/15/10 Lecture Notes 8 CIS 121 Spring 2010 Data Structures and Introduction to Algorithms using JAVA Lecture Notes 8: Heaps, binary search trees © 1999-2010 S.Kannan, V.Tannen, S.Guha & K.Daniilidis © 2006 M.Goodrich & R.Tamassia (with permission) 2/15/10 Lecture Notes 8 2 Sections 6.8, 19.1, 19.3 , 21.1, 21.2, 21.5 Recommended reading:
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 2/15/10 Lecture Notes 8 3 Binary Heaps: a kind of trees An implementation of priority queues with trees that satisfy: In order to have about n leaves in a tree of height log n we will want to keep all the leaves at roughly the same height . Actually, we will have a tree which is completely filled except possibly the bottom level which is filled from left to right. (Some books call this a complete binary tree, but we prefer to use this name more specifically, see earlier.) To be able to find the minimum quickly we will keep the minimum element at the root . More generally, for any node x and its parent p the value at p is less than or equal to the value at x . In some books this is called a min-heap . 2/15/10 Lecture Notes 8 4 Illustration of Binary Heaps 2 7 4 8 20 11 5 13 11 25 22 21 It is clear that getMin can be performed in O(1) time if we maintain the heap-order property. Question: How can we maintain heap-order property and heap-structure property when doing add and removeMin ?
Background image of page 2
3 2/15/10 Lecture Notes 8 5 add 2 7 4 8 20 11 5 13 11 25 22 21 Suppose we want to add the value ‘ 3 ’. We start by putting it in the right place to maintain the heap-structure property. 3 Now the heap-order property is violated. We will need to percolate the value ‘ 3 up the tree to its proper place (also called up-heap bubbling ). 2/15/10 Lecture Notes 8 6 2 7 4 8 20 5 13 11 25 22 21 11
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 2/15/10 Lecture Notes 8 7 2 7 3 8 20 5 13 11 25 22 21 11 Notice that we had to follow links to parents of nodes in order to do the percolation up in the tree. Also, in the code we need to take care of the special case where the percolation reaches the root, since the root has no parent. 2/15/10 Lecture Notes 8 8 removeMin Suppose we want to delete the minimum ( ‘ 2 ’ ) in the heap above. One idea: keep trying to percolate up the smaller child ( ‘ 4 ’, etc.). This won’t work. Why? Better idea: Replace the root with the last element ‘ 13 ’. Percolate it down (also called down-heap bubbling ) to its proper place. 2 7 4 8 20 11 5 13 11 25 22 21
Background image of page 4
5 2/15/10 Lecture Notes 8 9 13 7 4 8 20 11 5 11 25 22 21 2/15/10 Lecture Notes 8 10 4 7 13 8 20 5 11 25 22 21 11
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 2/15/10 Lecture Notes 8 11 4 7 5 8 20 13 11 25 22 21 11 When doing percolate down which direction (left or right child) should we go? What links do we need to follow? 2/15/10 Lecture Notes 8 12 Analysis How long do the operations take? The method add moves up the heap repeatedly finding parents. Each step does O(1) work. In the worst case, this is done from leaf to root. Thus the total work done is O(height of heap). But the height of a heap with n nodes is O(log n), (Why?) so the method runs in time O(log n).
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

08 - CIS 121 Spring 2010 Data Structures and Introduction...

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

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