You are on page 1of 29

Heaps

1
Binary SearchTrees
When discussing binary search trees we looked at
examples like
 

The items are placed in the tree so that the value


stored at each node of the tree is greater than the
values stored in its left subtree, and less than the
values stored in its right subtree.
Items are ordered from left to right.
Heaps Trees
An interesting alternative structure arises if the items are ordered
from bottom to top

The value stored at each node of the tree is greater than or equal to
the values stored in its left subtree, and greater than or equal to
the values stored in its right subtree.
Note that the value at a node is now greater than or equal to the
values in both subtrees

Note: A heap data structure should not be confused with the heap which is a
common name for dynamically allocated memory.
Binary Heaps Trees
Properties:
A simple local test for whether the heap property holds is
that the value stored at every node should be no greater
than the value stored at its parent
It follows that every path from the root to a leaf forms a
decreasing sequence.
The maximum number of children each node can have
depends on the type of heap, but in many types it is at
most two
A complete binary tree which also satisfies the
ordering heap property is called a binary heap.
Complete Binary Tree
Recall that a binary tree of height h is said to be
complete if it is full down to level h – 1, and level h is
filled from left to right.
Examples:

Now, recall our array-based representation of a


binary tree:
5
Array-Based Representation of a
Complete Binary Tree

6
Arithmetic Tree Traversal

Some authors leave the first element of the array


empty, so that the data items are stored in elements 1,
….n.
The children are then at 2i and  2i+1 and the parent is
at i/2.
Max heaps and Min heaps
A max-heap has the property that every node stores a
value that is greater than or equal to the value of either
of its children.

A min-heap has the property that every node stores a


value that is less than or equal to that of its children.
Applications of Binary Heaps
Priority Queue (ADT) where as heap is a structure to
implement it.
Sorting
The ADT Priority Queue
A priority queue is an ADT in which items are
ordered by a priority value. The item with the
highest priority is always the next to be removed
from the queue. (Highest Priority In, First Out:
HPIFO)
Supported operations include:
Insert a new item into a priority queue
Retrieve, and then delete from the priority queue the
item with the highest priority value
Destroy a priority queue
Determine whether a priority queue is empty

10
Sorting:
Step 1: Build a heap/Insert
Step 2: remove( )
Priority queue
To implement priority queues
 Priority queue = a queue where all elements have a “priority”
associated with them
 Remove in a priority queue removes the element with the

highest priority (lowest priority value)


insert
removeMin
Insertion
To add an element to a heap we must perform an up-
heap operation (also known as bubble-up, heapify-up),
Add the element to the bottom level of the heap.
Compare the added element with its parent; if they are
in the correct order, stop.
If not, swap the element with its parent and return to
the previous step.

Time complexity is (log n)


Insertion
Item to be inserted is 35

We can only fill it from left to right


Insertion
Heap order is violated. We have this path:
Solution
Bubble 35 up in the tree till the heap order is satisfied
Follow the following sequence of transformations
Example Insert
55

50 45

15 40 35 20

5 10 25 30 47
Solution
55

50 47

15 40 45 20

5 10 25 30 35
Removal, deletion
The procedure for deleting the root from the heap (effectively
extracting the maximum element in a max-heap or the
minimum element in a min-heap) and restoring the properties
is called down-heap (also known as bubble-down , heapify-down
and extract-min/max).
Replace the root of the heap with the last element on the last
level.
Compare the new root with its children; if they are in the
correct order, stop.
If not, swap the element with one of its children and return to
the previous step. (Swap with its smaller child in a min-heap
and its larger child in a max-heap.)
Removal Algorithm
Remove() will remove 40 from the heap. Which
element will be the root then?

Replace the last element on the lowest level with root


Removal Algorithm
Now it’s a binary tree. But is it a heap?

What to do. Demote 17 by comparing it with its


children
Removal Algorithm
Still, is it a heap?
Removal Algorithm
Demote 17 again
Remove Example

60

55 45

15 50 35 20

5 10 40 30 25

23
Solution
55

50 45

15 40 35 20

5 10 25 30
Examples insertion(min)
Putting an element 2 into a priority Queue
Insert 12
Solution
Remove Min
Solution

You might also like