0
0
Data Structures Theoryknowledge~6 mins

BST property and invariant in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a collection of numbers and want to organize them so you can quickly find any number later. Without a clear rule, searching can take a long time. The Binary Search Tree (BST) property and invariant solve this by keeping the numbers in a special order that makes searching fast and easy.
Explanation
BST Property
The BST property means that for every node in the tree, all values in its left subtree are smaller than the node's value, and all values in its right subtree are larger. This rule applies to every node, not just the root. It creates a sorted structure that helps in quick searching, insertion, and deletion.
Every node's left children are smaller, and right children are larger, maintaining order.
BST Invariant
The BST invariant is the rule that the BST property must always hold true after any operation like adding or removing nodes. This means the tree must be adjusted if needed to keep the order intact. Maintaining this invariant ensures the tree remains efficient for searching.
The BST property must be preserved after every change to keep the tree organized.
Real World Analogy

Think of a library where books are arranged so that all books with titles starting with letters before 'M' are on the left shelves, and those starting with letters after 'M' are on the right shelves. Every shelf follows this rule, so you can quickly find a book by checking the correct side.

BST Property → Library shelves where books on the left have titles alphabetically before a certain letter, and books on the right have titles after that letter
BST Invariant → The rule that the library must keep this arrangement after adding or removing books to ensure quick finding
Diagram
Diagram
        ┌─────10─────┐
       /             \
    ┌─5─┐           ┌─15─┐
   /     \         /      \
  2       7      12       20
A binary search tree where left children are smaller and right children are larger than their parent nodes.
Key Facts
Binary Search Tree (BST)A tree data structure where each node has at most two children and follows the BST property.
BST PropertyAll nodes in the left subtree have smaller values, and all nodes in the right subtree have larger values than the parent node.
BST InvariantThe rule that the BST property must hold true after every insertion or deletion.
Left SubtreeThe subtree containing nodes with values smaller than the parent node.
Right SubtreeThe subtree containing nodes with values larger than the parent node.
Code Example
Data Structures Theory
class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BST:
    def __init__(self):
        self.root = None

    def insert(self, value):
        if self.root is None:
            self.root = Node(value)
        else:
            self._insert(self.root, value)

    def _insert(self, current, value):
        if value < current.value:
            if current.left is None:
                current.left = Node(value)
            else:
                self._insert(current.left, value)
        else:
            if current.right is None:
                current.right = Node(value)
            else:
                self._insert(current.right, value)

    def inorder(self, node, result=None):
        if result is None:
            result = []
        if node:
            self.inorder(node.left, result)
            result.append(node.value)
            self.inorder(node.right, result)
        return result

# Example usage
bst = BST()
bst.insert(10)
bst.insert(5)
bst.insert(15)
bst.insert(7)
bst.insert(2)
print(bst.inorder(bst.root))
OutputSuccess
Common Confusions
Believing the BST property only applies to the root node.
Believing the BST property only applies to the root node. The BST property applies to every node in the tree, ensuring order throughout all subtrees.
Thinking the BST invariant is optional after insertions or deletions.
Thinking the BST invariant is optional after insertions or deletions. The BST invariant must always be maintained to keep the tree efficient and correctly ordered.
Summary
The BST property keeps all left children smaller and right children larger than their parent nodes.
The BST invariant ensures this property is maintained after every change to the tree.
Maintaining these rules allows fast searching, insertion, and deletion in the tree.