Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Recursion
- A function solving a problem by calling itself on a smaller part of it.
- Base case
- The input a recursive function answers directly, without calling itself.
- Recursive case
- The path that calls the function again on a smaller input and combines the result.
- Stack depth
- The number of unfinished invocations at one time. Physical stack use also depends on the runtime and compiled code.
- RangeError
- The error matched by the archived Node stack-exhaustion probe.
Smaller calls and waiting callers
A recursive function delegates part of its work to another invocation of itself. A base case answers directly, while a recursive case moves toward that base and uses the smaller answer. Each unfinished invocation retains its own place in the calculation, so work before and after the nested call has different timing.
For structured input, define both the smaller part and the way its answer contributes to the whole. A parent chain and a branching subtree require different combinations. The maximum nesting depth also matters: a finite input can still require more call-stack resources than its execution environment supports.
01Down and back
Work before the recursive call happens on the way down; work after it, on the way back
A directory-manifest builder reports a path’s depth, reconstructs paths from parent records and totals the bytes below each directory. It works on supplied records in memory. Each recursive answer must include the current directory as well as the smaller result; otherwise the manifest can omit a component or an entire branch.
The builder first checks how an ancestor walk suspends and resumes its current directory. This reduced probe represents three path components as depths 3, 2 and 1. visit prints enter, visits the parent depth, then prints leave; depth 0 ends the walk. Predict all six lines of visit(3).
visit returns early when n is 0.
// Diagnostic probe: visit the ancestors of a three-component manifest path.
function visit(n: number): void {
if (n === 0) {
return;
}
console.log(`enter ${n}`);
visit(n - 1);
console.log(`leave ${n}`);
}
visit(3);
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment.
02Each call’s own result
The innermost call returns first, and each caller adds its own part
After reconstructing paths, the manifest also needs byte totals. First isolate how a caller combines a smaller answer using one directory’s direct file-byte list. sum takes the first size and the sum of the rest, then prints its list and result. Predict the first line.
The rest is fileBytes.slice(1), a new array.
function sum(fileBytes: number[]): number {
if (fileBytes.length === 0) {
return 0;
}
const rest = sum(fileBytes.slice(1));
const total = fileBytes[0]! + rest;
console.log(`[${fileBytes.join(" ")}] gives ${total}`);
return total;
}
sum([4, 9, 2]);
03How deep
Logical depth and runtime resources are separate checks
A valid parent tree can still be a long chain. This historical stress probe requests 1000 and then 1000000 steps; the C++ version deliberately uses a loop for the larger request. Decide what the source and a small successful call actually establish about the larger request.
The second call requests a million logical recursive steps; do not assume a portable Node stack budget.
function depth(n: number): number {
if (n === 0) {
return 0;
}
return 1 + depth(n - 1);
}
console.log(depth(1_000));
console.log(depth(1_000_000));
04Repair the directory manifest
Path depth, full paths, then subtree bytes
First report the depth of supplied relative paths, including the empty marker and one-component paths. Then reconstruct each directory’s root-to-directory path from parent records, even when rows arrive bottom up. Finally add its own file bytes and every descendant’s bytes. These are pure manifest calculations; no real filesystem access is involved.
The lab opens in your reading language and has its own language control. Each file contains its imports, the reader, any supplied types and a complete main.
Repair the directory manifest
The base case’s answer, the side of the call where work happens, then combining every recursive result.
In the frontend A bounded directory tree can render one child component per child node. Optional
The manifest can also drive a nested directory list. Each component renders one supplied node; an empty child list needs no nested list, and every child receives a smaller subtree. The input is a finite acyclic tree. This shows recursive component structure, not a claim that framework rendering uses the same stack frames or limits as the native probes.
The Svelte excerpt is Folder.svelte and imports that component itself. Render the root Folder inside a ul; these static excerpts do not access a filesystem or compute the lab’s path strings or byte totals.
type FolderNode = { name: string; children: FolderNode[] };
export function Folder({ node }: { node: FolderNode }) {
return (
<li>
{node.name}
{node.children.length > 0 && (
<ul>
{node.children.map((child) => (
<Folder key={child.name} node={child} />
))}
</ul>
)}
</li>
);
}
JSX asks React to render each child component. Calling Folder as an ordinary function is not the component-use pattern.
Sources: React: React calls components and Hooks,Svelte: self import replaces svelte:self.
Sign in to keep your read marks on every device. Sign in
Was this helpful?