0
0
DSA Typescriptprogramming~10 mins

Create a Binary Tree Manually in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Create a Binary Tree Manually
Create root node
Assign root value
Create left child node?
NoDone
Yes
Assign left child value
Create right child node?
NoBack to parent
Yes
Assign right child value
Repeat for each node as needed
Start by creating the root node, then manually create and assign left and right child nodes step-by-step until the tree is built.
Execution Sample
DSA Typescript
class Node {
  constructor(public val: number, public left: Node | null = null, public right: Node | null = null) {}
}

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
This code creates a root node with value 1, then manually adds left and right child nodes with values 2 and 3.
Execution Table
StepOperationNode Created/AssignedPointer ChangesVisual State
1Create root nodeNode(1)root -> Node(1)1
2Assign left childNode(2)root.left -> Node(2) 1 / 2
3Assign right childNode(3)root.right -> Node(3) 1 / \ 2 3
4No more nodes to addNoneNo pointer changes 1 / \ 2 3
💡 All nodes manually created and assigned, tree construction complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
rootnullNode(1)Node(1)Node(1)Node(1)
root.leftnullnullNode(2)Node(2)Node(2)
root.rightnullnullnullNode(3)Node(3)
Key Moments - 3 Insights
Why do we assign left and right children after creating the root node?
Because the root node must exist first to attach children. See execution_table steps 1 and 2 where root is created before assigning root.left.
What happens if we forget to assign a child node?
That child pointer remains null, meaning no node exists there. In the table, if root.right was not assigned, it would stay null and the tree would be incomplete.
Why do we manually create each node instead of using a loop?
Manual creation helps understand the structure clearly. Loops are used for larger trees, but here each step shows exactly how nodes connect, as in execution_table rows 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the visual state after step 2?
A1 with right child 3 only
B1 with both children 2 and 3
C1 with left child 2 only
DOnly root node 1
💡 Hint
Check the Visual State column in row for step 2 showing left child assigned only.
At which step is the right child node created and assigned?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the Operation and Pointer Changes columns in execution_table row 3.
If we skip step 3, what would root.right be in variable_tracker final state?
ANode(3)
Bnull
CNode(2)
Dundefined
💡 Hint
See variable_tracker root.right value after step 2 and final if step 3 is skipped.
Concept Snapshot
Create a binary tree manually by:
- Creating the root node first
- Assigning left and right child nodes explicitly
- Each node has value, left, and right pointers
- Null means no child
- Build step-by-step for clear structure
Full Transcript
To create a binary tree manually, start by creating the root node with a value. Then assign its left and right children by creating new nodes and linking them to the root's left and right pointers. Each node holds a value and pointers to left and right children, which can be null if no child exists. This step-by-step manual creation helps visualize the tree structure clearly. The execution table shows each step with the nodes created and how pointers change, while the variable tracker shows the state of root and its children after each step. Key moments clarify why the root must be created first, what happens if children are not assigned, and why manual creation is useful for learning.