3.4.11 · HinglishTrees

Heapify — bottom-up O(n) build

1,933 words9 min readRead in English

3.4.11 · Coding › Trees


WHAT is heapify?

Array ↔ tree index map (0-based):

  • ka parent:
  • ka left child:
  • ka right child:
  • Last internal (non-leaf) node: index (iske baad sab leaf hain).

HOW: the siftDown operation

siftDown(i) yeh assume karta hai ki i ke children ki roots wali subtrees already valid heaps hain, aur A[i] ko tab tak neeche push karta hai jab tak heap property hold na kare.

siftDown(A, i, n):
    while True:
        l = 2*i + 1
        r = 2*i + 2
        largest = i
        if l < n and A[l] > A[largest]: largest = l
        if r < n and A[r] > A[largest]: largest = r
        if largest == i: break          # parent already biggest
        swap(A[i], A[largest])
        i = largest                     # follow the swap down
buildHeap(A, n):
    for i from floor(n/2)-1 down to 0:
        siftDown(A, i, n)
Figure — Heapify — bottom-up O(n) build

DERIVATION: yeh kyun hai, kyun nahi?

Setup. nodes wale complete binary tree ki height hoti hai. Node height ko bottom se measure karo: leaves ki height hoti hai, root ki height .

Ek siftDown ki cost. Height wala node zyada se zyada levels neechie sink ho sakta hai, toh uska kaam hai.

Har height pe nodes count karo. Ek (lagbhag-)complete tree mein height pe zyada se zyada nodes hote hain.

Total kaam ka sum karo.

Ab ek key infinite sum evaluate karo. Standard identity use karo (neeche derive ki gayi hai):

plug karo:

Isliye


Worked examples


Common mistakes


Recall Feynman: ek 12-saal ke bacche ko samjhao

Ek tournament bracket socho jo ek pyramid ki tarah drawn hai jisme log hain, sabse bada aur taqatwar upar hona chahiye. Zyaatar log bottom rows mein khade hain — bahut saare hain, lekin har ek ko sirf ek ya do log se larna padta hai jo seedha unke upar hain: sasta. Upar jaate jaate, bahut kam log hote hain, aur sirf unhe hi lambi charhaayi karni padti hai. Kyunki bheedbhad wala bottom sasta hai aur lambi charhaiyan rare hain, total kushti sirf "har insaan ke liye lagbhag ek match" hoti hai — yeh log ki sankhya ke saath seedhi line mein badhti hai, zyada tezi se nahi.


Flashcards

Bottom-up heapify ki time complexity kya hai?
(linear), loose bound se tighter.
Bottom-up build-heap kis index se start karta hai?
Last internal node, (iske baad sab leaf hain).
Build-heap mein nodes ko bottom se top kyun process karte hain?
siftDown(i) ke liye zaroori hai ki dono children ki subtrees already valid heaps hon; bottom-up yeh ordering guarantee karta hai.
Height wale node pe siftDown ki cost?
— woh zyada se zyada levels sink ho sakta hai.
Height pe kitne nodes hote hain (zyada se zyada)?
Lagbhag ; unche nodes exponentially rare hain.
Kaun sa infinite sum bound prove karta hai, aur uski value?
, ek constant.
derive karo.
ko differentiate karo taaki mile, phir se multiply karo: .
Insertion-based build kyun hai lekin siftDown build kyun?
siftUp numerous leaves ko root ki taraf move karta hai (mehenga direction); siftDown lambi path sirf rare unche nodes ko deta hai.
Children/parent index formulas (0-based)?
left , right , parent .
Leaves kyun skip kiye jaate hain?
Ek single node already valid heap hai; use sift karna wasted work hai aur out-of-bounds child access ka risk hai.

Connections

  • Binary Heap — woh data structure jo heapify produce karta hai.
  • Heapsort — build-heap () use karta hai phir extract-max ().
  • Priority Queue — heaps iska standard implementation hain.
  • Complete Binary Tree — array↔tree index mapping.
  • Geometric Series / Amortized Analysis — linear bound ke peeche ka summation trick.
  • siftUp vs siftDown — direction complexity kyun decide karta hai.

Concept Map

index map defines

calls

begins at

skip so start at

iterates

ensures children heaps first

cost bounded by

few nodes are tall

weighted sum

converges to

beats

restores

Array as complete binary tree

Heap property parent >= children

buildHeap bottom-up

siftDown node

Start at last internal node

Process nodes upward

Leaves already valid

Node height k

Count n over 2^k+1 nodes

Sum k times n over 2^k+1

Total O n

Naive insert O n log n