Priority Queues and Heaps - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Priority Queues and Heaps

Description:

HEAP ORDER PROPERTY - every node has highest priority of sub-tree rooted at that ... The last node in the heap is the rightmost node at depth h ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 20
Provided by: seedl
Category:
Tags: heap | heaps | priority | queues

less

Transcript and Presenter's Notes

Title: Priority Queues and Heaps


1
Priority Queues and Heaps
  • A Tree-based Data Structure

2
Storing and Processing Data
  • Applications process data, but you dont
    necessarily have all data at once
  • You can order data you have, but what if you get
    new data?
  • You can work with the data youve got, but you
    may get more data
  • We dont have that option with our current queue
    or stack

3
New Needs for Storing and Processing Data
  • We may need ability to put our data in order we
    need it and add new new data in appropriate
    position
  • Solution Priority Queue

4
Priority Queues
  • Analogy triage in emergency room
  • When a patient arrives is assigned priority value
    (based on previous information)
  • When doctor becomes free patient with highest
    priority is examined
  • This ensures that critical patients are treated
    quickly
  • Non-critical patients may have to wait a while

5
Applications of Priority Queues
  • Simulation systems
  • Priorities correspond to event times, to be
    processed in chronological order
  • Job scheduling in computer systems
  • Priorities indicate users to be served first

6
Priority Q ADT
  • Priority Q must implement at least
  • insert an element with some priority into Q
  • Based on some key
  • remove element with highest priority
  • changeKey of some element in Q
  • May mean reordering Q

7
Priority Q ADT
  • public interface PriorityQltEgt
  • public boolean insertItem(int k, E e)
  • //k entry key
  • //e item being stored
  • public E removeMin()
  • //Smallest key ? highest priority
  • public void changeKey(int k, E e)

8
Sorting
  • After adding elements to a priority queue they
    can be removed in order
  • Insert n elements one at a time with n insert
    operations
  • Remove in sorted order with n removeMin
    operations
  • Run time of each depends on implementation of
    priority Q

9
Linear-Based Priority Q
  • Elements either sorted when added or when removed
  • unsorted or sorted list (4 5 2 3 1) or (1 2 3 4
    5)
  • How long to insert?
  • How long to remove min?

UNSORTED SORTED O(1) O(n) O(n) O(1)
10
Binary Tree/Heap Based Priority Q
What is a heap?
11
Heap
  • A heap is not
  • A heap is complete binary tree with certain
    conditions placed on it

12
Heap Rules
  • Every node has a key
  • HEAP ORDER PROPERTY - every node has highest
    priority of sub-tree rooted at that node
  • key(v) gt key(parent(v))
  • Node has higher priority than its descendants
  • Root has highest priority

13
Complete Binary Tree
  • For complete binary tree of height h
  • all depths d, up to and including d h-1, have
    max number of nodes, i.e., 2d
  • all nodes at depth h are to the left
  • The last node in the heap is the rightmost node
    at depth h
  • If we maintain a complete tree, then the tree is
    as short as possible.
  • How short?
  • O(log(n)) shortheight is O(log(n))

14
Height log n is Important
  • Remember our depth function
  • It is simple example of function with run time
    based on height of tree.
  • Complete tree height will not be n but log n
  • log n time is much better than n

15
Implementing Pri Q with Heap
  • Operations (removeMin(), insertItem, and
    changeKey()) need to ensure tree
  • Remains complete
  • Satisfies heap order property
  • If tree is complete, what implementation should
    we use?
  • Array. It's simple to implement and won't waste
    space

16
Inserting into Heap
8
12
11
32
21
87
83
99
43
5
Is this still a heap?
17
Inserting into a Heap
  • We need to add to leftmost free spot in row h
  • In array implementation first open spot
  • But weve broken rule of heap
  • 21 not higher priority than 5
  • So swap them

8
12
11
32
21
87
83
99
43
5
18
Inserting into Heap
8
12
11
32
21
87
83
99
43
5
19
Insert Code
insertItem(item,key)   loc
insertAtEnd(item,key) //put in 1st free slot  
while (loc ! root key(parent(loc)) gt
key(loc)) swap(loc,parent(loc)) loc
parent(loc)
Write a Comment
User Comments (0)
About PowerShow.com