0
0
DSA Javascriptprogramming~10 mins

Tree Terminology Root Leaf Height Depth Level in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Tree Terminology Root Leaf Height Depth Level
Start at Root Node
Check if Node has Children
Go to Child
Repeat for all Children
Calculate Height and Depth
Assign Level Based on Depth
Start from the root node, explore children to find leaves, then calculate height, depth, and level for each node.
Execution Sample
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.children = [];
  }
}
Defines a simple tree node with a value and list of children.
Execution Table
StepCurrent NodeChildren Present?ActionHeightDepthLevel
1A (Root)YesGo to children B and C?00
2BYesGo to children D and E?11
3DNoLeaf node found022
4ENoLeaf node found022
5BAfter children processedCalculate height = max(0,0)+1=1111
6CNoLeaf node found011
7AAfter children processedCalculate height = max(1,0)+1=2200
8End-All nodes processed---
💡 All nodes visited, heights and depths calculated, levels assigned based on depth.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
Current Node-ABDEBCA-
Height-??00102-
Depth-0122110-
Level-0122110-
Key Moments - 3 Insights
Why is the root node's depth 0 but its height 2?
Depth counts edges from root down, so root is 0. Height counts edges down to farthest leaf, so root height is max child height + 1 (2 here). See execution_table rows 1 and 7.
Why do leaf nodes have height 0?
Leaf nodes have no children, so height is 0 by definition. This is shown in execution_table rows 3, 4, and 6.
How is level related to depth?
Level is the same as depth here, counting edges from root. Both are assigned together as shown in execution_table columns Depth and Level.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the height of node D?
A0
B1
C2
DUndefined
💡 Hint
Check the Height column at Step 3 in execution_table.
At which step does the condition 'Children Present?' become No for node C?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the Current Node and Children Present? columns in execution_table.
If node B had one more child, how would the height of node A change?
AIt would decrease
BIt would increase by 1
CIt would stay the same
DCannot determine
💡 Hint
Height depends on the maximum child height, see calculation in Step 7 of execution_table.
Concept Snapshot
Tree Terminology Quick Reference:
- Root: Top node, depth 0
- Leaf: Node with no children, height 0
- Depth: Edges from root to node
- Height: Edges from node to deepest leaf
- Level: Same as depth here
- Height(node) = max height(children) + 1
Full Transcript
This visual execution traces tree terminology concepts. Starting at the root node A, we check if it has children. Since it does, we move to children B and C. Node B has children D and E, which are leaves with height 0. We calculate heights bottom-up: leaves have height 0, their parents have height 1, and root has height 2. Depth counts edges from root down, so root is 0, its children 1, and leaves 2. Level matches depth. This helps understand how root, leaf, height, depth, and level relate in a tree.