Adsa-2unit Heap Continue
Adsa-2unit Heap Continue
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
3) Start from the first index of non-leaf node whose index is given by n/2 - 1.
Algorithm
Heapify(array, size, i)
set i as largest
leftChild = 2i + 1
rightChild = 2i + 2
To create a Max-Heap:
MaxHeap(array, size)
call heapify
For Min-Heap, both leftChild and rightChild must be smaller than the parent for all
nodes.
Insert Element into Heap
If there is no node,
create a newNode.
insert the newNode at the end (last node from left to right.)
For Min Heap, the above algorithm is modified so that parentNode is always smaller
than newNode.
Delete Element from Heap
remove noteToBeDeleted
For Min Heap, above algorithm is modified so that both childNodes are greater
smaller than currentNode.
Peek operation returns the maximum element from Max Heap or minimum element
from Min Heap without deleting the node.
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
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.
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.
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.
Insertion
Algorithm
insert(H, x)
degree[x] = 0
p[x] = NIL
child[x] = NIL
left[x] = x
right[x] = x
mark[x] = FALSE
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
Extract Min
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.
operation to those roots such that the min-heap property is maintained (i.e. the
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.
Decreasing a 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
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
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.
This process makes use of decrease-key and extract-min operations. The following steps
are followed for deleting a node.
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.