Depth-first search (DFS) on a binary search tree visits one branch all the way down before backtracking. In Python you implement it as in-order, pre-order or post-order traversal, either recursively or with an explicit stack. In-order traversal of a valid binary search tree (BST) yields the keys in ascending order.
The three orders differ only in when the current node is processed relative to its children. This article gives a complete, runnable implementation with recursive and iterative versions, generators for lazy iteration, and notes on recursion limits and complexity.
A BST keeps smaller keys in the left subtree and larger keys in the right subtree. A dataclass keeps the node definition short, and an iterative insert avoids deep recursion on skewed input.
The examples below build on this tree, created from the keys 50, 30, 70, 20, 40, 60 and 80.
All three orders use the same recursion; only the position of the "visit" step changes.
| Order | Sequence | Typical use |
|---|---|---|
| In-order | left, node, right | Read keys in sorted order, validate a BST |
| Pre-order | node, left, right | Copy or serialize a tree |
| Post-order | left, right, node | Delete a tree, compute sizes or heights bottom-up |
For the tree above the results are:
Python generators let you traverse lazily instead of building a list, which is useful for large trees or early exit. yield from delegates to the recursive call.
Because these are generators, next(inorder(tree.root)) returns the smallest key without walking the whole tree beyond the leftmost path.
Python limits recursion depth (1,000 frames by default, see sys.getrecursionlimit()). A degenerate BST built from sorted input is a linked list, so a recursive traversal of a few thousand sorted keys raises RecursionError. An explicit stack avoids that limit.
The pre-order version pushes the right child first so the left child is popped first. The post-order version produces node, right, left and then reverses the list, which gives left, right, node.
Every traversal touches each node once, so the time is O(n) for n nodes. The extra space is proportional to the tree height h: O(log n) for a balanced tree and O(n) for a skewed one. The result list itself is O(n) if you collect all keys. Generators avoid that list but still use stack space for the pending recursive calls.
For lookups, a BST search is O(h), not O(n). Use DFS when you need to visit every node, such as exporting, validating or aggregating.
itertools.islice.In-order traversal. It visits the left subtree, then the node, then the right subtree, so keys come out in ascending order.
Pre-order, in-order and post-order are all forms of DFS on a tree. They differ in when the node is visited. Breadth-first (level-order) traversal is the alternative.
The tree is deep, usually because keys were inserted in sorted order. Use the iterative versions, balance the tree, or raise the limit with sys.setrecursionlimit, which risks a crash if set too high.
Use a generator when you may stop early or the tree is large. Use a list when you need the whole result more than once.
DFS on a BST comes in three orders: in-order, pre-order and post-order. Write them recursively with generators for clarity, or with an explicit stack to avoid Python's recursion limit. Each runs in O(n) time, and in-order traversal gives sorted keys.