0% found this document useful (0 votes)
3 views

Heap Trees

Uploaded by

sunanda17battu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Heap Trees

Uploaded by

sunanda17battu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What is Heap Data Structure?

A Heap is a special Tree-based Data Structure in which the tree is


a complete binary tree.

Types of heaps:
Generally, heaps are of two types.
Max-Heap:
In this heap, the value of the root node must be the greatest among all its
child nodes and the same thing must be done for its left and right sub-tree
also.
The total number of comparisons required in the max heap is according to
the height of the tree. The height of the complete binary tree is always log n;
therefore, the time complexity would also be O(log n).
Min-Heap:
In this heap, the value of the root node must be the smallest among all its
child nodes and the same thing must be done for its left and right sub-tree
also.
The total number of comparisons required in the min heap is according to the
height of the tree. The height of the complete binary tree is always log n;
therefore, the time complexity would also be O(log n).

Page | 1
Properties of Heap:
Heap has the following Properties:

 Complete Binary Tree: A heap tree is a complete binary tree, meaning


all levels of the tree are fully filled except possibly the last level, which is
filled from left to right. This property ensures that the tree is efficiently
represented using an array.

 Heap Property: This property ensures that the minimum (or maximum)
element is always at the root of the tree according to the heap type.

 Parent-Child Relationship: The relationship between a parent node at


index ‘i’ and its children is given by the formulas: left child at
index 2i+1 and right child at index 2i+2 for 0-based indexing of node
numbers.

 Efficient Insertion and Removal: Insertion and removal operations in


heap trees are efficient. New elements are inserted at the next available
position in the bottom-rightmost level, and the heap property is restored
by comparing the element with its parent and swapping if necessary.
Removal of the root element involves replacing it with the last element
and heapifying down.

 Efficient Access to Extremal Elements: The minimum or maximum


element is always at the root of the heap, allowing constant-time access.

Heapify:
It is the process to rearrange the elements to maintain the property of heap
data structure. It is done when a certain node creates an imbalance in the
heap due to some operations on that node. It takes O(log N) to balance the
tree.
 For max-heap, it balances in such a way that the maximum element is
the root of that binary tree and
 For min-heap, it balances in such a way that the minimum element is the
root of that binary tree.

Page | 2
Insertion:

 If we insert a new element into the heap since we are adding a new
element into the heap so it will distort the properties of the heap so we
need to perform the heapify operation so that it maintains the property of
the heap.
This operation also takes O(logN) time.

Assume initially heap(taking max-heap) is as follows


8
/ \
4 5
/\
1 2
Now if we insert 10 into the heap
8
/ \
4 5
/ \ /
1 2 10
After heapify operation final heap will be look like this
10
/ \
4 8
/ \ /
1 2 5

Deletion:

 If we delete the element from the heap it always deletes the root element
of the tree and replaces it with the last element of the tree.
 Since we delete the root element from the heap it will distort the
properties of the heap so we need to perform heapify operations so that it
maintains the property of the heap.
It takes O(logN) time.

Page | 3
Assume initially heap(taking max-heap) is as follows
15
/ \
5 7
/ \
2 3
Now if we delete 15 into the heap it will be replaced by leaf node of the tree
for temporary.
3
/ \
5 7
/
2
After heapify operation final heap will be look like this
7
/ \
5 3
/
2

getMax (For max-heap) or getMin (For min-heap):

It finds the maximum element or minimum element for max-heap and min-
heap respectively and as we know minimum and maximum elements will
always be the root node itself for min-heap and max-heap respectively. It
takes O(1) time.

removeMin or removeMax:

This operation returns and deletes the maximum element and minimum
element from the max-heap and min-heap respectively. In short, it deletes
the root element of the heap binary tree.

Page | 4
A max (min)heap is a complete binary tree with the property that the value at
each node is at least as large as(as small as) the values at its children(if they
exist).Call this property the heap property.

The definition of a max heap implies that one of the largest elements is at the
root of the heap. If the elements are distinct, then the root contains the
largest item. A max heap can be implemented using an array a[].

To insert an element into the heap, one adds it "at the bottom" of the Heap
and then compares it with its parent, grand parent, great grandparent, and so
on, until it is less than or equal to one of these values.

Insertion into a heap

Action of Insert inserting 90 into an existing heap

Page | 5
To delete the maximum key from the max heap, we use an algorithm called
Adjust. Adjust takes as input the array a[] and the integers i and n.

It regards a[l:n] as a complete binary tree. If the sub trees rooted at 2i and
2i+1are already max heaps, then Adjust will rearrange elements of a[] such
that the tree rooted at i is also a maxheap. The maximum element from the
maxheap a[1:n] can be deleted by deleting the root of the corresponding
Complete binary tree. The last element of the array, that is, a[n],is copied to
the root, and finally we call Adjust(a,1,n-1).

Algorithm Deletion from a heap

Page | 6
Forming a heap from the set {40,80,35,90,45,50,70}

Algorithm Creating a heap out of n arbitrary elements

Page | 7
Action of Heapify(a,7)on the data (100,119,118,171,112, 151,and 132)

Algorithm Heapsort

The call of Heapify requires only O(n) operations, Adjust possibly requires
O(log n) operations for each invocation. Thus the worst-case time is
O(n logn).

Page | 8

You might also like