0
0
DSA Typescriptprogramming

Binary Tree Node Structure in DSA Typescript

Choose your learning style9 modes available
Mental Model
A binary tree node holds a value and links to two child nodes, one on the left and one on the right.
Analogy: Think of a family tree where each person can have up to two children, one on the left side and one on the right side.
    [Node]
    /    \
 [Left] [Right]
   null   null
Dry Run Walkthrough
Input: Create a binary tree node with value 5, left child 3, and right child 7
Goal: Build a node that connects to two child nodes correctly
Step 1: Create root node with value 5
[5] -> left: null, right: null
Why: Start with the main node that will hold the value
Step 2: Create left child node with value 3
[5] -> left: [3], right: null
Why: Add left child to hold smaller value
Step 3: Create right child node with value 7
[5] -> left: [3], right: [7]
Why: Add right child to hold larger value
Result:
[5] -> left: [3], right: [7]
Annotated Code
DSA Typescript
class TreeNode {
  value: number;
  left: TreeNode | null;
  right: TreeNode | null;

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

// Create root node
const root = new TreeNode(5);
// Create left child
root.left = new TreeNode(3);
// Create right child
root.right = new TreeNode(7);

// Function to print tree node and its children
function printNode(node: TreeNode | null): string {
  if (!node) return "null";
  const leftVal = node.left ? node.left.value.toString() : "null";
  const rightVal = node.right ? node.right.value.toString() : "null";
  return `[${node.value}] -> left: [${leftVal}], right: [${rightVal}]`;
}

console.log(printNode(root));
this.left = null; this.right = null;
Initialize left and right child pointers to null
root.left = new TreeNode(3);
Assign left child node with value 3
root.right = new TreeNode(7);
Assign right child node with value 7
OutputSuccess
[5] -> left: [3], right: [7]
Complexity Analysis
Time: O(1) because creating a node and assigning children is a constant time operation
Space: O(1) for each node created as it stores fixed number of references and a value
vs Alternative: Compared to arrays, nodes use pointers to link children which allows dynamic tree growth without resizing
Edge Cases
Node with no children
Both left and right pointers remain null indicating leaf node
DSA Typescript
this.left = null;
this.right = null;
Node with only left child
Right pointer stays null, left points to child node
DSA Typescript
root.left = new TreeNode(3);
Node with only right child
Left pointer stays null, right points to child node
DSA Typescript
root.right = new TreeNode(7);
When to Use This Pattern
When you need to represent hierarchical data with up to two children per item, use the binary tree node structure to link nodes simply and clearly.
Common Mistakes
Mistake: Forgetting to initialize left and right pointers to null
Fix: Always set left and right to null in the constructor to avoid undefined references
Mistake: Assigning children without creating new nodes
Fix: Create new TreeNode instances before assigning to left or right pointers
Summary
It creates a node that holds a value and links to two child nodes.
Use it when building trees where each node can have up to two children.
Remember each node has two pointers that start as null and can link to child nodes.