{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS1132_Fall_2011_Lecture8_BB

# CS1132_Fall_2011_Lecture8_BB - Lecture 8 Binary heaps CS...

This preview shows pages 1–9. Sign up to view the full content.

Lecture 8 Binary heaps CS 103 1

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

View Full Document
Binary heaps A binary heap (often just referred to as a heap) is a special kind of balanced binary tree . The tree satisfies two invariants: The node at the top (root) of the tree has minimum or maximum priority. The different paths from root to leaf differ in height by at most one. Has to be complete tree. At the bottom of the tree there may be some missing leaves; these are to the right to all of the leaves that are present. Suppose the priorities are just numbers. Here is a possible heap: 3 / \ / \ 5 9 / \ /
Binary heaps insert 1. Put the element at first missing leaf. 2. Switch it with its parent if its parent is larger: " bubble up " 3. Repeat #2 as necessary. 3 / \ / \ 5 9 / \ / 12 6 10 3 / \ / \ 5 9 / \ / 12 6 10

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

View Full Document
Binary heaps Example : inserting 4 into previous tree . / \ / \ 12 6 10 4 [3 5 9 12 6 10 4 ] [3 5 4 12 6 10 9] This operation requires only O (lg n ) time -- the tree depth is O(lg n ) , and we do a bounded amount of work on each level. 3 / \ / \ 5 9 / \ / \ 12 6 10 4 3 / \ / \ 5 9 / \ / \ 12 6 10 4 3 / \ / \ 5 4 / \ / \ 12 6 10 9 3 / \ / \ 5 4 / \ / \ 12 6 10 9
5 Implementation of a heap when implementing a complete binary tree, we actually can "cheat" and just use an array index of root = 1 (leave 0 empty for simplicity) for any node n at index i , index of n .left_child = 2 i index of n .right_child = 2 i + 1

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

View Full Document
Array tree implementation i Item Left Child 2*i Right Child 1 Pam 2 2 3 2 Joe 4 4 5 3 Sue 6 6 7 4 Bob 8 8 9 5 Mike 10 10 -1 6 Sam -1 11 -1 7 Tom -1 13 -1 8 Ann -1 15 -1 9 Jane -1 17 -1 10Mary -1 19 -1 Bo b Mik e Sa m To m Pam Joe Sue Ann Jane Mary LeftChild(i) = 2*i RightChild(i) = 2*i + 1 corollary: a complete binary tree can be implemented using an array (the example tree shown is not a heap)
7 Array binary tree - parent i Item Parent i / 2 1 Pam -1 0 2 Joe 1 1 3 Sue 1 1 4 Bob 2 2 5 Mike 2 2 6 Sam 3 3 7 Tom 3 3 8 Ann 4 4 9 Jane 4 4 10Mary 5 5 Bo b Mik e Sa m To m Pam Joe Sue Ann Jane Mary Parent(i) = i / 2

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

View Full Document
8 Implementing a priority queue using a Binary Heap In Chapter 4 we saw two implementations of Priority Queue ADT using 1.Sorted linked list 2.Unsorted array In Chapter 9 we will see an implementation of Priority Queue ADT using 3.Binary Heap with a contiguous sequential representation of complete binary trees
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 32

CS1132_Fall_2011_Lecture8_BB - Lecture 8 Binary heaps CS...

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

View Full Document
Ask a homework question - tutors are online