3.4.12 · HinglishTrees

Heap operations — insert O(log n), extract-max - min O(log n), decrease-key

1,932 words9 min readRead in English

3.4.12 · Coding › Trees


Heap KYA hai (definition)

Figure — Heap operations — insert O(log n), extract-max - min O(log n), decrease-key

Dono repair primitives (sab kuch inhi se banta hai)

sift-up (bubble-up)

sift-down (heapify-down)


insert —


extract-max —


decrease-key (min-heap) / increase-key (max-heap) —


Cost summary (Forecast-then-Verify)

Operation Steps Time
peek max/min padho
insert append + sift-up
extract-max/min last→root swap, sift-down
decrease/increase-key update + sift-up
build-heap (bonus) se 0 tak sift-down

Max-heap ki heap property kya hai?
Har parent ≥ dono children (root maximum hai).
0-indexed: node i ke children?
left = 2i+1, right = 2i+2.
0-indexed: node i ka parent?
floor((i-1)/2).
Heap mein insert algorithm?
Nayi key index n par append karo (tree complete rehti hai), phir property restore karne ke liye sift-up karo. O(log n).
Insert O(log n) kyun hai O(n) nahi?
sift-up ek hi path par swap karta hai jiska length ≤ height = floor(log2 n) hai.
extract-max ke steps?
A[0] save karo; last element ko root mein laao; size ghataao; sift-down(0) karo; saved max return karo.
extract mein LAST element ko root mein kyun laate hain?
Yeh O(1) mein completeness preserve karta hai; koi child promote karne se beech mein hole ban jaata aur array indexing toot jaati.
Min-heap par decrease-key kaun sa primitive use karta hai?
sift-up — choti value upar belonging karti hai.
Max-heap par increase-key kaun sa primitive use karta hai?
sift-up — badi value upar belonging karti hai.
Peek O(1) kyun hai lekin extract O(log n) kyun?
Peek sirf A[0] padhta hai; extract ko ek root-to-leaf path rebalance karna padta hai.
Heap mein siblings ordered hote hain?
Nahi — sirf parent–child chain ordered hai; heaps fully sorted nahi hote.
Ek array se heap banana ka time?
O(n) via index n/2-1 se 0 tak sift-down karo.

Recall Feynman: 12 saal ke bacche ko samjhao

Socho logon ka ek pyramid jahan har boss apne neeche wale dono logon ke upar khada hai aur unse lamba hai. Sabse lamba insaan hamesha upar hota hai, toh sabse lamba dhundhna ekdum instant hai. Jab koi naya insaan aata hai, woh neeche khali jagah par khada hota hai aur apne boss ke saath tab tak swap karta rehta hai jab tak woh lamba ho — woh utna hi upar jaata hai jitna deserve karta hai. Jab top wala insaan chala jaata hai, hum bilkul last wale insaan ko upar laate hain (taaki koi gap na ho), phir use apne sabse lambe bacche ke saath position swap karte karte neeche utarne dete hain jab tak upar wale sab lambe na ho jayein. Har chadhna ya utarna sirf pyramid mein ek seedhi line hai, aur pyramid chota hai (lagbhag floors), toh yeh fast hai.

Concept Map

enforces

kept

enables

read root

too big node

too small node

append then

breaks upward

swap root then

one path

height bound

Binary heap array

Heap property parent >= child

Complete tree shape

Index formulas 2i+1 2i+2

sift-up climb

sift-down sink

insert O log n

extract-max O log n

decrease-key

Root is extreme element