Update BTInOrder.ts
updated comments
This commit is contained in:
parent
2daf682587
commit
8190b3986f
1 changed files with 13 additions and 6 deletions
|
|
@ -1,16 +1,23 @@
|
||||||
function walk(curr: BinaryNode<number> | null, path: number[]): number[] {
|
function walk(curr: BinaryNode<number> | null, path: number[]): number[] {
|
||||||
//Base Case
|
// Base Case: If the current node is null, return the current path (end recursion).
|
||||||
if (!curr) {
|
if (!curr) {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
//Recurse
|
|
||||||
|
// Recursive Call: Traverse the left subtree first (In-Order: Left -> Node -> Right).
|
||||||
walk(curr.left, path);
|
walk(curr.left, path);
|
||||||
|
|
||||||
|
// Process Current Node: Add the current node's value to the path.
|
||||||
path.push(curr.value);
|
path.push(curr.value);
|
||||||
|
|
||||||
|
// Recursive Call: Traverse the right subtree.
|
||||||
walk(curr.right, path);
|
walk(curr.right, path);
|
||||||
//Post
|
|
||||||
|
// Return the path after visiting all nodes.
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to initiate the in-order traversal from the given tree head.
|
||||||
export default function in_order_search(head: BinaryNode<number>): number[] {
|
export default function in_order_search(head: BinaryNode<number>): number[] {
|
||||||
return walk(head, []);
|
return walk(head, []);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue