Data Structures

What is Heap Data Structure? | Data Structure Tutorial

What is Heap Data Structure? | Data Structure Tutorial

What is Heap Data Structure? | Data Structure Tutorial

In this article, you will learn about the Heap Data Structure in brief.

A Heap data structure is a whole binary tree that satisfies the heap property, where any node is:

  • Always larger than its child nodes and the key of the root node is the largest among all the other nodes. It is also known as the max heap property.
  • Always smaller than its child node and the key of the root node is always the smallest among all other nodes. It is also known as the min-heap property.

It is also known as a binary heap.

Heap Operations

Heapify

It is a way of creating a heap data structure from a binary tree. It is used to create a Max and Min heap.

  • Take an array
  • Form a complete binary tree from the given array
  • Start from the initial index of a non-leaf node whose index is n/2-1.
  • Set the value of the current item i as the largest.
  • The index of the left child is given by 2i + 1 and the index of the right child is given by 2i + 2
  • Swap the largest element with the CurrentElement
  • Repeat the steps from 3-7 until the complete subtree is heapified.

Insert Element into a heap

  1. Add the new item at the end of the tree.
  2. Heapify the tree

Delete Element from the heap

  1. Choose the element to be deleted
  2. Swap the element with the last element
  3. Delete the last element
  4. Heapify the tree.

Peek( Find out the max/min element): Peek operation used in returning the maximum element from the max heap and minimum element from the minimum heap without deleting the node.

Extract - Max/Min: Extract-Max returns the node with the maximum value after removing it from a max heap whereas Extract-Min returns the node with the minimum value after removing it from Min-heap.

Heap Applications

  • Heap Sort
  • Dijkstra’s Algorithm
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.