Skip to content

Real-World Applications

File systems are trees — naturally recursive!

root/
├── src/
│ ├── index.js
│ ├── utils/
│ │ ├── helper.js
│ │ └── math.js
│ └── app.js
├── README.md
└── package.json
function listAllFiles(directory) {
const results = [];
function traverse(node, path) {
if (node.type === 'file') {
results.push(path + '/' + node.name);
return;
}
for (const child of node.children) {
traverse(child, path + '/' + node.name);
}
}
traverse(directory, '');
return results;
}

Recursion explores ALL possible future game states. Backtracking allows undoing moves.

function canWin(board, player) {
const winner = checkWinner(board);
if (winner === player) return true;
if (winner !== null) return false;
if (isBoardFull(board)) return false; // Draw
const opponent = player === 'X' ? 'O' : 'X';
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === '.') {
board[i][j] = player; // Choose
if (!canWin(board, opponent)) { // Explore
board[i][j] = '.'; // Un-choose (for cleanup)
return true;
}
board[i][j] = '.'; // Un-choose
}
}
}
return false;
}
┌──────────────────────────────────────────────────────────┐
│ │
│ 📁 FILE SYSTEMS → Directory traversal │
│ 🌐 WEB CRAWLING → Follow links recursively │
│ 🧮 MATH EXPRESSIONS → Parse nested parentheses │
│ 🎨 FRACTAL GENERATION → Sierpinski triangle, Koch │
│ 🧬 DNA SEQUENCE MATCHING → Try all alignments │
│ 📦 JSON/XML PARSING → Nested structure traversal │
│ 🗺️ PATHFINDING → Maze solving, Google Maps │
│ ♟️ GAME AI → Chess, Go, Tic-Tac-Toe │
│ 📊 COMPILER DESIGN → Recursive descent parsing │
│ 🔐 CRYPTOGRAPHY → Brute force / pattern match │
│ 🏗️ TOWER OF HANOI → Classic recursive puzzle │
│ 📋 DEPENDENCY RESOLUTION → npm, package managers │
│ 🧩 CONSTRAINT SOLVING → Sudoku, crossword puzzles │
│ 🔄 UNDO/REDO SYSTEMS → State backtracking │
│ │
└──────────────────────────────────────────────────────────┘
  • Trees — Most tree algorithms (traversals, height, LCA) are inherently recursive
  • Graphs — DFS, cycle detection, topological sort use recursion
  • Stacks & Queues — The call stack is a LIFO structure

Happy Coding! 🚀