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.

Rendering diagram...

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.

PropertyDescription
HierarchicalElements are organized in parent-child relationships
AcyclicNo cycles exist; following edges from any node will never lead back to that same node
ConnectedEvery node is reachable from the root; no isolated nodes exist
Single RootExactly one node has no parent and serves as the entry point
Unique PathThere 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?

Rendering diagram...
Rendering diagram...
TermDefinitionExample (from diagram)
RootThe topmost node with no parentNode 50
ParentA node that has childrenNode 30 is parent of 20 and 40
ChildA node that has a parentNodes 20 and 40 are children of 30
SiblingNodes that share the same parentNodes 30 and 70 are siblings
LeafA node with no childrenNodes 10, 40, 60, 90
Internal NodeA node with at least one childNodes 30, 70, 20, 80
AncestorAny node on the path from root to current node50, 30, 20 are ancestors of 10
DescendantAny node in the subtree rooted at current node20, 40, 10 are descendants of 30
SubtreeA tree formed by a node and all its descendantsNode 30 with 20, 40, 10 forms a subtree
EdgeConnection between a parent and childLine 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

A general tree node class with parent tracking and relationship methods

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.
Rendering diagram...
PropertyDefinitionHow to Calculate
DepthNumber of edges from root to the nodeCount edges from root down to node
Height (of node)Number of edges on longest path from node to a leafCount edges from node down to deepest leaf
Height (of tree)Height of the root nodeLongest path from root to any leaf
LevelSame as depth (level 0 = root)Depth of the node
DegreeNumber of children a node hasCount 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

Functions to calculate height, depth, and degree of tree nodes

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.

Rendering diagram...
OperationArrayLinked ListTree (Balanced)
Access by indexO(1)O(n)O(log n)*
SearchO(n) / O(log n)**O(n)O(log n)
InsertO(n)O(1)*O(log n)
DeleteO(n)O(1)*O(log n)
Hierarchy supportNoNoYes
Ordered traversalYesYesYes

* 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 TypeKey PropertyCommon Use Cases
Binary TreeEach node has at most 2 childrenExpression parsing, decision trees
Binary Search Tree (BST)Left < Parent < RightDictionaries, sorted data, databases
AVL TreeSelf-balancing BST (height balanced)When guaranteed O(log n) operations needed
Red-Black TreeSelf-balancing with color propertyMost standard library implementations (Java TreeMap)
B-TreeMultiple keys per node, balancedDatabases, file systems
HeapParent >= children (max) or Parent <= children (min)Priority queues, heap sort
TrieCharacter/prefix-based structureAutocomplete, 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.
Rendering diagram...
Rendering diagram...
DFS: 1 - 2 - 4 - 5 - 3 - 6 - 7
Rendering diagram...
BFS: 1 - 2 - 3 - 4 - 5 - 6 - 7
Rendering diagram...
TraversalOrderData StructureUse Cases
DFS - PreorderRoot, Left, RightStack/RecursionCopying trees, serialization, prefix expressions
DFS - InorderLeft, Root, RightStack/RecursionSorted output from BST, infix expressions
DFS - PostorderLeft, Right, RootStack/RecursionDeleting trees, postfix expressions, dependency resolution
BFS - Level OrderLevel by levelQueueFinding 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

Comparing DFS preorder with BFS level-order traversal

Common Applications of Trees#

Trees are used extensively throughout computer science and software engineering:

ApplicationTree TypeWhy Trees?
File SystemsGeneral TreeHierarchical directory structure (folders contain subfolders)
HTML/DOMGeneral TreeNested elements form a tree structure
Organization ChartsGeneral TreeReporting hierarchy (manager to employees)
Database IndexesB-Tree, B+ TreeFast range queries and ordered access
CompilersAbstract Syntax TreeParse and represent code structure
Decision MakingDecision TreeModel choices and outcomes (ML, game AI)
AutocompleteTriePrefix-based word suggestions
Priority QueuesHeapEfficiently get min/max element
Network RoutingSpanning TreeAvoid loops in broadcast networks
Version ControlDAG (tree-like)Track commit history in Git

When to Use Trees#

Rendering diagram...
Use CaseRecommended TreeReason
Sorted data with fast lookupBinary Search TreeO(log n) average search, in-order gives sorted output
Guaranteed performanceAVL / Red-Black TreeSelf-balancing ensures O(log n) worst case
Frequent min/max accessHeapO(1) access to min or max element
Prefix-based searchTrieEfficient for autocomplete, spell check
Database indexingB-Tree / B+ TreeOptimized for disk reads, handles large datasets
Expression evaluationBinary TreeNatural representation of operator precedence
Parent-child relationshipsGeneral TreeDirect 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.

IssueWhat to CheckCommon AI Mistake
Null checksAlways check node before accessing propertiesAccessing node.left when node is None
Height vs DepthHeight goes up from leaves, depth goes down from rootConfusing the two in calculations
Base caseRecursive functions need proper terminationMissing or wrong base case
Traversal orderDFS variants have specific visit ordersWrong order in preorder/inorder/postorder
Empty treeHandle the case when root is NoneAssuming tree always has at least one node

Spotting AI Tree Bugs

Common errors in AI-generated tree code

Correct tree height and balance check with null handling

Questions to Ask When Reviewing Tree Code#

  1. "What happens with an empty tree?" — The code must handle a null root without crashing.
  2. "What happens at leaf nodes?" — Children are null (or an empty list) and must not be dereferenced.
  3. "Is this calculating height or depth?" — Height goes up from leaves; depth goes down from root.
  4. "What is the traversal order?" — Verify that the preorder, inorder, or postorder logic matches the intended behavior.
  5. "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#

ConceptKey Point
Tree StructureHierarchical, acyclic, connected graph with a single root
Key TermsRoot, parent, child, sibling, leaf, height, depth, level
Height vs DepthHeight: up from leaves. Depth: down from root
Tree TypesBinary, BST, AVL, Heap, Trie, B-Tree - each optimized for specific use cases
TraversalDFS (preorder, inorder, postorder) uses stack; BFS uses queue
When to UseHierarchical 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.