Open In App

Heap Data Structure

Last Updated : 30 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is less than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.

Heap Data Structure

What is Heap Data Structure?

A heap is a binary tree-based data structure that satisfies the heap property: the value of each node is greater than or equal to the value of its children. This property makes sure that the root node contains the maximum or minimum value (depending on the type of heap), and the values decrease or increase as you move down the tree.

Types of Heaps

There are two main types of heaps:

  • Max Heap: The root node contains the maximum value, and the values decrease as you move down the tree.
  • Min Heap: The root node contains the minimum value, and the values increase as you move down the tree.

Heap Operations

Common heap operations are:

  • Insert: Adds a new element to the heap while maintaining the heap property.
  • Extract Max/Min: Removes the maximum or minimum element from the heap and returns it.
  • Heapify: Converts an arbitrary binary tree into a heap.

Heap Data Structure Applications

Heaps have various applications, like:

  • Heaps are commonly used to implement priority queues, where elements are retrieved based on their priority (maximum or minimum value).
  • Heapsort is a sorting algorithm that uses a heap to sort an array in ascending or descending order.
  • Heaps are used in graph algorithms like Dijkstra’s algorithm and Prim’s algorithm for finding the shortest paths and minimum spanning trees.

Basics of Heap Data Structure

Other Types of Heap Data Structure

Easy Problems on Heap Data Structure

Medium Problems on Heap Data Structure

Hard Problems on Heap Data Structure

Quick Links:

Recommended:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads