Dataslope logoDataslope

Binary Search Trees

Learn the BST invariant, search, insertion, deletion, validation, and ordered C++ containers

A binary search tree (BST) is a binary tree with an ordering invariant: every value in the left subtree is less than the root, and every value in the right subtree is greater than the root. The rule applies recursively at every node.

Sorted by structure

A BST is not necessarily sorted in memory. It is sorted by the relationship between each node and its subtrees.

At each node, compare the target with the current value and choose one branch.

Code Block
C++ 20 (202002L)

Insert

Insertion follows the same path as search, then attaches the new node at the first empty child position.

Code Block
C++ 20 (202002L)

Min and max

The minimum is found by walking left until no left child remains. The maximum walks right.

Code Block
C++ 20 (202002L)

Delete

Deletion has three cases: leaf, one child, and two children. With two children, copy the in-order successor, then delete that successor from the right subtree.

Code Block
C++ 20 (202002L)

Complexity and balance

Every operation follows one root-to-leaf path, so cost is O(h), where h is height.

ShapeHeightOperation cost
BalancedO(log n)O(log n)
SkewedO(n)O(n)

AVL and Red-Black trees rotate nodes to stay balanced. std::set and std::map are ordered containers typically implemented as red-black trees.

Code Block
C++ 20 (202002L)

In-order traversal gives sorted order

Because all left values come first, then the root, then all right values, in-order traversal of a BST yields sorted values.

Code Block
C++ 20 (202002L)

Validating a BST

Checking only immediate children is wrong because a deeper node can violate an ancestor's allowed range. Correct validation carries lower and upper bounds.

Code Block
C++ 20 (202002L)

Practice: Insert into BST

Challenge
C++ 20 (202002L)
Insert into BST

Implement TreeNode* insert(TreeNode* root, int val). The driver inserts values and prints in-order traversal.

Practice: Search BST

Challenge
C++ 20 (202002L)
Search BST

Implement bool contains(TreeNode* root, int val) using the BST ordering.

Practice: Lowest common ancestor in a BST

Challenge
C++ 20 (202002L)
Lowest common ancestor in a BST

Implement TreeNode* lca(TreeNode* root, int a, int b). Use the BST property to walk toward the split point.

Check your understanding

QuestionSelect one

What is the BST invariant for a node?

All descendants are greater than the node

Values in the left subtree are smaller and values in the right subtree are larger.

Only immediate children must be sorted alphabetically

Every node has exactly two children

QuestionSelect one

What is the cost of search in a BST of height h?

O(h)

O(1) always

O(n log n)

O(n squared)

QuestionSelect one

Why must BST validation track lower and upper bounds?

To count the number of nodes

To make recursion iterative

A node can satisfy its parent but violate an ancestor.

Because leaf nodes are always invalid

On this page