lecture09heaps - Heaps Uses for heaps/priority queues...

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

View Full Document Right Arrow Icon
1 Heaps Uses for heaps/priority queues Heapsort: another O(N log N) sort Interesting study of data structure designed for special requirements
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 Suppose… You’ve been asked to write a notification system for the web Users enter events System sends email at certain date/times Millions of messages a day Millions of new messages in a day What data structure might you use?
Background image of page 2
3 What are the operations? Insert event May insert anywhere (in time order) In-order loading is common Obtain next pending event ExtractMin… (next event coming)
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 Bad choices Unordered vector O(1) Insert, O(N) ExtractMin Sorted Vector O(N) Insert, O(N) ExtractMin Unordered Linked List O(1) Insert, O(N) ExtractMin Sorted Linked List O(N) Insert, O(1) ExtractMin Trees?
Background image of page 4
5 Priority Queue Data structure designed to efficiently support these operations: Insert FindMin ExtractMin Useful in MANY applications Scheduling, process management, ordered arrival, priority management, controlling AI search etc.
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 Heaps A heap is a complete binary tree with the following criteria: For every node i other than the root, A[parent(i)] ≤ A[i] Parents are less than children Or the inverse rule can apply… What does this imply about the root?
Background image of page 6
7 Principle 1 ANY binary tree can be stored in an array Tree is easily kept in balance: paths <= log N Store Root in location 1 Store Lchild of node j in location 2*j Store Rchild in location 2*j+1 Find parent of node j in location j/2
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 It’s a tree stored in an array 2 4 5 6 7 10 20 8 11 9 2 4 5 6 7 10 20 8 11 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 A[ j ] j
Background image of page 8
9 Principle 2 For a binary min heap, value in parent <= value in child All paths from leaves to the root are non increasing in value So , the insertion operation will be O(log N) worst case compared to O(N) when inserting into a totally ordered array. Heap has less order than AVL tree, so it takes less effort to build or maintain it
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 Complete binary tree A complete binary tree is a binary tree for which all levels are full except the bottom one, which is full as far from the left as possible.
Background image of page 10
11 A Heap 2 4 5 6 7 10 20 8 11 9 Next insert will go here
Background image of page 11

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

View Full DocumentRight Arrow Icon
Functions for parent/children 2 4 5 6 7 10 20 8 11 9 2 4 5 6 7 10 20 8 11 9 1 2 3 4 5 6 7 8 9 10 Parent(i) return i/2 Left(i) return 2i Right(i) return 2i+1 Where does next new node go? 1
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 331 taught by Professor M.mccullen during the Spring '08 term at Michigan State University.

Page1 / 50

lecture09heaps - Heaps Uses for heaps/priority queues...

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

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