0
0
DSA Typescriptprogramming

Create a Binary Tree Manually in DSA Typescript

Choose your learning style9 modes available
Mental Model
A binary tree is a structure where each node has up to two children, called left and right. Creating it manually means linking nodes by hand to form the tree.
Analogy: Think of a family tree where each person can have up to two children. You build the tree by connecting parents to their children one by one.
    1
   / \
  2   3
 / \
4   5
Dry Run Walkthrough
Input: Create a binary tree with root 1, left child 2, right child 3, and 2's children 4 and 5
Goal: Build the tree by creating nodes and linking them correctly
Step 1: Create root node with value 1
1 (root) -> left: null, right: null
Why: Start with the root node as the base of the tree
Step 2: Create node with value 2 and link as left child of root
1 -> left: 2, right: null
2 -> left: null, right: null
Why: Add left child to root to build left subtree
Step 3: Create node with value 3 and link as right child of root
1 -> left: 2, right: 3
2 -> left: null, right: null
3 -> left: null, right: null
Why: Add right child to root to complete root's children
Step 4: Create node with value 4 and link as left child of node 2
1 -> left: 2, right: 3
2 -> left: 4, right: null
3 -> left: null, right: null
4 -> left: null, right: null
Why: Add left child to node 2 to build left subtree further
Step 5: Create node with value 5 and link as right child of node 2
1 -> left: 2, right: 3
2 -> left: 4, right: 5
3 -> left: null, right: null
4 -> left: null, right: null
5 -> left: null, right: null
Why: Add right child to node 2 to complete its children
Result:
1 -> left: 2, right: 3
2 -> left: 4, right: 5
3 -> left: null, right: null
4 -> left: null, right: null
5 -> left: null, right: null
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;

  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

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

function printTree(node: TreeNode | null): void {
  if (!node) return;
  const leftVal = node.left ? node.left.val : 'null';
  const rightVal = node.right ? node.right.val : 'null';
  console.log(`${node.val} -> left: ${leftVal}, right: ${rightVal}`);
  printTree(node.left);
  printTree(node.right);
}

const root = createManualBinaryTree();
printTree(root);
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 to root
root.right = new TreeNode(3); // link right child
attach right child to root
root.left.left = new TreeNode(4); // link left child of node 2
attach left child to node 2
root.left.right = new TreeNode(5); // link right child of node 2
attach right child to node 2
printTree(node.left);
recursively print left subtree
printTree(node.right);
recursively print right subtree
OutputSuccess
1 -> left: 2, right: 3 2 -> left: 4, right: 5 4 -> left: null, right: null 5 -> left: null, right: null 3 -> left: null, right: null
Complexity Analysis
Time: O(n) because we create and print each node once
Space: O(n) because we store all nodes in memory
vs Alternative: Manual creation is direct and simple for small trees, unlike automated or input-based creation which may require parsing or loops
Edge Cases
Empty tree (no nodes)
Function would return null or no root node, printTree prints nothing
DSA Typescript
if (!node) return;
Single node tree
Only root node exists, left and right are null
DSA Typescript
root.left = null; root.right = null;
When to Use This Pattern
When you need to build a binary tree from scratch or fixed values, manually creating nodes and linking them is the straightforward approach.
Common Mistakes
Mistake: Forgetting to link child nodes to their parents
Fix: Always assign child nodes to the parent's left or right property explicitly
Mistake: Mixing up left and right children
Fix: Remember left child goes to .left and right child goes to .right property
Summary
Manually create a binary tree by making nodes and linking them as left and right children.
Use this when you want to build a specific tree structure by hand for learning or testing.
The key is to create each node and connect it properly to its parent to form the tree.