Create a Binary Tree Manually in DSA Typescript - Time & Space Complexity
When we create a binary tree manually by adding nodes one by one, it is important to understand how the time needed grows as the tree gets bigger.
We want to know how long it takes to build the tree as we add more nodes.
Analyze the time complexity of the following code snippet.
class TreeNode {
value: number;
left: TreeNode | null = null;
right: TreeNode | null = null;
constructor(value: number) {
this.value = value;
}
}
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
This code creates a binary tree by manually assigning child nodes to the root and its children.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and linking each node manually.
- How many times: Once per node added, no loops or recursion here.
Each new node requires one step to create and link it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The time grows directly with the number of nodes added.
Time Complexity: O(n)
This means the time to create the tree grows linearly with the number of nodes you add.
[X] Wrong: "Creating a binary tree manually is always very slow because it involves complex operations."
[OK] Correct: Actually, each node is created and linked in a simple step, so the time grows only as you add more nodes, not more complex than that.
Understanding how manual tree creation scales helps you explain basic data structure building blocks clearly and confidently in interviews.
"What if we added a loop to create nodes instead of manual assignments? How would the time complexity change?"
Practice
Solution
Step 1: Understand binary tree structure
A binary tree consists of nodes where each node has at most two children, left and right.Step 2: Purpose of manual creation
Manually creating a binary tree helps visualize and understand how nodes connect to form the tree.Final Answer:
To understand how nodes connect and form the tree structure -> Option CQuick Check:
Manual creation = Understand tree structure [OK]
- Thinking it automatically sorts data
- Confusing binary tree with linked list
- Assuming no objects or classes are needed
Solution
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.Step 2: Verify constructor initialization
Constructor must initialize value and set left and right to null initially.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 AQuick 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]
- Using number type for left/right instead of Node or null
- Not initializing left and right to null
- Omitting constructor parameters
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);
Solution
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.Step 2: Access left child's value
root.left?.value accesses left node's value safely, which is 5.Final Answer:
5 -> Option AQuick Check:
root.left.value = 5 [OK]
- Confusing root value with left child value
- Expecting right child value instead
- Not using optional chaining and getting error
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);
Solution
Step 1: Check type of root.left assignment
root.left is declared as Node or null, but assigned number 5 which is invalid.Step 2: Confirm correct assignment type
root.left should be assigned a Node instance or null, not a number.Final Answer:
Assigning number 5 to root.left instead of a Node or null -> Option DQuick Check:
Type mismatch in root.left assignment = Assigning number 5 to root.left instead of a Node or null [OK]
- Assigning primitive values to node children
- Ignoring type errors in assignments
- Assuming number can be assigned directly
Solution
Step 1: Create root and children nodes correctly
Root node with value 20, left child 10, right child 30 must be Node instances.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.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 BQuick 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]
- Assigning numbers directly to children
- Swapping left and right child values
- Printing children before root or using null children
Ready to prove it?
Pass the test and this pattern moves unknown
