Heaps and Priority Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Heaps and Priority Queues

Description:

3. Use trickle-down to form a heap. Growth rate function : 3*log N 1 = O(log N) ... 2. Trickle up (repeat for various levels) to form a heap. ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 19
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Heaps and Priority Queues


1
Heaps and Priority Queues
  • B.Ramamurthy

2
Complete Binary Tree
  • A complete binary tree is a binary tree
  • with leaves on either a single level or on two
    adjacent levels
  • such that the leaves on the bottommost level are
    placed as far left as possible
  • such that nodes appear in every possible position
    in each level above the bottom level.

3
Example
Complete Binary Tree
Incomplete Binary Tree
4
Example (contd.)
Complete Binary Tree
5
Finding nodes in Contiguous Representation
0 1 2 3 4 5 6
7 8 9 10 11
D
B
F
A
E
G
H
K
J
L
C
I
Let Aj be a node and n be the number of
nodes. Left child of Aj is A2j1 if 2j1
lt n Right child of Aj is A2j2 id 2j2 lt
n Parent of Aj is Aj-1/2 if j gt 0 Root is
A0 If 2j gt n the Aj is a leaf. Lets look
at an example (from last years final exam)
6
Finding nodes (contd.)
  • height h of a tree of n nodes h ceiling(log n)
  • A perfect tree of h height has power(2, h1) -1
    nodes out of which power(2,h-2) are internal.
  • Number of leaf nodes in a tree of n nodes
  • (power(2, h-1) - 1) (ceiling(n - (power2,h) -
    1))/2

7
Exercise
Given a complete binary tree with 2670 nodes and
with contiguous representation A1 to
A2670 what is the maximum level l of the
tree? How many nodes are in the level n where n lt
l ? Give an expression in terms of n. Derive the
expression for number of internal nodes of the
tree. How many leaf nodes are there in this tree?
Where is the parent of node A261
located? Where are the children of node at A261
located?
8
Exercise (contd.)
9
Heap A Formal Definition
  • Heap is a loosely ordered complete binary tree.
  • A heap is a complete binary tree with values
    stored in its nodes such that no child has a
    value greater than the value of its parent.
  • A heap is a complete binary tree
  • 1. That is empty or
  • 2a. Whose root contains a search key greater than
    or equal to both its children node.
  • 2b. Whose left subtree and right subtree are
    heaps.

10
Types of heaps
  • Heaps can be max heaps or min heaps. Above
    definition was for a max heap.
  • In a max-heap the root is higher than or equal to
    the values in its left and right child.
  • In a min-heap the root is smaller than or equal
    to the values in its left and right child.

11
ADT Heap
  • createHeap ( )
  • destroyHeap ( )
  • empty ( )
  • heapInsert (NewItem)
  • heapDelete ( ) // always the root

12
Example
Consider data set
13
Example
1
14
Delete Root Item
  • In a max heap the root item is the largest and is
    the chosen one for deletion.
  • 1. After deletion of root, two disjoint heaps
    result.
  • 2. Place last node as a root, to form a
    semi-heap.
  • 3. Use trickle-down to form a heap.
  • Growth rate function 3log N 1 O(log N)
  • Consider a heap example discussed above.
  • Delete root item.

15
Insert An Item
  • 1. Insert a node as a last node.
  • 2. Trickle up (repeat for various levels) to form
    a heap.
  • Consider inserting 15 into the heap of the
    delete example.
  • Insert is also a O(log N) operation.

16
Insert Node Example
15
5
9
3
6
2
17
Priority Queue
  • Priority Queue implemented as a heap. Priority
    queue inserts are done according to some criteria
    known as priority
  • Insert location are according to priority and
    delete are to the head of queue.
  • pQueue constructor
  • pQueueInsert
  • pQueueDelete
  • Data
  • Heap pQ

18
Heap Sort
  • Heapsort
  • 1. Represent elements as a max-heap.
  • 2. Delete item at root, as it is the largest.
  • 3. Heap, repeat step 2, until one item is left.
  • O(Nlog N) in the worst case!
Write a Comment
User Comments (0)
About PowerShow.com