VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-to-max-heap-data-structure/

⇱ Introduction to Max-Heap - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Max-Heap

Last Updated : 23 Mar, 2026

A Max-Heap is a Data Structure with the following properties:

  • It is a Complete Binary Tree.
  • The value of the root node must be the largest among all its descendant nodes, and the same property must hold for its left and right subtrees.

Implementation of Max-Heap Data Structure

A heap can be efficiently represented using an array.
If a node is stored at index i:

  • Its left child is at index 2 * i + 1.
  • Its right child is at index 2 * i + 2.
  • The parent of a node at index i can be found at index [(i-1)/2].

Operations on Max-heap Data Structure and their Implementation

Here are some common operations that can be performed on a Heap Data Structure data structure,

Insertion - O(log n) Time and O(n) Space

Insertion in a Max-Heap involves the following steps:

  • Add the new element to the end of the heap, in the next available position in the last level of the tree.
  • Compare the new element with its parent. If the parent is smaller than the new element, swap them.
  • Repeat step 2 until the parent is greater than or equal to the new element, or until the new element reaches the root of the tree.

Illustration:

👁 Insertion-In-Max-Heap

Output
Inserted 10 into the max-heap: 10 
Inserted 7 into the max-heap: 10 7 
Inserted 11 into the max-heap: 11 7 10 
Inserted 5 into the max-heap: 11 7 10 5 
Inserted 4 into the max-heap: 11 7 10 5 4 
Inser...

Deletion - O(log n) Time and O(n) Space

Removing the largest element (the root) from a Max-Heap involves the following steps:

  • Replace the root (or the element to be deleted) with the last element in the heap.
  • Remove the last element from the heap, since it has been moved to the root.
  • Heapify-down: The element now at the root may violate the Max-Heap property, so perform heapify starting from the root to restore the heap property.

Illustration:  


Output
Initial heap: 100 41 51 13 31 16 
Heap after deleting 13: 100 41 51 16 31 

Peek operation - O(1) Time and O(n) Space

 To access the maximum element (i.e., the root of the heap), the value of the root node is returned.

👁 peak-element-of-max-heap

Output
Peak element: 9

Heapify operation - O(n) Time and O(n) Space

Heapify is an operation that places a node in its correct position in a heap so that the subtree rooted at that node satisfies the heap property.
To build a max heap from an unsorted array, we start from the last non-leaf node and move up to the root, calling heapify on each node. For each node, if it is smaller than any of its children, we swap it with the largest child and continue until it is larger than both children. After all nodes are processed, the array becomes a valid max heap.

👁 heapify-operations-in-max-heap

Output
Original array: 10 5 15 2 20 30 
Max-Heap after heapify operation: 30 20 15 2 5 10 

Max Heap vs Min Heap

👁 420046868

for more detail refer - Difference between Min Heap and Max Heap

Applications of Max-Heap Data Structure

  • Heap Sort: Heaps are used in the heap sort algorithm, an efficient sorting method with worst-case time complexity of O(n log n), applied in database indexing and numerical analysis.
  • Memory Management: Heaps help allocate and deallocate memory dynamically, managing memory blocks efficiently for programs.
  • Graph Algorithms: Heaps are used in algorithms like Dijkstra, Prim, and Kruskal to implement efficient priority queues.
  • Job Scheduling: Heaps enable quick access to highest-priority tasks, making them ideal for task scheduling based on priority or deadlines.

Advantages of Max-Heap Data Structure

  • Quick access to maximum: Max-heap allows O(1) access to the largest element.
  • Efficient insert/delete: Both operations take O(log n), making it suitable for large datasets.
  • Priority Queues: Max-heap can implement priority queues for job scheduling, task prioritization, and event-driven simulations.
  • Heap Sort: Can be used for heap sort, an efficient O(n log n) sorting algorithm.
  • Space Efficient: Can be implemented as an array, using less memory than BSTs or linked lists.
Comment
Article Tags: