Update BTInOrder.ts

updated comments
This commit is contained in:
Steven Carpenter 2025-02-08 12:34:37 -05:00 committed by GitHub
parent 2daf682587
commit 8190b3986f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,16 +1,23 @@
function walk(curr:BinaryNode<number> | null, path:number[]): number[]{
//Base Case
if(!curr){
function walk(curr: BinaryNode<number> | null, path: number[]): number[] {
// Base Case: If the current node is null, return the current path (end recursion).
if (!curr) {
return path;
}
//Recurse
// Recursive Call: Traverse the left subtree first (In-Order: Left -> Node -> Right).
walk(curr.left, path);
// Process Current Node: Add the current node's value to the path.
path.push(curr.value);
// Recursive Call: Traverse the right subtree.
walk(curr.right, path);
//Post
// Return the path after visiting all nodes.
return path;
}
// Function to initiate the in-order traversal from the given tree head.
export default function in_order_search(head: BinaryNode<number>): number[] {
return walk(head,[]);
return walk(head, []);
}