DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

🌲 Trees Explained Like You're 5

A family tree with parent and children

Day 36 of 149

πŸ‘‰ Full deep-dive with code examples


The Family Tree

        Grandma
       /       \
     Mom       Uncle
    /   \        |
  You  Sister   Cousin
Enter fullscreen mode Exit fullscreen mode

Each person has a "parent" and maybe "children."

Computer trees work exactly like family trees!


In Code

       CEO
      /    \
    CTO    CFO
   /   \     \
 Dev1  Dev2   Accountant
Enter fullscreen mode Exit fullscreen mode
  • CEO is the root (top)
  • CTO and CFO are children of CEO
  • Dev1 and Dev2 are leaves (no children)

Why Use Trees?

Fast searching!

Binary Search Tree:

  • Every left child is smaller
  • Every right child is bigger
       5
      / \
     3   7
    / \   \
   1   4   9
Enter fullscreen mode Exit fullscreen mode

To find 4: Go right of 3, done! Didn't check every node.


Real Uses

  • File systems (folders contain folders)
  • HTML (elements contain elements)
  • Decision trees in AI

In One Sentence

Trees organize data in a hierarchy of parent-child relationships, like a family tree or folder structure.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)