What is a Tree?
A tree is a hierarchical data structure consisting of nodes connected by edges. Unlike linear structures (arrays, linked lists), trees represent hierarchical relationships.
The Family Tree Analogy: Think of a family tree:
- •The oldest ancestor is the root
- •Each person (node) can have children
- •Nodes with no children are leaves
- •Every node has exactly one parent (except root)
Visual Representation:
Why Trees Matter:
- •File systems (folders contain files/folders)
- •HTML/DOM (elements contain elements)
- •Organization charts
- •Decision trees
- •Expression parsing
- •Database indexes (B-trees)
Binary Trees: Each node has at most 2 children (left and right). Most interview questions focus on binary trees.