Open In App

What is Binary Heap

Last Updated : 01 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Example of Heap

Example of Heap

Characteristics of Binary Heap:

The following are the properties of a binary heap:

  • It is a complete binary tree, which means that all levels of the tree are completely filled, with the exception of the last one, and the nodes in the last level are filled from left to right.
  • The value of each node is greater than or equal to the values of its children in a binary heap.
  • The root node has the highest value in a max-heap, while it has the lowest value in a min-heap.

Types of Binary Heap:

There are two types of binary heaps: the max-heap and the min-heap:

  • Max-Heap: The maximum element is always stored at the root node in a max-heap. Each node’s children have a value that is less than or equal to that of their parent. As a result, the root node has the highest value in the heap.
  • Min-Heap: The minimum element in a min-heap is always stored at the root node. Each node’s children have a value greater than or equal to that of their parent. As a result, the root node has the lowest value in the heap.

Applications of Binary Heap:

  • Priority Queue: Binary heap is used to implement priority queue data structure.
  • Operating systems: Prioritizing processes based on their level of importance.
  • Networking: Prioritizing packets in a network to ensure timely delivery.
  • Databases: Indexing data based on importance to optimize search times.
  • Pathfinding algorithms: Finding the shortest path between two points in a graph.

Advantages of Binary Heap:

  • Efficient insertion and removal: It takes O(log n) time to add or remove an element from a binary heap, where n is the number of elements in the heap.
  • Quick access to the maximum or minimum element: Because the root node has the highest or lowest value, it can be accessed in O(1) time.
  • Sorting efficiently: A binary heap can be sorted in O(N logN) time.

Disadvantages of Binary Heap:

  • Inefficient for searching: Finding an element in a binary heap takes O(n) time because all elements must be examined.
  • Not suitable for dynamic data: Because the tree structure must be maintained, binary heaps are not suitable for situations where the data is constantly changing.

What else can you read?


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads