Data Structures · Visualization

BST: Introduction

Binary Search Tree

A binary search tree (BST) is a binary tree that follows one important ordering rule at every node: everything in its left subtree is smaller than the node's value, and everything in its right subtree is larger. Time: O(h) for search/insert/delete, where h is the tree's height (O(log n) balanced, O(n) worst case if skewed). Space: O(n).

Big-O: Search/insert/delete O(log n) on a balanced tree, O(n) worst case.

BST: Introduction

Binary Search Tree

A binary search tree (BST) is a binary tree that follows one important ordering rule at every node: everything in its left subtree is smaller than the node's value, and everything in its right subtree is larger. This rule, applied consistently, means you can search, insert, or delete by always knowing which direction to go, similar to how binary search works on a sorted array.

How BST: Introduction Works

  1. Pick a root value.
  2. Any value less than a node goes into that node's left subtree.
  3. Any value greater than a node goes into that node's right subtree.
  4. Apply this rule recursively at every node as the tree grows.

BST: Introduction Worked Example

Input: Insert 8, 3, 10, 1, 6 into an empty BST

  1. 8 becomes the root
  2. 3 < 8 → goes left of 8
  3. 10 > 8 → goes right of 8
  4. 1 < 8, 1 < 3 → goes left of 3
  5. 6 < 8, 6 > 3 → goes right of 3

Output: 8 / \ 3 10 / \ 1 6

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
BST: Introduction
A binary tree with a rule: every left descendant is smaller than its parent, and every right descendant is bigger — which makes searching fast.
45
20
10
30
40
65
50
60
70
80
Active Result
BST: Introduction
Press ▶ Run to watch it step by step.
Python · Binary Search Trees in Code
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

# Invariant at every node:
#   left.val < node.val < right.val
# example: intro

BST: Introduction Time & Space Complexity

TimeO(h) for search/insert/delete, where h is the tree's height (O(log n) balanced, O(n) worst case if skewed)
SpaceO(n)

When to Use BST: Introduction

  • Maintaining a dynamic, sorted collection that supports fast search, insert, and delete
  • Implementing ordered maps and sets
  • Range queries (finding all values between X and Y)
  • The foundation for self-balancing trees like AVL and Red-Black trees

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.