Introduction to Binary Search Trees

Say you have a plain binary tree full of numbers. Now someone asks you a simple question. β€œIs 57 in this tree?” What do you do? You have no choice. You walk every single node and check each one until you find it or run out. That is slow. If the tree has a million numbers, you might look at a million nodes.

A binary search tree fixes exactly this problem. It keeps the numbers in a smart order so you never have to look at the whole tree. At each step you can throw away half of what is left. That is what we will learn here.

πŸ“– What Is a Binary Search Tree?

A binary search tree is a binary tree that follows one ordering rule. A binary tree is just a tree where every node has at most two children, a left child and a right child. The β€œbinary search” part adds the rule that keeps things sorted.

Here is the rule, and it is the whole idea:

  • For any node, every value in its left subtree is smaller than that node.
  • For any node, every value in its right subtree is larger than that node.
  • This rule holds for every node, not just the top one.

So smaller things go left. Bigger things go right. Always.

Note

A subtree is just a node together with everything hanging below it. So β€œleft subtree” means the left child and all of its children, grandchildren, and so on.

🎯 Why We Need It

Think of a real dictionary. The words are sorted. When you look for β€œmango” you do not read every word from β€œapple” onward. You open the middle, see if you have gone too far or not far enough, and jump. You keep cutting the search area in half. That is fast.

A plain tree is like a dictionary with the words thrown in randomly. To find one word you would have to read them all. A BST is the sorted dictionary. The ordering rule is what lets you jump.

Searching a plain tree means you might check every node. That is O(n). The BST ordering rule lets you skip half the nodes that are left at each step. On average that gives you O(log n). That is the whole win.

🌳 A Valid BST

Let’s look at one. Take the numbers 50, 30, 70, 20, 40, 60, 80. Arranged as a BST, they look like this.

50

30

70

20

40

60

80

Check the rule at the top node, 50. Everything on its left (30, 20, 40) is smaller than 50. Everything on its right (60, 70, 80) is larger than 50. Now check 30. Its left child 20 is smaller, and its right child 40 is larger. Good. Every node passes. So this is a valid BST.

Tip

A quick way to test a BST in your head: if you read the nodes in sorted order (left, then node, then right) you should get the numbers fully sorted. For the tree above that reads 20, 30, 40, 50, 60, 70, 80. Sorted. Nice.

πŸ” How Search Becomes Fast

Now let’s actually search. Say we want to find 60 in that tree. Watch how little we look at.

  • Start at the root, 50. We want 60. Since 60 is greater than 50, the answer can only be on the right. So we ignore the entire left side.
  • Move to 70. Since 60 is less than 70, go left. Ignore 70’s right side.
  • Move to 60. Found it.

We touched three nodes, not seven. Every comparison told us which half to drop. That dropping of half is where the O(log n) speed comes from. With a million sorted values in a balanced BST, you would look at around 20 nodes, not a million.

Compare that to a plain unordered tree. There, 60 could be anywhere, so you would have to keep checking node after node. There is no shortcut. That is the O(n) we wanted to escape.

Note

β€œlog n” here means log base 2. Each step cuts the work in half. The number of times you can halve a million before you reach one is about 20. That is why log n is so much smaller than n.

🧱 What a BST Node Looks Like

A BST is built from nodes. Each node holds a value and two links, one to a left child and one to a right child. A link points to nothing when there is no child there. Here is a tiny node definition in all five languages so the structure is clear.

bst_node.py
# One node of a BST: a value plus a left and right link.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
root = Node(50)
root.left = Node(30)
root.right = Node(70)
print("Root:", root.value)
print("Left child:", root.left.value)
print("Right child:", root.right.value)

The output of the above code will be:

Output
Root: 50
Left child: 30
Right child: 70

⚠️ When a BST Slows Down

The O(log n) speed is the average case. It only holds when the tree is roughly balanced, meaning the left and right sides are about the same height so the tree stays short and bushy.

But what if you insert numbers in already-sorted order, like 10, 20, 30, 40, 50? Each new number is larger than the last, so it always goes right. You end up with a tree that is just one long line going down.

10

20

30

40

50

This is called a skewed tree. It looks more like a linked list than a tree. Now searching has to walk down node by node again. So the speed drops back to O(n), the very thing we were trying to avoid.

Caution

A BST gives fast O(log n) search only when it stays balanced. A skewed BST drops back to O(n). This is exactly why self-balancing trees like AVL trees and red-black trees exist. They reshape the tree as you insert, so it never goes skewed.

Here’s the timing in one table.

Tree shape Search time Why
Balanced BST O(log n) Each step drops half the remaining nodes
Skewed BST O(n) The tree is one long line, so you walk every node
Plain unordered tree O(n) No ordering rule, so no half to drop

🧩 What You’ve Learned

  • βœ… A binary search tree is a binary tree with one ordering rule: left subtree smaller, right subtree larger, for every node.
  • βœ… That rule keeps the values sorted, so reading left-node-right gives you a sorted list.
  • βœ… Searching a plain tree is O(n) because you may check every node.
  • βœ… A BST lets you drop half the remaining nodes at each step, giving O(log n) search on average, just like searching a sorted dictionary.
  • βœ… A BST node holds a value plus a left link and a right link.
  • βœ… If the tree becomes skewed (like inserting sorted numbers), it turns into a line and search slows back to O(n).

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is the ordering rule of a binary search tree?

    Why: In a BST, every value in the left subtree is smaller than the node and every value in the right subtree is larger.

  2. 2

    Why is search in a balanced BST faster than in a plain tree?

    Why: Because of the ordering rule, each comparison tells you which half to drop, giving O(log n) instead of O(n).

  3. 3

    What is the average-case search time in a balanced BST?

    Why: A balanced BST stays short, so search halves the work each step, giving O(log n).

  4. 4

    What happens if you insert already-sorted numbers into a BST?

    Why: Each value goes the same direction, forming one long line, so search degrades to O(n) like a linked list.

πŸš€ What’s Next?

Share & Connect