1.2.37 · D3 · HinglishIntroduction to Programming (Python)

Worked examplesRecursion — call stack visualization, base case, recursive case

2,959 words13 min read↑ Read in English

1.2.37 · D3 · Coding › Introduction to Programming (Python) › Recursion — call stack visualization, base case, recursive c


The scenario matrix

Har recursive problem inhi cells mein se kisi ek mein hoti hai. Last column us example ka naam deta hai jo use cover karta hai.

# Cell (scenario class) Isme tricky kya hai Covered by
A Normal shrink-by-one ordinary recursive case, ek branch Example 1 (factorial)
B Base case POORA input hi hai input already tiny, koi recursion nahi chalti Example 2 (fact(0), summ([]))
C Degenerate / invalid input negative ya empty — kya yeh rukta hai? Example 3 (fact(-1))
D Ek saath do base cases do stops chahiye, ek nahi Example 4 (Fibonacci)
E Branching (tree-shaped) recursion har call kai calls spawn karta hai Example 4 (Fibonacci)
F Shrink-by-halving (depth limit karta hai) problem jaldi drop hoti hai, depth chhoti rehti hai Example 5 (power via halving)
G Order matters: call se pehle vs baad mein kaam winding vs unwinding (phir bhi linear) Example 6 (print order)
H Real-world word problem story → base + recursive case mein translate karo Example 7 (Russian dolls)
I Exam-style twist subtle bug jo aapko spot karna hai Example 8 (missing return)

Example 1 — Cell A: ordinary shrink-by-one

Figure — Recursion — call stack visualization, base case, recursive case

Example 2 — Cell B: base case POORA input hi hai


Example 3 — Cell C: degenerate / invalid input


Example 4 — Cells D & E: do base cases + branching

Figure — Recursion — call stack visualization, base case, recursive case

Example 5 — Cell F: shrink-by-halving (chhoti depth)


Example 6 — Cell G: order matters (winding vs unwinding)

def up(n): if n < 0: return up(n-1) print(n) # call ke BAAD

Note: inme se har ek har frame mein exactly **ek** recursive call karta hai — yeh *linear* recursion hai, branching nahi. Sirf kaam ki *position* badlti hai.
**Forecast:** `down(3)` aur `up(3)` ke liye, padhne se pehle dono printed sequences predict karo.

**Step 1 — `down`: print winding ke dauran hota hai.**
`print(n)` recursive call se *pehle* run hota hai, toh yeh frames ke **neeche jaate waqt** fire karta hai: `3, 2, 1, 0`.
*Yeh step kyun?* Recursive call se pehle rakha kaam descent par execute hota hai.

**Step 2 — `up`: print unwinding ke dauran hota hai.**
`print(n)` sub-call ke poori tarah finish hone ke *baad* run hota hai, toh yeh frames ke **wapas pop hote waqt** fire karta hai: `0, 1, 2, 3`.
*Yeh step kyun?* Recursive call ke baad rakha kaam poore subtree ka wait karta hai, phir wapasi mein run hota hai — yeh exactly *post-order* [[Tree and Graph Traversal]] ka kaam karne ka tarika hai.

**Verify:** `down(3)` → `3,2,1,0`; `up(3)` → `0,1,2,3`. ✅ Same recursion, mirror-image output — sirf ek line ki position sab decide karti hai.
Figure — Recursion — call stack visualization, base case, recursive case

Example 7 — Cell H: real-world word problem


Example 8 — Cell I: exam-style twist (bug spot karo)


Recall Matrix par quick self-test

fact(0) kaunsa cell hai? ::: Cell B — base case poora input hi hai, zero recursive calls. fib kaunse cell mein aayega aur do base cases kyun? ::: Cells D & E — branching recursion, aur n-2 branch 0 ko leap karke guzar sakta hai, toh hum 0 aur 1 dono pakadते hain. print ko recursive call ke baad move karne se woh kis phase mein run karta hai yeh badal jaata hai? ::: Winding (descent) se unwinding (return) mein, output order reverse ho jaata hai. Halving-power ko shrink-by-one se kaafi kam frames kyun chahiye? ::: Yeh base tak lagbhag log₂(e) recursive calls mein pahunchta hai instead of e ke.