Recommended resources
The freely available resources this lesson will direct you to.
Hello! In our last lesson, we explored the world of non-linear data structures by defining what a binary tree is and learning its essential terminology. We now have the foundation to explore a special, highly efficient type of binary tree.
This lesson focuses on the Binary Search Tree (BST). We'll build directly on your knowledge of binary trees by adding a specific set of rules that give BSTs their power. Your goal is to understand and be able to describe these properties. This is a crucial step because these properties are what make BSTs one of the most fundamental and efficient data structures for searching and sorting information.
1. The Search for a Better Data Structure
So far, we've worked with arrays and linked lists. They are effective, but each has trade-offs, especially when you need a modifiable collection that you can also search quickly.
- An unsorted array or linked list has a slow search time, , because you might have to check every single element.
- A sorted array is great for searching ( with binary search), but inserting or deleting elements is slow () because you may need to shift many other elements to maintain the sorted order.
This raises the question: can we get the best of both worlds? Can we have a data structure with fast search and fast updates? This is the problem the Binary Search Tree was designed to solve.
Let's watch a video that perfectly frames this challenge and introduces the BST as the solution.
Data structures: Binary Search Tree
The "Data structures: Binary Search Tree" video from mycodeschool provides an excellent motivation for why we need BSTs by analyzing the performance of arrays and linked lists for search, insertion, and deletion operations.
Watch the first part of the video from the problem setup. As you watch, pay close attention to the time complexity analysis for: Searching and modifying an unsorted array. Searching and modifying a sorted array. How a BST promises to achieve an average time complexity of O(\log n) for all these operations.
As the video explained, the BST aims to combine the search performance of a sorted array with more efficient insertion and deletion, avoiding the costly process of shifting elements. How does it achieve this? Through a simple but powerful set of ordering rules.
2. The Core Properties of a Binary Search Tree
A Binary Search Tree is a binary tree that satisfies a specific ordering property, often called the BST invariant. For any given node in the tree:
- All values in the node's left subtree are less than the node's own value.
- All values in the node's right subtree are greater than the node's own value.
- Both the left and right subtrees must also be binary search trees.
This rule must hold true for every single node in the tree, not just the root. For simplicity, we'll assume all values in the tree are unique.

Let's see this property in action with a concrete example.
Data structures: Binary Search Tree
The next segment of the mycodeschool video provides a clear, formal definition of the BST property and walks through a valid example, node by node. It also shows what happens when the property is violated.
Watch the segment from the BST definition. Focus on how the presenter verifies the BST property at the root node and then recursively for its children.
To solidify this concept, let's examine a more detailed textual definition and another visual example.
This article from W3Schools provides a very clear, itemized list of the BST properties.
Read the section titled "Binary Search Trees". Notice how it explicitly states that not just the immediate child, but all of its descendants in a subtree must follow the rule.
Here is a complete example of a valid Binary Search Tree. Take a moment to verify for yourself that every node satisfies the BST property.

3. Test Your Understanding: Is it a BST?
Now, let's put your understanding to the test. The following video will present you with several tree-like structures. Your task is to determine whether each one is a valid Binary Search Tree.
Binary Search Tree Introduction
This video from WilliamFiset presents a series of "puzzles" to help reinforce the BST rules. It's an excellent way to check your understanding.
Watch from the BST game. After each new structure is presented, I encourage you to pause the video and decide for yourself if it's a valid BST before the answer is revealed. Pay close attention to the reasoning for why a structure fails the test.
4. Why the Properties Matter: Performance
Now we return to the "why." How do these properties lead to performance?
When you search for a value in a BST, you start at the root.
- If the value you're looking for is equal to the current node's value, you've found it.
- If the value is less than the current node's value, you know it can only be in the left subtree.
- If the value is greater than the current node's value, you know it can only be in the right subtree.
At each step, you discard about half of the remaining nodes. This process is strikingly similar to the binary search algorithm we studied for sorted arrays. This is where the name "Binary Search Tree" comes from.
However, there's a critical catch. This efficiency is only realized if the tree is balanced.
- A balanced tree is one where the nodes are distributed evenly, and the left and right subtrees of any node have roughly the same height.
- An unbalanced tree is skewed to one side. In the worst case, a BST can become a "degenerate" tree, which is essentially a linked list. Searching a degenerate tree degrades to performance, as you have to traverse node by node.
The next video segment explains this connection between the BST property, the search algorithm, and the importance of balance.
Data structures: Binary Search Tree
This final segment of the mycodeschool video connects all the dots: it shows how searching a BST works and visually demonstrates the performance difference between a balanced and an unbalanced tree.
Watch from the search operation. Focus on: The analogy between BST search and binary search in an array. The visual examples of a balanced tree versus a completely unbalanced, "linked-list-like" tree.
Keeping a BST balanced is an advanced topic (involving structures like AVL trees or Red-Black trees), but for now, the key takeaway is that a BST's excellent performance depends on it being reasonably balanced.
5. An Emergent Property: In-Order Traversal
The strict ordering of a BST gives rise to another useful property. In the previous module, you learned about different tree traversals. If you perform an in-order traversal (Left, Root, Right) on a Binary Search Tree, you will visit the nodes in ascending sorted order.
This is a powerful feature. It provides a simple way to get a sorted list of all elements in the tree and is often used to verify if a given binary tree is a valid BST.
This section of the W3Schools article explains this elegant property.
Read the short section titled "Traversal of a Binary Search Tree". It concisely explains how in-order traversal can be used to confirm that a tree is a valid BST.
Conclusion
In this lesson, we added a crucial layer of understanding on top of our knowledge of binary trees. You've learned the specific rules that define a Binary Search Tree and, more importantly, why those rules are so powerful.
Key Takeaways:
- A Binary Search Tree (BST) is a binary tree where for any node, all values in the left subtree are smaller, and all values in the right subtree are larger.
- This property must hold true for all nodes in the tree.
- The BST property allows for efficient search, insertion, and deletion, with an average-case time complexity of .
- This efficiency is dependent on the tree being balanced. An unbalanced or degenerate BST can have a worst-case time complexity of , similar to a linked list.
- A key side-effect of the BST property is that an in-order traversal of the tree will always produce the elements in sorted order.
Preview of the Next Lesson:
Understanding the properties of a BST is the first step. The next step is to get hands-on. In our next lesson, we will transition from theory to practice by implementing the core insert and search operations for a BST in C. You will write the code that brings these properties to life.