Bird
Raised Fist0
DSA Typescriptprogramming

Create a Binary Tree Manually in DSA Typescript

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of manually creating a binary tree in TypeScript?
easy
A. To avoid using classes or objects
B. To automatically sort data without coding
C. To understand how nodes connect and form the tree structure
D. To create a linked list instead of a tree

Solution

  1. Step 1: Understand binary tree structure

    A binary tree consists of nodes where each node has at most two children, left and right.
  2. Step 2: Purpose of manual creation

    Manually creating a binary tree helps visualize and understand how nodes connect to form the tree.
  3. Final Answer:

    To understand how nodes connect and form the tree structure -> Option C
  4. Quick Check:

    Manual creation = Understand tree structure [OK]
Hint: Manual creation helps visualize node connections [OK]
Common Mistakes:
  • Thinking it automatically sorts data
  • Confusing binary tree with linked list
  • Assuming no objects or classes are needed
2. Which of the following is the correct way to define a binary tree node class in TypeScript?
easy
A. class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } }
B. class Node { value: number; left: number; right: number; constructor(value: number) { this.value = value; this.left = 0; this.right = 0; } }
C. class Node { value: number; left: Node; right: Node; constructor(value: number) { this.value = value; } }
D. class Node { value: number; left: any; right: any; constructor() { this.value = 0; } }

Solution

  1. Step 1: Check node properties and types

    A binary tree node should have a value and left/right children which can be nodes or null.
  2. Step 2: Verify constructor initialization

    Constructor must initialize value and set left and right to null initially.
  3. Final Answer:

    class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } -> Option A
  4. Quick Check:

    Node class with nullable children = class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = null; this.right = null; } } [OK]
Hint: Children must be Node or null, initialized in constructor [OK]
Common Mistakes:
  • Using number type for left/right instead of Node or null
  • Not initializing left and right to null
  • Omitting constructor parameters
3. Given the following TypeScript code to create a binary tree manually, what will be the output of console.log(root.left?.value)?
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
medium
A. 5
B. 10
C. 15
D. undefined

Solution

  1. Step 1: Understand tree structure from code

    Root node has value 10, left child is node with value 5, right child is node with value 15.
  2. Step 2: Access left child's value

    root.left?.value accesses left node's value safely, which is 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    root.left.value = 5 [OK]
Hint: Access left child value directly with root.left?.value [OK]
Common Mistakes:
  • Confusing root value with left child value
  • Expecting right child value instead
  • Not using optional chaining and getting error
4. Identify the error in the following TypeScript code that tries to create a binary tree node manually:
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(10);
root.left = 5;
root.right = new Node(15);
medium
A. Missing constructor parameters in Node class
B. Using wrong class name for Node
C. Not initializing root.right
D. Assigning number 5 to root.left instead of a Node or null

Solution

  1. Step 1: Check type of root.left assignment

    root.left is declared as Node or null, but assigned number 5 which is invalid.
  2. Step 2: Confirm correct assignment type

    root.left should be assigned a Node instance or null, not a number.
  3. Final Answer:

    Assigning number 5 to root.left instead of a Node or null -> Option D
  4. Quick Check:

    Type mismatch in root.left assignment = Assigning number 5 to root.left instead of a Node or null [OK]
Hint: Children must be Node or null, not a number [OK]
Common Mistakes:
  • Assigning primitive values to node children
  • Ignoring type errors in assignments
  • Assuming number can be assigned directly
5. You want to manually create a binary tree with root value 20, left child 10, and right child 30. Which code snippet correctly creates this tree and prints the root, left, and right values in order?
hard
A. const root = new Node(20); root.left = 10; root.right = 30; console.log(root.value, root.left, root.right);
B. const root = new Node(20); root.left = new Node(10); root.right = new Node(30); console.log(root.value, root.left?.value, root.right?.value);
C. const root = new Node(20); root.left = new Node(30); root.right = new Node(10); console.log(root.value, root.left?.value, root.right?.value);
D. const root = new Node(20); root.left = null; root.right = null; console.log(root.left?.value, root.value, root.right?.value);

Solution

  1. Step 1: Create root and children nodes correctly

    Root node with value 20, left child 10, right child 30 must be Node instances.
  2. Step 2: Print values in order root, left, right

    console.log prints root.value, root.left?.value, root.right?.value which outputs 20 10 30.
  3. Final Answer:

    const root = new Node(20); root.left = new Node(10); root.right = new Node(30); console.log(root.value, root.left?.value, root.right?.value); -> Option B
  4. Quick Check:

    Correct node creation and print order = const root = new Node(20); root.left = new Node(10); root.right = new Node(30); console.log(root.value, root.left?.value, root.right?.value); [OK]
Hint: Create nodes with new, assign children as nodes, print values [OK]
Common Mistakes:
  • Assigning numbers directly to children
  • Swapping left and right child values
  • Printing children before root or using null children
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard