0
0
DSA Javascriptprogramming

Binary Tree Node Structure in DSA Javascript

Choose your learning style9 modes available
Mental Model
A binary tree node holds a value and links to up to two child nodes, left and right.
Analogy: Think of a family tree where each person can have up to two children, one on the left and one on the right.
    [Node]
    /    \
 [Left] [Right]

Each node points to two children or null if none.
Dry Run Walkthrough
Input: Create a binary tree node with value 10, left child 5, right child 15
Goal: Build a node that connects to two child nodes correctly
Step 1: Create root node with value 10
[10] -> left: null, right: null
Why: Start with the main node that will hold the value
Step 2: Create left child node with value 5
[10] -> left: [5], right: null
Why: Add left child to root to build the left branch
Step 3: Create right child node with value 15
[10] -> left: [5], right: [15]
Why: Add right child to root to build the right branch
Result:
[10] -> left: [5], right: [15]
Annotated Code
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value; // store the node's value
    this.left = null;   // pointer to left child
    this.right = null;  // pointer to right child
  }
}

// Create nodes
const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);

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

console.log(printNode(root));
this.value = value; // store the node's value
assign the node's value
this.left = null; // pointer to left child
initialize left child pointer to null
this.right = null; // pointer to right child
initialize right child pointer to null
root.left = new TreeNode(5);
link left child node to root
root.right = new TreeNode(15);
link right child node to root
OutputSuccess
[10] -> left: [5], right: [15]
Complexity Analysis
Time: O(1) because creating a node and linking children are constant time operations
Space: O(1) for each node created as it stores fixed number of pointers and a value
vs Alternative: Compared to arrays, nodes use pointers to link children, allowing flexible tree shapes without resizing
Edge Cases
Node with no children
left and right pointers remain null, representing leaf node
DSA Javascript
this.left = null;   // pointer to left child
Node with only left child
right pointer stays null, left points to child node
DSA Javascript
this.right = null;  // pointer to right child
Node with only right child
left pointer stays null, right points to child node
DSA Javascript
this.right = null;  // pointer to right child
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.
Common Mistakes
Mistake: Forgetting to initialize left and right pointers to null
Fix: Always set left and right to null in constructor to avoid undefined references
Mistake: Assigning children values directly instead of creating new nodes
Fix: Create new TreeNode instances for children to maintain structure
Summary
It creates a node with a value and two pointers for left and right children.
Use it when building trees where each node can have up to two children.
Remember each node holds its value and links to children or null if none.