0% found this document useful (0 votes)
10 views9 pages

Ads Unit-3

Uploaded by

dksdambala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Ads Unit-3

Uploaded by

dksdambala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT-III

PRIORITY QUEUES (HEAPS)


What is a Priority Queue?
1) Stores prioritized key-value pairs
2) Implements insertion
• No notion of storing at particular position
3) Returns elements in priority order
• Order determined by key

Stacks and Queues


• Removal order determined by order of inserting
Sequences
• User chooses exact placement when inserting and explicitly chooses removal order
Priority Queue
• Order determined by key
• Key may be part of element data or separate

An entry in a priority queue is simply a key-value pair


Priority queues store entries to allow for efficient insertion and removal based on
keys Methods:
• getkey : returns the key for this entry
• getvalue : returns the value associated with this entry
Implementing PQ with Unsorted Sequence
Each call to insertItem(k, e) uses insertLast( ) to store in Sequence
• O(1) time
Each call to extractMin( ) traverses the entire sequence to find the minimum, then
removes element
• O(n) time

Implementing PQ with Sorted Sequence


Each call to insertItem(k, e) traverses sorted sequence to find correct position, then
does insert
• O(n) worst case
Each call to extractMin( ) does removeFirst( )
• O(1) time

Implementing PQ with a BST


Each call to insertItem(k, e) does tree insert
• O(log(n)) worst case
Each call to extractMin( ) does delete( )
• O(log(n)) time

ADVANCED DATA STRUCTURES Page 1


Heaps
" A heap is a binary tree storing keys at its nodes and satisfying the following
properties:
Heap-Order: for every internal node v other than the root,
key(v) key(parent(v))
Complete Binary Tree: let h be the height of the heap
for i = , … , h - 1, there are 2i nodes of depth i
at depth h - 1, the internal nodes are to the left of the external nodes
The last node of a heap is the rightmost node of depth h

Height of a Heap
Theorem: A heap storing n keys has height O(log n)
Proof: (we apply the complete binary tree property)
Let h be the height of a heap storing n keys
Since there are i keys at depth i = , … , h - 1 and at least one key at
depth h, we have n + + 4 + … + h-1 + 1
Thus, n h , i.e., h log n

ADVANCED DATA STRUCTURES Page 2


Heap

Binary tree-based data structure

• Complete in the sense that it fills up levels as completely as possible

• Height of tree is O(log n)

Can be stored using the array representation (just add at the end of the array)

Use extendable arrays to expand and shrink as Needed

Heap Example

Binary Heaps
• A binary heap is a binary tree NOT a BST that is:
› Complete: the tree is completely filled except possibly the bottom level, which is
filled from left to right
 Satisfies the heap order property
• every node is less than or equal to its children or every node is greater than
or equal to its children
• The root node is always the smallest node or the largest, depending on the
heap order
Heap order property
• A heap provides limited ordering information
• Each path is sorted, but the subtrees are not sorted relative to each other

ADVANCED DATA STRUCTURES Page 3


A binary heap is NOT a binary search tree

Fig: These are all valid binary heaps (minimum)

Binary Heap vs Binary Search Tree

Structure property
• A binary heap is a complete tree
› All nodes are in use except for possibly the right end of the bottom row

ADVANCED DATA STRUCTURES Page 4


Examples:

Array Implementation of Heaps

• Root node = A[ ]

• Children of A[i] = A[ i], A[2i + 1]

• Keep track of current size N number of nodes)

ADVANCED DATA STRUCTURES Page 5


FindMin and DeleteMin:

Maintain the Structure Property

ADVANCED DATA STRUCTURES Page 6


Maintain the Heap Property

Applications of Priority queues:

ADVANCED DATA STRUCTURES Page 7


The Selection Problem Event Simulation Problem:

Binomial Queues

A Binomial Queue is a collection of heap-ordered trees known as a forest. Each tree


is a binomial tree.

A recursive definition is:

1. A binomial tree of height 0 is a one-node tree.

2. A binomial tree, Bk, of height k is formed by attaching a binomial tree Bk− to the
root of another binomial tree Bk− .

ADVANCED DATA STRUCTURES Page 8


Examples

Implementing Binomial Queues

1. Use a k-ary tree to represent each binomial tree – sibling and child pointers

2. Use a Vector to hold references to the root node of each binomial tree

3. Keep a reference to smallest root for past find min (e.g. a Heap on positions).

Use a k-ary tree to represent each binomial tree.

Use an array to hold references to root nodes of each binomial tree.

ADVANCED DATA STRUCTURES Page 9

You might also like