Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 50

Properties of a Binary Search Tree

Recommended resources

Hello! Welcome to our new module, Binary Search Trees (BSTs).

In the previous module, we explored binary trees, their traversals, and how to solve problems like finding the Lowest Common Ancestor (LCA). You learned that in a general binary tree, there's no specific rule about how values are organized. This means that to find a particular value, you might have to look at every single node, resulting in an search time.

Today, we'll introduce a special kind of binary tree that solves this problem. This lesson addresses the learning outcome: Describe the properties of a Binary Search Tree (BST). We'll explore the simple but powerful rules that define a BST and make it one of the most fundamental data structures for efficient searching, insertion, and deletion.

1. From Binary Tree to Binary Search Tree

A Binary Search Tree is, first and foremost, a binary tree. This means every node has at most two children. However, a BST adds a crucial constraint that organizes the data in a predictable way.

To get a clear initial picture of this distinction and the core idea behind a BST, let's start with a short video.

Binary Tree Part 1 | BST | Binary Search Tree - Data Structures & Algorithms Tutorials In Python #10

This video from the 'codebasics' channel, 'Binary Tree Part 1 | BST | Binary Search Tree', will help you quickly understand the difference between a general binary tree and a BST, and introduce the main ordering property.

Watch from 01:26 to 03:15. Focus on how a BST is a more constrained version of a binary tree and what the specific rule is regarding the values of left and right children.


2. The Core Properties of a BST

As you saw in the video, the "search" in Binary Search Tree comes from a specific set of ordering rules. These properties must hold true for every single node in the tree.

Formally, a Binary Search Tree has the following properties:

  1. The Left Subtree Property: For any given node, all values in its left subtree are strictly less than the node's own value.
  2. The Right Subtree Property: For any given node, all values in its right subtree are strictly greater than the node's own value.
  3. The BST Property for Subtrees: Both the left and right subtrees must also be binary search trees. This recursive definition ensures the ordering rules apply throughout the entire tree.

Additionally, it's a common convention that BSTs do not contain duplicate values. This simplifies many operations, and we will follow this convention.

Let's look at some visual examples to solidify this.

Valid and Invalid Binary Search Tree Examples
This image shows three trees. The first is a valid BST. The other two violate the BST properties. Notice in the second tree, the node '4' is in the left subtree of the root '8', which is correct, but it is in the *right* subtree of '3'. Since 4 > 3, this violates the BST property at node '3'. In the third tree, the node '7' is in the right subtree of the root '8', which immediately violates the property since 7 < 8.
Test your understanding!

Consider the following binary tree. Is it a valid Binary Search Tree? Why or why not?

    10
   /  \
  5    15
 / \    \
3   7    18
     \
      11 
Show answer

No, it is not a valid BST. The node with value 11 violates the BST property. While 11 is correctly in the right subtree of node 7 (since 11 > 7), it is ultimately in the left subtree of the root node 10. All nodes in the left subtree of 10 must be less than 10, but 11 is not. This violates the rule that must apply to the entire subtree, not just the immediate parent-child relationship.


3. Why These Properties Matter: The "Search" Advantage

The rigid structure of a BST is what makes it so powerful. When you're searching for a value, these ordering properties allow you to eliminate half of the remaining tree at every step, much like how binary search works on a sorted array.

Let's trace the search for the value 23 in the tree from the video:

  1. Start at the root (15). 23 > 15, so we know 23 must be in the right subtree. We discard the entire left half.
  2. Move to the new node (27). 23 < 27, so 23 must be in the left subtree of 27.
  3. Move to the new node (20). 23 > 20, so 23 must be in the right subtree of 20.
  4. Move to the new node (23). We found it!

This efficient process is the primary reason for using a BST. The following video segment explains this logic and its performance implications.

Binary Tree Part 1 | BST | Binary Search Tree - Data Structures & Algorithms Tutorials In Python #10

Let's return to the 'codebasics' video to see how the BST properties lead to highly efficient searching.

Watch from 03:04 to 05:43. Pay close attention to how the search space is reduced by half at each step and how this leads to O(log n) time complexity.

As the video explains, this search operation has a time complexity of . This is a massive improvement over the search in an unordered list or a general binary tree. It's important to note that this performance is achieved when the tree is balanced. If the tree is heavily unbalanced (e.g., looking like a linked list), the performance can degrade to . We will explore balanced BSTs later in the course.


4. A Key Consequence: In-Order Traversal

Recall from the previous module that an in-order traversal of a binary tree follows the pattern: Left -> Root -> Right.

What do you think happens when you perform an in-order traversal on a Binary Search Tree?

Because of the BST's ordering property (all left values < root value < all right values), an in-order traversal will visit the nodes in ascending sorted order. This is an incredibly useful and elegant consequence of the BST structure. It also provides a simple way to verify if a given binary tree is a valid BST.

The following article explains this concept and provides a clear example.

DSA Binary Search Trees

The article 'DSA Binary Search Trees' from W3Schools provides a great summary of BST properties and demonstrates the outcome of an in-order traversal.

Read the section titled 'Traversal of a Binary Search Tree'. It confirms how to check the BST properties and explains why an in-order traversal results in a sorted list. You can skip the Python code for now, as we'll focus on implementation in the next lesson.


Conclusion

In this lesson, we've defined the fundamental properties of a Binary Search Tree, which sets the stage for the rest of this module.

Key Takeaways:

  • A BST is a binary tree with a specific ordering rule that must apply to every node.
  • The Ordering Property: For any node N, all values in its left subtree are less than N's value, and all values in its right subtree are greater than N's value.
  • BSTs generally do not contain duplicate values.
  • This structure enables highly efficient search operations, typically for balanced trees, because half of the search space can be eliminated at each step.
  • A key consequence of the BST properties is that an in-order traversal visits the nodes in ascending sorted order.

Up Next:
Now that you understand what a BST is and why it's useful, our next lesson will focus on the "how". We'll begin implementing the core operations for a BST, starting with searching for a value and inserting a new node while maintaining the BST properties.