3.4.3 · HinglishTrees

Level-order traversal — BFS with queue

1,880 words9 min readRead in English

3.4.3 · Coding › Trees


Yeh hai kya


HOW it works — algorithm scratch se derive karo

Requirement se shuru karo: floor 0 visit karo, phir floor 1, phir floor 2…

  1. Hum kisi node ke children tab hi "dekh" sakte hain jab hum us node ko process kar lein. Toh humein un nodes ko yaad rakhna hoga jo discover ho chuke hain par abhi process nahi hue.
  2. Humein unhe discovery order mein process karna hai (pehle root, phir root ke children left→right, etc.). Yeh ordering demand = FIFO = ek queue.
  3. Toh recipe khud likh jaati hai:
Figure — Level-order traversal — BFS with queue

Har level ki ek alag list banana (level grouping)

Plain BFS ek flat list deta hai. Level ke hisaab se group karne ke liye, har round ke start mein queue ka size snapshot lo — woh size exactly us current level ke nodes ki count hai.


Complexity — reasoned, not memorized

  • Time : har node exactly ek baar enqueue aur ek baar dequeue hota hai. Har node par constant work ⇒ linear total.
  • Space jahan maximum width hai (kisi bhi single level par sabse zyada nodes). Worst case mein ek complete tree ke bottom level par nodes hote hain ⇒ .

Worked examples



Recall Feynman: 12-saal ke bachche ko samjhao (click to reveal)

Socho ek family tree deewar par bani hai. Tum names ek row at a time, left se right padhna chahte ho. Tumhare paas waiting mein khade logon ki ek line hai (ek queue). Tum line mein pehle waale ko bulao, unka naam likho, aur unhe kaho: "apne bachche line ke PEECHE bhej do." Phir line mein agla banda bulao. Kyunki naye bachche hamesha peeche jaate hain, tum ek poori row padh lete ho pehle, aur us row ka koi bhi bachcha tab tak nahi bulaya jaata. Yahi level order hai — aur waiting line hi queue hai!


Active recall

Level-order traversal ke liye kaun sa data structure natural hai, aur kyun?
Queue (FIFO); yeh nodes ko discovery order mein serve karta hai isliye paas/leftmost nodes pehle process hote hain, level-by-level order produce hoti hai.
Queue ki jagah stack use karna level-order traversal kyun tod deta hai?
Stack LIFO hai, isliye yeh sabse recently add hue (deepest) node mein pehle jaata hai — isse depth-first order milti hai, level order nahi.
Grouped (level-by-level) BFS mein, inner loop se pehle len(queue) snapshot kyun karna zaroori hai?
Kyunki queue children enqueue karte waqt badhti hai; count freeze karna exactly current level ke nodes ko fence karta hai taaki tum agले level mein na ghuso.
Tree par BFS ki time complexity kya hai aur kyun?
— har node ek baar enqueue aur ek baar dequeue hota hai jisme work lagta hai.
BFS ki space complexity kya hai aur use kya determine karta hai?
jahan maximum level width hai; worst case (complete tree ka bottom level ≈ n/2 nodes).
Python mein BFS ke liye list.pop(0) bura queue kyun hai?
Yeh hai (saare elements shift karta hai), BFS ko bana deta hai; ke liye collections.deque.popleft() use karo.
Tree par, BFS ka "root se distance" se kya relation hai?
Level/depth root se distance ke barabar hai, isliye BFS (jo badhte distance se expand karta hai) nodes ko level order mein visit karta hai.
BFS ke dauran child missing hone par crash rokne wala guard kya hai?
Enqueue/dereference se pehle if node.left / if node.right check karo (ya None skip karo).

Connections

  • Breadth-First Search (BFS) — general graph algorithm; level-order uska tree special case hai.
  • Depth-First Search (DFS) — stack/recursion counterpart (pre/in/post-order).
  • Queue (FIFO) data structure — BFS ka engine; Stack (LIFO) se contrast karo.
  • Binary Tree representation.left / .right wale nodes.
  • Shortest path in unweighted graphs — BFS ka killer app; level = hop count.
  • Tree height vs width — DFS-stack vs BFS-queue space trade-off explain karta hai.

Concept Map

is exactly

expands by distance

needs to

must keep

demands FIFO

enables

dequeue oldest gives

freeze n before loop

fences levels

each node once

Level-order traversal

Breadth-First Search

Queue FIFO

Visit floor by floor

Remember discovered nodes

Process in discovery order

BFS-with-queue procedure

Snapshot queue size n

One list per level

Time O of n