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[]{
|
||||
//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, []);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue