{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

9_heaps - withoutremovingit imp

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

View Full Document Right Arrow Icon
Priority Queues and Heaps
Background image of page 1

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

View Full Document Right Arrow Icon
October 2004 John Edgar 2 ! A queue should implement at least the first two of these operations: ! insert – insert item at the back of the queue ! remove – remove an item from the front ! peek – return the item at the front of the queue without removing it ! It is assumed that these operations will be implemented efficiently ! That is, in constant time
Background image of page 2
! Either with an array ! Or with a linked list October 2004 John Edgar 3 4 18 25 2 5 0 1 2 3 4 5 6 7 front 4 6 back 7 3
Background image of page 3

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

View Full Document Right Arrow Icon
! Queues are first‐in first‐out (FIFO) ! A fancier type of queue, which maintains an ordering is a priority queue October 2004 John Edgar 4
Background image of page 4
September 2004 John Edgar 5
Background image of page 5

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

View Full Document Right Arrow Icon
! Items in a priority queue are given a priority value ! Which could be numerical or something else ! The highest priority item is removed first ! Uses include ! System requests ! Data structure to support Dijkstra’s Algorithm September 2004 John Edgar 6
Background image of page 6
! Can items be inserted and removed efficiently from a priority queue? ! Using an array, or ! Using a linked list? ! Note that items are not removed based on the order in which they are inserted September 2004 John Edgar 7 Now we’ll see how we can do these efficiently (using a different data structure)
Background image of page 7

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

View Full Document Right Arrow Icon
! Items in a priority queue have a priority ! Not necessarily numerical ! Could be lowest first or highest first ! The highest priority item is removed first ! Priority queue operations ! Insert ! Remove in priority queue order Both operations should be performed in at most O (log n ) time October 2004 John Edgar 8
Background image of page 8
September 2004 John Edgar 9
Background image of page 9

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

View Full Document Right Arrow Icon
! Items have to be removed in priority order ! This can only be done efficiently if the items are ordered in some way ! One option would be to use a balanced binary search tree ! Binary search trees are fully ordered and insertion and removal can be implemented in O (log n ) time Some operations (e.g. removal) are complex Although operations are O (log n ) they require quite a lot of structural overhead ! There is a much simpler binary tree solution October 2004 John Edgar 10
Background image of page 10
! A heap is binary tree with two properties ! Heaps are complete ! All levels, except the bottom, are completely filled in ! The leaves on the bottom level are as far to the left as possible. ! Heaps are partially ordered ! The value of a node is at least as large as its children’s values, for a max heap or ! The value of a node is no greater than its children’s values, for a min heap October 2004 John Edgar 11
Background image of page 11

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

View Full Document Right Arrow Icon
October 2004 John Edgar 12 complete binary trees incomplete binary trees
Background image of page 12
October 2004 John Edgar 13 98 41 86 13 65 9 10 32 29 44 23 21 17 Heaps are not fully ordered, an inorder traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 17, 41, 29
Background image of page 13

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

View Full Document Right Arrow Icon
!
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}