Binary Search Tree

What is it?

A binary search tree (BST) is a data structure where each node has at most two children. Values in the left subtree are smaller than the node, and values in the right subtree are larger.

Example of a binary search tree

How to search

  1. Start at the root.
  2. If the target is smaller than the current node, go left.
  3. If the target is larger, go right.
  4. Stop when you find the value or reach an empty spot.

Why use it?

Search, insert, and delete take O(h) time, where h is the height of the tree. A balanced tree gives O(log n).

More info: Binary search tree on Wikipedia