Trees - Hierarchies and Decision Paths
What are Trees?
A tree is a hierarchical data structure that consists of nodes connected by edges.
Each tree has a root node at the top, and each node can have zero or more child nodes.
Unlike graphs, trees have no cycles - there's exactly one path between any two nodes.
Trees are everywhere in computer science: file systems, organization charts, decision-making algorithms,
and even in how your browser displays this webpage (the DOM tree)!
Tree Terminology
Understanding these key terms is essential for working with trees:
Root
The topmost node in the tree. It has no parent and is the starting point of the tree.
Parent
A node that has one or more child nodes connected below it.
Child
A node that has a parent node connected above it.
Leaf (Terminal Node)
A node with no children. These are the endpoints of the tree.
Depth
The distance (number of edges) from the root to a specific node.
Height
The maximum depth of the tree - the longest path from root to any leaf.
Subtree
A tree formed by a node and all its descendants.
Sibling
Nodes that share the same parent.
Binary Tree Properties
- Each node has at most 2 children
- Left child < Parent < Right child (BST)
- Perfect binary tree: all levels full
- Complete: all levels full except possibly last
- Height = log₂(n) when balanced
Decision Trees
- Used in machine learning classification
- Each node represents a decision/question
- Leaves represent final classifications
- Easy to interpret and visualize
- Can handle both numeric and categorical data
Tree Traversal Methods
There are three main ways to visit every node in a tree (using binary trees as an example):
In-Order Traversal
- Visit left subtree
- Visit current node
- Visit right subtree
Use: Gets values in sorted order for BST
Pre-Order Traversal
- Visit current node
- Visit left subtree
- Visit right subtree
Use: Copy tree, prefix expressions
Post-Order Traversal
- Visit left subtree
- Visit right subtree
- Visit current node
Use: Delete tree, postfix expressions
Real-World Applications of Trees
File Systems
Your computer organizes folders and files in a tree structure
Organization Charts
Company hierarchies with managers and employees
Decision Making
AI and machine learning classification trees
Game Trees
Representing possible moves in chess, tic-tac-toe, etc.
HTML DOM
Web pages are structured as a tree of elements
Database Indexes
B-trees enable fast data retrieval
Expression Parsing
Mathematical expressions represented as trees
Auto-Complete
Trie trees for efficient word suggestions
Why Trees Are Efficient
Trees provide logarithmic time complexity for many operations when balanced.
Instead of checking every item in a list (O(n)), a balanced binary search tree can find an item
in O(log n) time. This means searching through 1,000,000 items requires only about 20 comparisons!
Example: Finding a file on your computer with 100,000 files would take ages
if you checked each one sequentially. But with a tree structure organized alphabetically,
you can navigate: Computer → Documents → Projects → 2024 → MyFile.txt in just a few steps.