← Back to writing
Algorithm & Data Structure

Tree Traversal Part 1: Overview

Overview

Tree traversal means visiting every node in a tree-shaped data structure exactly once according to a specific rule. Unlike linear data structures such as lists and arrays, trees have a hierarchical structure, and a single node can have multiple child nodes. Because of that, several traversal patterns can be defined depending on the root, order, and timing of visits.

There are two major algorithms for traversing tree structures: BFS, or breadth-first search, and DFS, or depth-first search.

Tree Traversal Techniques木の走査技法の分類図。DFSとBFSの2種類に大別され、DFSはInorder・Preorder・Postorderの3種に分かれる。Tree Traversal TechniquesDepth First Traversal(DFS)Breadth First Traversal(Level Order Traversalor BFS)Inorder TraversalPreorder TraversalPostorder Traversal

BFS (Breadth-First Search)

BFS uses a queue and visits all nodes at the same depth before moving to the next depth.

Visit order: A -> B -> C -> D -> E -> F -> G
BFS: Layer-by-Layer Traversal AnimationAnimated visualization showing BFS visiting nodes layer by layer with queue state transitionsLayer 0 (start)Layer 1 (distance 1)Layer 2 (distance 2)Sstart①Avisit 2②Bvisit 3③Cvisit 4④Dvisit 5⑤Evisit 6⑥Fvisit 7⑦Gvisit 8⑧Hvisit 9⑨Queue (FIFO)Init: [S]→Pop S: [A,B,C]→Pop A: [B,C,D,E]→Pop B: [C,D,E,F]Each layer is fully explored before moving to the next

It is well suited for problems that require the shortest path, such as maze solving.

DFS (Depth-First Search)

DFS uses a stack, or recursion, and follows a path as deeply as possible before backtracking. There are three common visit timings.

  • Pre-order: self -> left -> right
  • In-order: left -> self -> right, often used to output a binary search tree in ascending order
  • Post-order: left -> right -> self, useful for tasks such as aggregating file sizes
Tree Traversal Orders: Pre-Order, In-Order, Post-OrderComparison of three binary tree traversal methods with animated diagrams and pseudocode.Pre-Orderroot → left → rightR①L②R③function pre-order(T)if !ISEMPTY(T) then① visit(root(T))pre-order(left(T))pre-order(right(T))end if / end functionIn-Orderleft → root → rightL①R②R③function in-order(T)if !ISEMPTY(T) thenin-order(left(T))② visit(root(T))in-order(right(T))end if / end functionPost-Orderleft → right → rootL①R②R③function post-order(T)if !ISEMPTY(T) thenpost-order(left(T))post-order(right(T))③ visit(root(T))end if / end functionTraversal order comparison on the same treeRoot nodeLeft subtreeRight subtreevisit( ) callExample: root = A, left = B, right = CMethodVisit orderCommon use casePre-OrderA → B → CTree copy / serializationIn-OrderB → A → CSorted output from BSTPost-OrderB → C → AMemory free / dependency resolve

Complexity

If V is the number of nodes, E is the number of edges, and H is the height of the tree, both BFS and DFS have a time complexity of O(V + E). Their space complexity differs: BFS is O(V), while DFS is O(H).

AlgorithmTime ComplexitySpace Complexity
BFSO(V + E)O(V)
DFSO(V + E)O(H), where H is the height of the tree

Interactive Demo

Choose an algorithm and press the "Next" button to see the order in which nodes are visited step by step.

ABCDEFG
Press Next to start