TheHingineer

  • Data Structures & Algorithms


  • DSA Part-1

  • DSA Part-2

  • DSA Part-3

  • DSA Part-4

  • DSA Part-5

  • Binary Heap: Min-Heap and Max-Heap Properties

    Binary Heap ek complete binary tree data structure hai jo ek special Heap Property ko satisfy karta hai. Binary Heaps Priority Queues implement karne aur Heap Sort algorithm execute karne ke liye primary data structure hain.

    The Two Structural Properties

    1. Complete Binary Tree Property: Heap ke sabhi levels completely filled hote hain except last level, jo strictly left se right fill hota hai. Is property ki wajah se Binary Heap ko pointers ki zaroorat nahi hoti; ise directly ek 1D Array me compactly represent kiya ja sakta hai.
    2. Heap-Order Property:
      • Max-Heap: Har parent node ki value apne dono children se greater than or equal to hoti hai: A[parent] >= A[child]. Root element hamesha pure heap ka Maximum hota hai.
      • Min-Heap: Har parent node ki value apne dono children se less than or equal to hoti hai: A[parent] <= A[child]. Root element hamesha pure heap ka Minimum hota hai.

    Array Index Mapping Formulas (0-based indexing)

    Parent Index:                i
    Left Child Index:            2 * i + 1
    Right Child Index:           2 * i + 2
    Parent of Node i:            floor((i - 1) / 2)
    
    Tree View:                  Array View:
             [ 90 ] (0)         Index:  0    1    2    3    4    5
            /      \            Val:  [ 90 | 80 | 70 | 40 | 50 | 60 ]
         [ 80 ](1) [ 70 ](2)
         /    \    /
      [40](3)[50](4)[60](5)
    

    Agli post me hum cover karenge Heap Operations: Heapify, Insertion, and Deletion.

    Scroll to Top