0
0
DSA Javascriptprogramming

Create a Binary Tree Manually in DSA Javascript

Choose your learning style9 modes available
Mental Model
A binary tree is a structure where each item can have up to two children, left and right. We build it by linking nodes manually one by one.
Analogy: Think of a family tree where each person can have up to two children. You add each person and connect them to their parents to form the tree.
    1
   / \
 null null
Dry Run Walkthrough
Input: Create a binary tree with root 1, left child 2, and right child 3
Goal: Build the tree manually by creating nodes and linking them correctly
Step 1: Create root node with value 1
    1
   / \
 null null
Why: Start with the root node as the base of the tree
Step 2: Create left child node with value 2 and link to root's left
    1
   / \
  2  null
 / \
null null
Why: Add left child to root to build the left subtree
Step 3: Create right child node with value 3 and link to root's right
    1
   / \
  2   3
 / \ / \
null null null null
Why: Add right child to root to complete the tree
Result:
    1
   / \
  2   3
 / \ / \
null null null null
Annotated Code
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function createManualBinaryTree() {
  const root = new TreeNode(1); // create root node
  root.left = new TreeNode(2); // link left child
  root.right = new TreeNode(3); // link right child
  return root;
}

function printTree(node) {
  if (!node) return 'null';
  const left = printTree(node.left);
  const right = printTree(node.right);
  return `${node.value} -> (${left}, ${right})`;
}

const tree = createManualBinaryTree();
console.log(printTree(tree));
const root = new TreeNode(1); // create root node
create the root node as the starting point
root.left = new TreeNode(2); // link left child
attach left child node to root
root.right = new TreeNode(3); // link right child
attach right child node to root
if (!node) return 'null';
base case for printing: no node means null
const left = printTree(node.left);
recursively print left subtree
const right = printTree(node.right);
recursively print right subtree
OutputSuccess
1 -> (2 -> (null, null), 3 -> (null, null))
Complexity Analysis
Time: O(n) because we create and print each node once
Space: O(n) because we store all nodes and recursion stack for printing
vs Alternative: Manual creation is simple and direct compared to building from arrays or traversals which need extra parsing
Edge Cases
Empty tree (no nodes)
The root is null and print returns 'null'
DSA Javascript
if (!node) return 'null';
Single node tree
Only root exists with left and right as null
DSA Javascript
const root = new TreeNode(1);
When to Use This Pattern
When you need to build a binary tree from scratch or small fixed data, manually creating nodes and linking is the simplest approach.
Common Mistakes
Mistake: Forgetting to link left or right child nodes after creating them
Fix: Always assign the new node to root.left or root.right explicitly
Mistake: Confusing left and right children positions
Fix: Remember left child goes to root.left and right child to root.right
Summary
Manually create each node and link left and right children to build a binary tree.
Use this when you have a small tree or want full control over node connections.
The key is to create nodes first, then connect them step-by-step to form the tree.