Trees
A tree is a hierarchical data structure made up of nodes connected by edges. Unlike linear structures such as arrays or linked lists—where elements are arranged one after another—trees organize data in parent-child relationships, forming a branching structure that mirrors how we naturally represent hierarchies.
Consider a company's organization chart, a file system on your computer, or the nested elements of an HTML document. Each of these is a tree: there is one top-level entry point, and every element belongs to exactly one position in the hierarchy.
Key Properties of Trees#
A tree must satisfy all of the following properties. If any of them is violated, the structure is no longer a tree—it may instead be a general graph, a forest (multiple disconnected trees), or contain a cycle.
| Property | Description |
|---|---|
| Hierarchical | Elements are organized in parent-child relationships |
| Acyclic | No cycles exist; following edges from any node will never lead back to that same node |
| Connected | Every node is reachable from the root; no isolated nodes exist |
| Single Root | Exactly one node has no parent and serves as the entry point |
| Unique Path | There is exactly one path between any two nodes in the tree |
Tree Terminology#
Tree terminology can feel abstract at first. The key is to anchor each term to a concrete position in the tree: where is the node relative to the root, and does it have children?
| Term | Definition | Example (from diagram) |
|---|---|---|
| Root | The topmost node with no parent | Node 50 |
| Parent | A node that has children | Node 30 is parent of 20 and 40 |
| Child | A node that has a parent | Nodes 20 and 40 are children of 30 |
| Sibling | Nodes that share the same parent | Nodes 30 and 70 are siblings |
| Leaf | A node with no children | Nodes 10, 40, 60, 90 |
| Internal Node | A node with at least one child | Nodes 30, 70, 20, 80 |
| Ancestor | Any node on the path from root to current node | 50, 30, 20 are ancestors of 10 |
| Descendant | Any node in the subtree rooted at current node | 20, 40, 10 are descendants of 30 |
| Subtree | A tree formed by a node and all its descendants | Node 30 with 20, 40, 10 forms a subtree |
| Edge | Connection between a parent and child | Line connecting 50 to 30 |
Two terms worth extra attention:
- Ancestors form the path from a node back up to the root. Every node you pass through while walking upward is an ancestor. The root is an ancestor of every node in the tree.
- Descendants are all nodes reachable by walking downward from a given node—its children, their children, and so on. Together with the node itself, they form a subtree.
Understanding Tree Relationships
Navigate and identify relationships between nodes in a tree
Tree Properties: Height, Depth, and Level#
These three measurements describe the position and size of nodes within a tree. They are frequently confused with one another—especially height and depth—so it is worth being precise:
- Depth answers: "How far is this node from the root?" It is measured by counting edges going downward from the root. The root itself has depth 0.
- Height (of a node) answers: "How far down does this node's subtree extend?" It is measured by counting edges going upward from the deepest leaf in that subtree. A leaf node has height 0.
- Height (of a tree) is the height of the root node—equivalently, the number of edges on the longest root-to-leaf path.
- Level is another name for depth. A node at level 0 is the root, level 1 contains the root's children, and so on.
| Property | Definition | How to Calculate |
|---|---|---|
| Depth | Number of edges from root to the node | Count edges from root down to node |
| Height (of node) | Number of edges on longest path from node to a leaf | Count edges from node down to deepest leaf |
| Height (of tree) | Height of the root node | Longest path from root to any leaf |
| Level | Same as depth (level 0 = root) | Depth of the node |
| Degree | Number of children a node has | Count immediate children |
Convention note: Some implementations measure height by number of nodes on the path rather than number of edges. Under that convention, a single-node tree has height 1 instead of 0. Always verify which convention a codebase uses before comparing height values across functions.
Calculating Tree Properties
Compute height, depth, and other tree metrics
Trees vs Linear Structures#
When choosing a data structure, consider whether your data has a natural hierarchy and what operations you perform most often. Trees offer significant performance advantages for search and hierarchical access, at the cost of higher memory usage and implementation complexity.
| Operation | Array | Linked List | Tree (Balanced) |
|---|---|---|---|
| Access by index | O(1) | O(n) | O(log n)* |
| Search | O(n) / O(log n)** | O(n) | O(log n) |
| Insert | O(n) | O(1)* | O(log n) |
| Delete | O(n) | O(1)* | O(log n) |
| Hierarchy support | No | No | Yes |
| Ordered traversal | Yes | Yes | Yes |
* For indexed trees like B-trees. ** Sorted array with binary search. *** After finding position.
The O(log n) performance of balanced trees comes from the fact that each comparison eliminates roughly half of the remaining nodes—the same principle as binary search. However, this guarantee only holds when the tree remains balanced. An unbalanced tree can degrade to O(n) in the worst case, which is why self-balancing variants (AVL, Red-Black) exist.
Types of Trees#
Each tree variant is designed to make a specific set of operations fast. Understanding which type to use—and why—matters more for practical development than memorizing their implementation details.
| Tree Type | Key Property | Common Use Cases |
|---|---|---|
| Binary Tree | Each node has at most 2 children | Expression parsing, decision trees |
| Binary Search Tree (BST) | Left < Parent < Right | Dictionaries, sorted data, databases |
| AVL Tree | Self-balancing BST (height balanced) | When guaranteed O(log n) operations needed |
| Red-Black Tree | Self-balancing with color property | Most standard library implementations (Java TreeMap) |
| B-Tree | Multiple keys per node, balanced | Databases, file systems |
| Heap | Parent >= children (max) or Parent <= children (min) | Priority queues, heap sort |
| Trie | Character/prefix-based structure | Autocomplete, spell checkers, IP routing |
Tree Traversal Concepts#
Traversing a tree means visiting every node exactly once in a defined order. The order you choose determines what information you can extract efficiently—for example, traversing a binary search tree in a specific order yields sorted output, while another order is ideal for copying or deleting the tree.
There are two fundamental strategies:
- Depth-First Search (DFS): Explore as far down one branch as possible before backtracking. Naturally implemented with recursion (using the call stack implicitly) or an explicit stack. DFS has three variants—preorder, inorder, and postorder—that differ only in when the current node is visited relative to its children.
- Breadth-First Search (BFS): Visit all nodes at the current level before moving to the next. Requires an explicit queue to track which nodes to process next.
| Traversal | Order | Data Structure | Use Cases |
|---|---|---|---|
| DFS - Preorder | Root, Left, Right | Stack/Recursion | Copying trees, serialization, prefix expressions |
| DFS - Inorder | Left, Root, Right | Stack/Recursion | Sorted output from BST, infix expressions |
| DFS - Postorder | Left, Right, Root | Stack/Recursion | Deleting trees, postfix expressions, dependency resolution |
| BFS - Level Order | Level by level | Queue | Finding shortest paths, level-based operations |
The choice of data structure for each strategy follows from the algorithm's behavior: DFS must remember where to backtrack when it finishes a branch—a stack (last-in, first-out) handles this naturally. BFS must process nodes in the order they were discovered—a queue (first-in, first-out) ensures that all nodes at one level are visited before any node at the next level.
Traversal Order Differences
See how different traversals visit nodes in different orders
Common Applications of Trees#
Trees are used extensively throughout computer science and software engineering:
| Application | Tree Type | Why Trees? |
|---|---|---|
| File Systems | General Tree | Hierarchical directory structure (folders contain subfolders) |
| HTML/DOM | General Tree | Nested elements form a tree structure |
| Organization Charts | General Tree | Reporting hierarchy (manager to employees) |
| Database Indexes | B-Tree, B+ Tree | Fast range queries and ordered access |
| Compilers | Abstract Syntax Tree | Parse and represent code structure |
| Decision Making | Decision Tree | Model choices and outcomes (ML, game AI) |
| Autocomplete | Trie | Prefix-based word suggestions |
| Priority Queues | Heap | Efficiently get min/max element |
| Network Routing | Spanning Tree | Avoid loops in broadcast networks |
| Version Control | DAG (tree-like) | Track commit history in Git |
When to Use Trees#
| Use Case | Recommended Tree | Reason |
|---|---|---|
| Sorted data with fast lookup | Binary Search Tree | O(log n) average search, in-order gives sorted output |
| Guaranteed performance | AVL / Red-Black Tree | Self-balancing ensures O(log n) worst case |
| Frequent min/max access | Heap | O(1) access to min or max element |
| Prefix-based search | Trie | Efficient for autocomplete, spell check |
| Database indexing | B-Tree / B+ Tree | Optimized for disk reads, handles large datasets |
| Expression evaluation | Binary Tree | Natural representation of operator precedence |
| Parent-child relationships | General Tree | Direct modeling of hierarchical data |
Reviewing AI-Generated Code#
Tree algorithms involve recursive thinking, null pointer handling, and careful base-case design. These are precisely the areas where AI-generated code most often contains subtle errors. A function may look correct at a glance but silently produce wrong results on edge cases—empty trees, single-node trees, or trees that are heavily skewed to one side. Always trace through these cases manually before trusting AI output.
| Issue | What to Check | Common AI Mistake |
|---|---|---|
| Null checks | Always check node before accessing properties | Accessing node.left when node is None |
| Height vs Depth | Height goes up from leaves, depth goes down from root | Confusing the two in calculations |
| Base case | Recursive functions need proper termination | Missing or wrong base case |
| Traversal order | DFS variants have specific visit orders | Wrong order in preorder/inorder/postorder |
| Empty tree | Handle the case when root is None | Assuming tree always has at least one node |
Spotting AI Tree Bugs
Common errors in AI-generated tree code
Questions to Ask When Reviewing Tree Code#
- "What happens with an empty tree?" — The code must handle a null root without crashing.
- "What happens at leaf nodes?" — Children are null (or an empty list) and must not be dereferenced.
- "Is this calculating height or depth?" — Height goes up from leaves; depth goes down from root.
- "What is the traversal order?" — Verify that the preorder, inorder, or postorder logic matches the intended behavior.
- "Which height convention does this use?" — Confirm whether height is measured in edges (leaf = 0) or in nodes (leaf = 1), and check that it is consistent across all functions.
Summary#
| Concept | Key Point |
|---|---|
| Tree Structure | Hierarchical, acyclic, connected graph with a single root |
| Key Terms | Root, parent, child, sibling, leaf, height, depth, level |
| Height vs Depth | Height: up from leaves. Depth: down from root |
| Tree Types | Binary, BST, AVL, Heap, Trie, B-Tree - each optimized for specific use cases |
| Traversal | DFS (preorder, inorder, postorder) uses stack; BFS uses queue |
| When to Use | Hierarchical data, fast search, sorted access, prefix matching |
Now that you understand tree fundamentals, you are ready to explore specific tree types. Continue with Binary Trees to learn about the most common tree structure and its traversal algorithms.