Title: Heaps and Priority Queues
1Heaps and Priority Queues
2Complete 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.
3Example
Complete Binary Tree
Incomplete Binary Tree
4Example (contd.)
Complete Binary Tree
5Finding 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)
6Finding 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
7Exercise
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?
8Exercise (contd.)
9Heap 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.
10Types 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.
11ADT Heap
- createHeap ( )
- destroyHeap ( )
- empty ( )
- heapInsert (NewItem)
- heapDelete ( ) // always the root
12Example
Consider data set
13Example
1
14Delete 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.
15Insert 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.
16Insert Node Example
15
5
9
3
6
2
17Priority 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
18Heap 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!