Binary Trees and Search Trees
Explore branching nodes, traversal orders, and the ordering rule of a binary search tree.
binary tree, binary search tree, traversal, preorder, inorder, postorderA branching family of nodes
Imagine a family chart drawn downward. Each person can have smaller branches below them. A binary tree is made of nodes with at most two children: left and right. The top node is the root. A node without children is a leaf. An empty tree has no root.
5
/ \
2 8
/ \ \
1 3 9
The left subtree of 5 contains 2, 1, and 3. A subtree includes a node and everything beneath it. Each node other than the root has one parent.
Why use it and where it helps
Trees describe nested choices and hierarchical relationships. A binary tree alone does not require the values to be ordered. A binary search tree (BST) adds an ordering rule: for every node, all values in its left subtree are smaller and all values in its right subtree are larger. Our example uses distinct numbers; other implementations must define how to handle duplicates.
Searching for 3 starts at 5, moves left to 2, then right to 3. The rule lets us discard an entire subtree at each step. A tree is not automatically balanced, however: inserting already sorted values into a simple BST can produce a chain.
Three ways to visit nodes
Traversal means visiting nodes in a chosen order. For the tree above:
| Order | Rule | Visits |
|---|---|---|
| Preorder | node, left, right | 5, 2, 1, 3, 8, 9 |
| Inorder | left, node, right | 1, 2, 3, 5, 8, 9 |
| Postorder | left, right, node | 1, 3, 2, 9, 8, 5 |
Inorder gives sorted values for this BST because its ordering rule holds at every node. It does not sort an arbitrary binary tree.
See the reasoning
Follow the example one step at a time
Find 3 in the lesson's binary search tree. This is a prepared teaching demonstration, not live code execution.
Start at the root
[5]
/ \
2 8
/ \ \
1 3 9Compare 3 with 5. The square brackets mark the current node.
Python illustration
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def inorder(node):
if node is None:
return []
return inorder(node.left) + [node.value] + inorder(node.right)
def contains(root, target):
while root is not None:
if target == root.value:
return True
root = root.left if target < root.value else root.right
return False
tree = Node(5, Node(2, Node(1), Node(3)), Node(8, None, Node(9)))
print(inorder(tree))
print(contains(tree, 3), contains(tree, 7))
print(inorder(None))
Prepared output — not live execution:
[1, 2, 3, 5, 8, 9]
True False
[]
The search function assumes the BST ordering rule and comparable values.
None represents an absent child. The example creates the tree by hand; it
does not implement insertion or deletion.
Time, space, and common mistakes
Search follows one path: O(h) time for tree height h, which can be O(log n)
for a balanced tree or O(n) for a chain of n nodes. A traversal visits all
nodes, but the list concatenations in this teaching implementation can copy
values repeatedly: O(n log n) for balanced trees and O(n²) for a chain.
An accumulator can visit in O(n) time. This code returns O(n) values and
uses O(h) recursion depth; Python's recursion limit matters for deep trees.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
