MC4101
MC5301 ADVANCED DATA STRUCTURES AND ALGORITHMS
TOPIC: HEAP
INTRODUCTION
Heap data structure is a complete binary tree that satisfies the heap property.
It is also called as a binary heap.
Heap have two properties.
o Structure Property
o Heap order Property
Structure Property:
A heap is a binary tree that is completely filled with the possible exception at the
bottom level, which is filled from left to right. It is also known as Complete Binary
Tree.
Heap Order Property is the property of a node in which,
o (for max heap) key of each node is always greater than its child node/s and
the key of the root node is the largest among all other nodes;
A [parent (x) ] ≥ A [x]
o (for min heap) key of each node is always smaller than the child node/s and
the key of the root node is the smallest among all other nodes.
A [parent (x) ] ≤ A [x]
Heap Operations
Some of the important operations performed on a heap are described below along
with their algorithms.
Heapify
Heapify is the process of creating a heap data structure from a binary tree.
It is used to create a Min-Heap or a Max-Heap.
1) Let the input array be
2) Create a complete binary tree from the array
3) Start from the first index of non-leaf node whose index is given by n/2 - 1.
4) Set current element i as largest.
5) The index of left child is given by 2i + 1 and the right child is given by 2i + 2.
If leftChild is greater than currentElement (i.e. element at ith index),
set leftChildIndex as largest. If rightChild is greater than element in largest,
set rightChildIndex as largest.
6) Swap largest with currentElement
7) Repeat steps 3-7 until the subtrees are also heapified.
Algorithm
Heapify(array, size, i)
set i as largest
leftChild = 2i + 1
rightChild = 2i + 2
if leftChild > array[largest]
set leftChildIndex as largest
if rightChild > array[largest]
set rightChildIndex as largest
swap array[i] and array[largest]
To create a Max-Heap:
MaxHeap(array, size)
loop from the first index of non-leaf node down to zero
call heapify
For Min-Heap, both leftChild and rightChild must be smaller than the parent for all
nodes.
Insert Element into Heap
Algorithm for insertion in Max Heap
If there is no node,
create a newNode.
else (a node is already present)
insert the newNode at the end (last node from left to right.)
heapify the array
1) Insert the new element at the end of the tree.
2) Heapify the tree.
For Min Heap, the above algorithm is modified so that parentNode is always smaller
than newNode.
Delete Element from Heap
Algorithm for deletion in Max Heap
If nodeToBeDeleted is the leafNode
remove the node
Else swap nodeToBeDeleted with the lastLeafNode
remove noteToBeDeleted
heapify the array
1) Select the element to be deleted.
2) Swap it with the last element.
3) Remove the last element.
4) Heapify the tree.
For Min Heap, above algorithm is modified so that both childNodes are greater
smaller than currentNode.
Peek (Find max/min)
Peek operation returns the maximum element from Max Heap or minimum element
from Min Heap without deleting the node.
For both Max heap and Min Heap
return rootNode
Extract-Max/Min
Extract-Max returns the node with maximum value after removing it from a Max Heap
whereas Extract-Min returns the node with minimum after removing it from Min Heap.
Heap Data Structure Applications
Heap is used while implementing a priority queue.
Dijkstra’s Algorithm
Heap Sort
Fibonacci Heap
Fibonacci heap is a modified form of a binomial heap with more efficient heap
operations than that supported by the binomial and binary heaps.
Unlike binary heap, a node can have more than two children.
The fibonacci heap is called a fibonacci heap because the trees are constructed in a
way such that a tree of order n has at least Fn+2 nodes in it, where Fn+2 is the (n +
2)nd Fibonacci number.
Properties of a Fibonacci Heap
Important properties of a Fibonacci heap are:
1) It is a set of min heap-ordered trees. (i.e. The parent is always smaller than the
children.)
2) A pointer is maintained at the minimum element node.
3) It consists of a set of marked nodes. (Decrease key operation)
4) The trees within a Fibonacci heap are unordered but rooted.
Memory Representation of the Nodes in a Fibonacci Heap
The roots of all the trees are linked together for faster access. The child nodes of a
parent node are connected to each other through a circular doubly linked list as
shown below.
There are two main advantages of using a circular doubly linked list.
1) Deleting a node from the tree takes O(1) time.
2) The concatenation of two such lists takes O(1) time.
Operations on a Fibonacci Heap
Insertion
Algorithm
insert(H, x)
degree[x] = 0
p[x] = NIL
child[x] = NIL
left[x] = x
right[x] = x
mark[x] = FALSE
concatenate the root list containing x with root list H
if min[H] == NIL or key[x] < key[min[H]]
then min[H] = x
n[H] = n[H] + 1
Inserting a node into an already existing heap follows the steps below.
1) Create a new node for the element.
2) Check if the heap is empty.
3) If the heap is empty, set the new node as a root node and mark it min.
4) Else, insert the node into the root list and update min.
Find Min : The minimum element is always given by the min pointer.
Union
Union of two fibonacci heaps consists of following steps.
1) Concatenate the roots of both the heaps.
2) Update min by selecting a minimum key from the new root lists.
Extract Min
It is the most important operation on a fibonacci heap.
In this operation, the node with minimum value is removed from the heap and the
tree is re-adjusted.
The following steps are followed:
1) Delete the min node.
2) Set the min-pointer to the next root in the root list.
3) Create an array of size equal to the maximum degree of the trees in the heap
before deletion.
4) Do the following (steps 5-7) until there are no multiple roots with the same
degree.
5) Map the degree of current root (min-pointer) to the degree in the array.
6) Map the degree of next root to the degree in array.
7) If there are more than two mappings for the same degree, then apply union
operation to those roots such that the min-heap property is maintained (i.e. the
minimum is at the root).
An implementation of the above steps can be understood in the example below.
1) We will perform an extract-min operation on the heap below.
2) Delete the min node, add all its child nodes to the root list and set the min-pointer
to the next root in the root list.
3) The maximum degree in the tree is 3. Create an array of size 4 and map degree of
the next roots with the array.
4) Here, 23 and 7 have the same degrees, so unite them.
5) Again, 7 and 17 have the same degrees, so unite them as well.
6) Again 7 and 24 have the same degree, so unite them.
7) Map the next nodes.
8) Again, 52 and 21 have the same degree, so unite them
9) Similarly, unite 21 and 18.
10)Map the remaining root.
11)The final heap is
.
Decrease Key and Delete Node Operations on a Fibonacci Heap
In a fibonacci heap, decrease-key and delete-node are important operations. These
operations are discussed below.
Decreasing a Key
In decreasing a key operation, the value of a key is decreased to a lower value.
Following functions are used for decreasing the key.
Decrease-Key
1) Select the node to be decreased, x, and change its value to the new value k.
2) If the parent of x, y, is not null and the key of parent is greater than that of
the k then call Cut(x) and Cascading-Cut(y) subsequently.
3) If the key of x is smaller than the key of min, then mark x as min.
Cut
1) Remove x from the current position and add it to the root list.
2) If x is marked, then mark it as false.
Cascading-Cut
1) If the parent of y is not null then follow the following steps.
2) If y is unmarked, then mark y.
3) Else, call Cut(y) and Cascading-Cut(parent of y).
Decrease Key Example
The above operations can be understood in the examples below.
Example: Decreasing 46 to 15.
1) Decrease the value 46 to 15.
Cut part: Since 24 ≠ nill and 15 < its parent, cut it and add it to the root list. Cascading-
Cut part: mark 24 (Add 15 to root list and mark 24)
Example: Decreasing 35 to 5
Decrease the value 35 to 5.
Cut part: Since 26 ≠ nill and 5<its parent, cut it and add it to the root list.
Cascading-Cut part: Since 26 is marked, the flow goes to Cut and Cascading-Cut.
Cut(26): Cut 26 and add it to the root list and mark it as false.
Cascading-Cut(24):
Since the 24 is also marked, again call Cut(24) and Cascading-Cut(7). These operations
result in the tree below.
Cut 24 and add it to root list
Since 5 < 7, mark 5 as min.
Deleting a Node
This process makes use of decrease-key and extract-min operations. The following steps
are followed for deleting a node.
1) Let k be the node to be deleted.
2) Apply decrease-key operation to decrease the value of k to the lowest possible
value (i.e. -∞).
3) Apply extract-min operation to remove this node.
HashSet Data Structure
The HashSet is a Set class in Java that uses a hash table internally.
The HashSet is able to add, find and remove items in constant time, so it can solve
many problems in an optimal manner. (Ruby and Python provide the same
capabilities with their Set classes.)
A HashSet is a good choice whenever you have a large set of items and need to quickly
check if a given item is in the set or not. We already saw that this can be used to detect
duplicate data as you go through a list of numbers.
It is often used to check live user data against established data for matches.
For example, a Spell Checker that checks words as a user types them needs to
extremely fast, so it uses a HashSet of real words to check if a given word is real or
not.