Binary Search Trees
Binary Search Trees
Content:
• What is a Binary Search Tree (BST)?
A Binary Search Tree (BST) is a node-based data structure in which each node has at most
two children, referred to as the left child and the right child. For each node, the left subtree
contains nodes with values less than the node’s value, while the right subtree contains nodes
with values greater than the node’s value.
o Dynamic Data Structure: Allows insertion and deletion of elements while maintaining
sorted order.
• Key Characteristics:
o Sorted In-Order Traversal: An in-order traversal of a BST results in sorted data. o Self-
Balancing Variants: AVL trees, Red-Black trees, etc., maintain balanced height for optimized
operations.
• Real-Life Analogy:
Imagine a large library where books are organized alphabetically by title for easy searching.
1. Root (Main Hall):
o When you enter the library (BST), you start at the main hall (root), which
provides directions or indexes to different sections.
2. Nodes (Bookshelves):
o Each bookshelf (node) holds books with titles arranged in a specific way.
o The left side of a shelf only has books with titles that come earlier
alphabetically than the current book.
o The right side of a shelf only has books with titles that come later
alphabetically.
3. Edges (Aisles):
o The pathways (edges) connecting bookshelves (nodes) allow you to traverse
through the library efficiently.
o Bookshelves that contain only a few books and don’t lead to other aisles are
like leaf nodes.
o The more levels of bookshelves, the larger (or taller) the library is.
o To find a specific book, you start at the main hall (root) and follow the
directions (edges) based on alphabetical order.
o If the book’s title is before the current bookshelf’s title, go left. If it’s after,
go right.
o When a book is removed, the library adjusts itself to ensure the alphabetical
order remains intact.
• Historical Context:
Binary Search Trees were introduced as part of early computer science research on efficient
searching and sorting algorithms, with further advancements leading to self-balancing
variants for optimized performance
.
2. Core Concepts and Mechanism
Content:
• How BSTs Work (Step-by-Step):
1. Insertion: Recursively compare the new value with the current node, moving left or
right accordingly until an empty spot is found.
2. Searching: Compare the target value with the root node and navigate left or right until
the value is found or the subtree is exhausted.
3. Deletion: Remove a node while preserving the BST property. Three cases: leaf node,
node with one child, node with two children.
• Tree Structure:
o Root = The topmost node
• Example:
Insert the values {15, 10, 20, 8, 12, 17, 25} into a BST and visualize the structure.
3. Operations on Binary Search Trees
Content:
• Insertion
• Pseudo Code:
INSERT(root, key)
IF root IS NULL
RETURN root
• Deletion
2. Node with One Child: Replace the node with its child.
3. Node with Two Children: Replace the node with its in-order successor or
predecessor.
• Pseudo Code:
DELETE(root, key)
ELSE
IF NOT (root.left AND root.right) RETURN root.left OR root.right
temp = FIND_MIN(root.right)
RETURN root
FIND_MIN(node)
RETURN node
• Searching
• Pseudo Code:
SEARCH(root, key)
IF NOT root OR root.key == key
RETURN root
ELSE
• Traversals
• In-Order (Left, Root, Right): Returns sorted order.
Pseudo Code:
4Applications of Binary Search Trees (BST)
1. Searching and Sorting: Efficient searching, insertion, and deletion with average complexity of O(log n).
3. Symbol Table / Dictionary Implementation: Efficiently implements associative arrays (e.g., Map and Set in C++ STL).
2. Efficient Insertion and Deletion: O(log n) time complexity for balanced trees.
4. Space Efficiency: Requires O(n) space, with memory efficiency compared to hash tables.
2. Requires Balancing: Maintaining balance (e.g., AVL or Red-Black trees) adds complexity.
4. Not Ideal for Large Datasets: Hash tables are often faster for large datasets.
A Binary Search Tree (BST) offers efficient O(log n) searching, insertion, and deletion when
balanced, making it ideal for sorted data retrieval and dynamic resizing. However,
unbalanced BSTs degrade to O(n) performance, and maintaining balance (using AVL or Red-
Black trees) adds complexity. While efficient for small to medium datasets, hash tables can
outperform BSTs on large, unordered data. Keeping BSTs balanced is essential for optimal
performance.