Trees and Binary Search Trees, One Node at a Time

Understand roots, leaves, traversals, and the ordering rule that makes a balanced search tree useful.

By Rishipal Singh · · 6 min read

A binary search tree keeps smaller values left and larger values right.
Visual guide: A binary search tree keeps smaller values left and larger values right.

A tree represents a hierarchy. A family tree, a folder system, and an organization chart all have a starting point with branches below it.

Useful tree vocabulary

The top node is the root. A node's children are below it; nodes with no children are leaves. The number of edges from the root to a node is its depth. The height is the length of the longest path from a node down to a leaf.

A binary tree allows each node to have at most two children. In a binary search tree, values in the left subtree are smaller than the node and values in the right subtree are larger (or follow a consistent duplicate rule).

Searching depends on balance

At each step in a binary search tree, compare the target with the current node and continue into at most one subtree. A well-balanced tree with n nodes has height around log₂(n), so search is O(log n). If values arrive in sorted order and the tree is not self-balancing, it can become a chain with height n and search becomes O(n).

Walk the tree

Depth-first traversals visit nodes by going down branches; breadth-first traversal visits level by level. An in-order traversal of a binary search tree visits values in sorted order.

Trees are useful whenever data is naturally hierarchical or needs ordered lookup. In production, balanced variants such as AVL trees and red-black trees preserve predictable height as values change.

Practice: follow a search path

In a binary search tree with root 8, its left child 4, and right child 12, where do you search next for 6?

Reveal the explanation

Compare 6 with 8 and go left. Then compare with 4; since 6 is larger, continue to the right child of 4.