From e2aea4aab69fa18b1a68eaec79d0c865637d732b Mon Sep 17 00:00:00 2001 From: Tejas Gavankar Date: Tue, 29 Apr 2025 09:43:41 +0530 Subject: [PATCH] chore: base class for Binary Trees --- BinaryTree/binaryTreeBase.js | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 BinaryTree/binaryTreeBase.js diff --git a/BinaryTree/binaryTreeBase.js b/BinaryTree/binaryTreeBase.js new file mode 100644 index 0000000..4e8bd8f --- /dev/null +++ b/BinaryTree/binaryTreeBase.js @@ -0,0 +1,56 @@ + +// Node class for the binary tree +class Node { + constructor(val) { + this.data = val; + this.left = null; + this.right = null; + } +} + +// Function to perform inorder traversal +// of the tree and store values in 'arr' +function inorder(root, arr) { + // If the current node is null + // (base case for recursion), return + if (root === null) { + return; + } + // Recursively traverse the left subtree + inorder(root.left, arr); + // Push the current node's + // value into the array + arr.push(root.data); + // Recursively traverse + // the right subtree + inorder(root.right, arr); +} + +// Function to initiate inorder traversal +// and return the resulting array +function inOrder(root) { + // Create an empty array to + // store inorder traversal values + const arr = []; + // Call the inorder traversal function + inorder(root, arr); + // Return the resulting array + // containing inorder traversal values + return arr; +} + +// Creating a sample binary tree +const root = new Node(1); +root.left = new Node(2); +root.right = new Node(3); +root.left.left = new Node(4); +root.left.right = new Node(5); + +// Getting inorder traversal +const result = inOrder(root); +console.log(result) + +// Displaying the inorder traversal result +console.log("Inorder Traversal: " + result.join(" ")); + + \ No newline at end of file