Create a Binary Tree Manually in DSA Javascript - Time & Space Complexity
When we create a binary tree manually, we add nodes one by one. Understanding how long this takes helps us plan for bigger trees.
We want to know how the time to build the tree grows as we add more nodes.
Analyze the time complexity of the following code snippet.
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
// Manually creating nodes and linking them
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
This code creates a binary tree by making nodes and connecting them manually.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating each node and linking it.
- How many times: Once per node added to the tree.
Each new node requires a fixed amount of work to create and link.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 node creations and links |
| 100 | 100 node creations and links |
| 1000 | 1000 node creations and links |
Pattern observation: The work grows directly with the number of nodes added.
Time Complexity: O(n)
This means the time to build the tree grows linearly with the number of nodes.
[X] Wrong: "Creating a tree manually is constant time because we just write code once."
[OK] Correct: Each node creation is a separate step that takes time, so total time grows with how many nodes you add.
Knowing how building a tree scales helps you understand more complex tree operations and shows you can reason about code efficiency.
"What if we used a loop to create and link nodes instead of writing each link manually? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a binary tree is
A binary tree is a structure where each node has up to two children: left and right.Step 2: Purpose of manual creation
Manually creating a binary tree means linking nodes by setting their left and right child references.Final Answer:
To build a tree structure by linking nodes with left and right children -> Option DQuick Check:
Binary tree = linked nodes with left/right children [OK]
- Thinking binary tree sorts data automatically
- Confusing binary tree with arrays or lists
- Assuming binary tree nodes have more than two children
Solution
Step 1: Understand JavaScript object syntax
Objects use curly braces with key-value pairs, e.g., { key: value }.Step 2: Check each option
const node = { value: 5, left: null, right: null }; correctly uses an object with keys value, left, right. Others use array, parentheses, or invalid syntax.Final Answer:
const node = { value: 5, left: null, right: null }; -> Option CQuick Check:
Node as object with keys = const node = { value: 5, left: null, right: null }; [OK]
- Using arrays instead of objects for nodes
- Using parentheses instead of braces
- Trying to declare multiple variables without braces
const root = { value: 10, left: { value: 5, left: null, right: null }, right: { value: 15, left: null, right: null } };
console.log(root.left.value);Solution
Step 1: Understand the tree structure
Root node has value 10, left child with value 5, right child with value 15.Step 2: Access root.left.value
root.left points to node with value 5, so root.left.value is 5.Final Answer:
5 -> Option AQuick Check:
root.left.value = 5 [OK]
- Confusing root value with left child value
- Accessing root.right.value instead
- Expecting undefined if not careful with null checks
const node = { value: 7, left: null, right: null };
node.left.value = 3;Solution
Step 1: Analyze node.left value
node.left is set to null, so it has no properties.Step 2: Assigning to node.left.value
Trying to assign node.left.value = 3 causes error because node.left is null, cannot set property on null.Final Answer:
Cannot assign value to left because left is null -> Option AQuick Check:
Assigning property on null causes error [OK]
- Ignoring that null has no properties
- Assuming automatic node creation on assignment
- Thinking missing semicolon causes error here
Solution
Step 1: Check tree node structure
Each child must be an object with value, left, right keys or null.Step 2: Evaluate each option
const root = { value: 20, left: { value: 10, left: null, right: null }, right: { value: 30, left: null, right: null } }; console.log(root.right.value); correctly creates left and right child nodes as objects with proper keys. const root = { value: 20, left: 10, right: 30 }; console.log(root.right); uses numbers directly, not objects. const root = { value: 20, left: { 10 }, right: { 30 } }; console.log(root.right.value); uses invalid object syntax. const root = { value: 20, left: null, right: null }; root.left.value = 10; root.right.value = 30; console.log(root.right.value); tries to assign properties to null children causing error.Final Answer:
const root = { value: 20, left: { value: 10, left: null, right: null }, right: { value: 30, left: null, right: null } }; console.log(root.right.value); -> Option BQuick Check:
Proper node objects with children = const root = { value: 20, left: { value: 10, left: null, right: null }, right: { value: 30, left: null, right: null } }; console.log(root.right.value); [OK]
- Assigning numbers directly as children
- Using invalid object syntax for children
- Assigning properties to null children
Ready to prove it?
Pass the test and this pattern moves unknown
