Operations on Binary Heaps

The common operation involved using heaps are:

  1. Heapify: Process to rearrange the heap in order to maintain heap-property.
  2. Insertion: Add a new item in the heap.
  3. Deletion: Delete an item from the heap.
  4. Find-max (or Find-min): find a maximum item of a max-heap, or a minimum item of a min-heap, respectively.

1. 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. 

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.

Example :

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

3. 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.

Example:

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

4. Find-max (or Find-min):

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.

Algorithm for building a Heap of an input array A:

BUILD-HEAP(A) 

    heapsize := size(A); 

    for i := floor(heapsize/2) downto 1 

        do HEAPIFY(A, i); 

    end for 

END

A quick look over the above algorithm suggests that the running time is O(n*log(n)) since each call to Heapify costs O(log(n)) and and Build-Heap makes O(n) such calls but this upper bound, though correct, is not asymptotically tight.  

We can derive a tighter bound by observing that the running time of Heapify depends on the height of the tree ‘h’ (which is equal to log(n), where n is a number of nodes) and the heights of most sub-trees are small.. Line-3 of Build-Heap runs a loop from the index of the last internal node (heapsize/2) to the index of root(1) . Hence, Heapify takes a different time for each node, which is:

For finding the Time Complexity of building a heap, we must know the number of nodes having height h. For this we use the fact that, A heap of size n has at most nodes with height h. 

To derive the time complexity, we express the total cost of Build-Heap as- 

Step 2 uses the properties of the Big-Oh notation to ignore the ceiling function and the constant . Similarly in Step three, the upper limit of the summation can be increased to infinity since we are using Big-Oh notation. Sum of infinite G.P. (x < 1) 

On differentiating both sides and multiplying by x, we get 

Putting the result obtained in (3) back in our derivation (1), we get

 

Hence Proved that the Time complexity for Building a Binary Heap is 

Binary Heap Notes for GATE Exam [2024]Time Complexity of building a heap:

In the GATE Exam, understanding binary heaps is like having a secret weapon. Questions might ask you to pick the right tool for a job, and heaps are often the superheroes of quick and efficient data organization.

Table of Content

  • Introduction to Heap:
  • Types of heaps:
  • Representation of Binary Heap:
  • Operations on Binary Heaps:
  • Advantages of Heap Data Structure:
  • Disadvantages of Heap Data Structure:
  • Previously Asked GATE Questions on Binary Heap

Similar Reads

Introduction to Heap:

Binary heap is a complete binary tree where the root of any subtree has a higher (or lower based on the type of heap) value than all the nodes in its subtree....

Types of heaps:

There are 2 types of heaps:...

Representation of Binary Heap:

A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as an array....

Operations on Binary Heaps:

The common operation involved using heaps are:...

Advantages of Heap Data Structure:

Efficient insertion and deletion: The heap data structure allows efficient insertion and deletion of elements. When a new element is added to the heap, it is placed at the bottom of the heap and moved up to its correct position using the heapify operation. Similarly, when an element is removed from the heap, it is replaced by the bottom element, and the heap is restructured using the heapify operation.Efficient priority queue: The heap data structure is commonly used to implement a priority queue, where the highest priority element is always at the top of the heap. The heap allows constant-time access to the highest priority element, making it an efficient data structure for implementing priority queues.Guaranteed access to the maximum or minimum element: In a max-heap, the top element is always the maximum element, and in a min-heap, the top element is always the minimum element. This provides guaranteed access to the maximum or minimum element in the heap, making it useful in algorithms that require access to the extreme values.Space efficiency: The heap data structure requires less memory compared to other data structures, such as linked lists or arrays, as it stores elements in a complete binary tree structure.Heap-sort algorithm: The heap data structure forms the basis for the heap-sort algorithm, which is an efficient sorting algorithm that has a worst-case time complexity of O(n log n)....

Disadvantages of Heap Data Structure:

Lack of flexibility: The heap data structure is not very flexible, as it is designed to maintain a specific order of elements. This means that it may not be suitable for some applications that require more flexible data structures.Not ideal for searching: While the heap data structure allows efficient access to the top element, it is not ideal for searching for a specific element in the heap. Searching for an element in a heap requires traversing the entire tree, which has a time complexity of O(n).Not a stable data structure: The heap data structure is not a stable data structure, which means that the relative order of equal elements may not be preserved when the heap is constructed or modified.Memory management: The heap data structure requires dynamic memory allocation, which can be a challenge in some systems with limited memory. In addition, managing the memory allocated to the heap can be complex and error-prone.Complexity: While the heap data structure allows efficient insertion, deletion, and priority queue implementation, it has a worst-case time complexity of O(n log n), which may not be optimal for some applications that require faster algorithms....

Previously Asked GATE Questions on Binary Heap

Question 1: The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is....