Beginner Topic 17 of 52 Data Structures
Binary Search Trees
Ordered binary trees with left smaller and right larger keys.
How it works
A binary search tree keeps every key in the left subtree smaller than the node and every key in the right subtree larger. Search, insert and delete follow one root-to-leaf path, so each costs O(h) where h is the height. With random insertions the height is about log n, but sorted insertions produce a chain. Deletion handles three cases: leaf, one child and two children.
Where it is used
Ordered maps and sets, range queries, in-order iteration of sorted data and implementing symbol tables in interpreters use BSTs. They are the base for AVL and red-black trees.
Common pitfall
Without balancing, sorted or nearly sorted input makes the tree degenerate and operations O(n). Duplicate keys need a defined policy. Deleting a node with two children is error-prone: replace it with the inorder successor. Use a self-balancing tree for guaranteed performance.
Where it fits in the course
Binary Search Trees is topic 17 of 52 in Data Structures, rated Beginner. It follows Tree Traversals. The next topic is AVL Trees.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
- Explore the Linear vs Binary Search visualizer: See how halving beats scanning.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Binary Search Trees?
Where do people use Binary Search Trees?
What is the most common mistake with Binary Search Trees?
What should I learn before this topic?
How can I practice it?
Is Binary Search Trees hard to learn?
How long does it take to learn Binary Search Trees?
Which topics are related to Binary Search Trees?
What should I study after Binary Search Trees?
Related topics
- Tree Traversals (Data Structures)
- AVL Trees (Data Structures)
- Binary Trees (Data Structures)
- Red-Black Trees (Data Structures)
- Binary Search (Algorithms)
- Binary Number System (Computer Architecture & Logic)
- Linear Search (Algorithms)