3.4.10 · HinglishTrees

Heap — max-heap and min-heap properties

1,689 words8 min readRead in English

3.4.10 · Coding › Trees


Heap KYA hota hai?


HOW hum isse store karte hain: array trick

Figure — Heap — max-heap and min-heap properties

Height aur kyun operations hain


Worked examples


Recall

Recall Active recall (answers cover karo)
  • Heap ko define karne wali do properties kya hain? → shape (complete tree) + heap-order (parent vs child).
  • Max-heap root kya hota hai? → maximum. Min-heap root? → minimum.
  • Kya siblings ordered hote hain? → Nahi.
  • 0-based: ke left aur right children? → . Parent? → .
  • -node heap ki height? → .
  • Heap mein BST ki tarah mein search kyun nahi ho sakti? → sirf ancestor–descendant order hai, siblings unordered hain.
Recall Feynman: 12-saal ke bachche ko explain karo

Socho ek pyramid of people jahan har boss unke directly neeche wale logon se lamba hai. Sabse lamba banda hamesha bilkul upar hota hai — usse tum turant dekh sakte ho. Lekin do side-by-side log kisi bhi height ke ho sakte hain; hum unhe kabhi compare nahi karte. Jab koi naya banda join karta hai, woh neeche ki next khaali jagah pe khada hota hai, phir apne boss ke saath upar ki taraf swap karta rehta hai jab tak usse upar koi lamba nahi milta. Yeh swapping choti hoti hai kyunki pyramid wide hoti hai aur bahut lambi nahi — isliye hamesha jaldi ho jaata hai.


Connections

  • Priority Queue — heaps ka main use (insert + extract-min/max).
  • Heapsort — build-heap phir baar baar root extract karo.
  • Complete Binary Tree — shape property ki foundation.
  • Binary Search Tree — contrast: BST siblings order karta hai, heap nahi karta.
  • Build-Heap (Heapify) O(n) — bottom-up sift-down.
  • Big-O Notation — kyun har operation hai.

Har heap ko kaunsi do properties satisfy karni chahiye?
Shape (complete binary tree, last level left-to-right fill) aur heap-order (parent vs child relation).
Max-heap mein maximum element kahan hota hai?
Root pe (index 0).
Min-heap mein minimum element kahan hota hai?
Root pe (index 0).
Kya heap mein sibling nodes ordered hote hain?
Nahi — sirf ancestor–descendant pairs ordered hote hain; siblings kisi bhi order mein ho sakte hain.
0-based: node i ke left aur right children ke indices?
2i+1 aur 2i+2.
0-based: node i ke parent ka index?
floor((i-1)/2).
n nodes wale heap ki height kya hoti hai?
floor(log2(n)).
Heap mein insert/extract O(log n) kyun hai?
Sift-up/sift-down zyada se zyada ek root-to-leaf path walk karta hai jis ki length = height = O(log n) hai.
Heap ko BST ki tarah O(log n) mein search kyun nahi kiya ja sakta?
Siblings unordered hote hain, isliye comparison se tree ka aadha hissa discard nahi kiya ja sakta.
Insert karte waqt naya element pehle kahan jaata hai aur kyun?
Next free leaf pe (last array slot) taaki complete-tree shape preserve ho, phir sift up hota hai.
Max-heapify (sift-down) mein bade ya chhote child se swap karte hain, aur kyun?
Bade child se, taaki naya parent doosre child pe bhi dominate kare, max-heap property maintain rahe.

Concept Map

requires

requires

means

guarantees

max-heap

min-heap

so

so

enables

O 1 peek

enables

index math

siblings unordered

Heap

Shape property

Heap-order property

Complete binary tree

Height floor log2 n

Parent >= children

Parent <= children

Root is maximum

Root is minimum

Priority queue

Array storage

left 2i+1 right 2i+2 parent floor i-1 over 2

Not a BST no O log n search