Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSATreesData Structures

DSA Trees — Fundamentals Visualized

Learn tree fundamentals — root, parent, child, leaf, height, hierarchical structure with interactive visualizer and real-world file system examples.

Jul 4, 20264 min read

Introduction

Trees are hierarchical structures — unlike arrays and linked lists that sequence data, trees branch in multiple directions from a single root.

File systems, the DOM, org charts, and decision flows are all trees.

Quick index

#Section
1How trees work
2Interactive visualizer
3Full implementation
4Terminology
5vs linear structures
6Real-life examples

1. How trees work

plaintext
         R
      /  |  \
     A   B   C
    /|      /||\
   D E     F G H I

Unlike linear structures, each node can have multiple children — data branches instead of sequencing.

Back to index


2. Interactive visualizer

Trees — Fundamentals

Root, parent, child, leaf — hierarchical structures that branch instead of sequencing.

R
A
D
E
B
C
F
G
H
I

Root R has three children — trees branch in many directions unlike linear lists.

Tree node

class TreeNode {
  constructor(value) {
    this.value = value
    this.children = []
  }
}

Each node holds a value and a list of child references.

Root

1

Leaves

6

Height

2

Back to index


3. Full implementation

js
class TreeNode {
  constructor(value) {
    this.value = value
    this.children = []
  }
}
 
function buildSampleTree() {
  const root = new TreeNode('R')
  const a = new TreeNode('A')
  const b = new TreeNode('B')
  const c = new TreeNode('C')
  a.children.push(new TreeNode('D'), new TreeNode('E'))
  c.children.push(new TreeNode('F'), new TreeNode('G'), new TreeNode('H'), new TreeNode('I'))
  root.children.push(a, b, c)
  return root
}
 
function find(node, target) {
  if (!node) return null
  if (node.value === target) return node
  for (const child of node.children) {
    const found = find(child, target)
    if (found) return found
  }
  return null
}
 
function height(node) {
  if (!node) return -1
  if (node.children.length === 0) return 0
  return 1 + Math.max(...node.children.map(height))
}
 
function preOrderWalk(node, out = []) {
  if (!node) return out
  out.push(node.value)
  for (const child of node.children) preOrderWalk(child, out)
  return out
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const root = buildSampleTree()
 
find(root, 'G') // → TreeNode { value: 'G' }
find(root, 'Z') // → null
height(root) // → 2
preOrderWalk(root) // → ['R','A','D','E','B','C','F','G','H','I']
root.children.length // → 3

Back to index


4. Terminology

TermMeaning
RootTop node — single entry point
ParentNode with children
ChildNode directly below a parent
LeafNode with no children
HeightLongest path from root to any leaf
DepthEdges from root to a given node

Back to index


5. vs linear structures

StructureAccess patternBest for
ArrayIndex O(1)Sequential data
Linked listHead-to-tailDynamic sequence
TreeRoot-to-leaf pathsHierarchy, branching

Interview Answer

"When would you use a tree over an array?"

When data has natural hierarchy — folders, HTML DOM, company org charts, or categories with subcategories. Trees model parent-child relationships that arrays cannot express without extra indirection.

Back to index


6. Real-life examples

ScenarioTree role
File systemDirectories branch into subdirectories and files
DOMHTML elements nest as parent/child nodes
Org chartCEO → VPs → managers → employees
JSONNested objects form a tree

File system directory tree

Folders branch into subfolders and files — classic tree hierarchy.

/

├── src/

├── public/

└── package.json

Root "/" has children — directories and files.

Real-world implementation

class FileNode {
  constructor(name, isDir = false) {
    this.name = name; this.isDir = isDir; this.children = []
  }
  addChild(node) { if (this.isDir) this.children.push(node) }
  find(name) {
    if (this.name === name) return this
    for (const c of this.children) { const f = c.find(name); if (f) return f }
    return null
  }
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const root = new FileNode("/", true)
root.addChild(new FileNode("src", true))→ 1 child

Side output shows return value after each call — step through to trace execution.

Back to index


Summary

Trees model hierarchical branching — one root, many paths. Next: restrict to two children with Binary Trees.

Next reads: Binary Trees · Pre-order Traversal

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime