0% found this document useful (0 votes)
0 views

Binary Search Trees

Uploaded by

shuklarohan388
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Binary Search Trees

Uploaded by

shuklarohan388
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Introduction to 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.

• Why Use Binary Search Trees?

o Efficient Searching: Provides O(log n) average-case time complexity for searching,


insertion, and deletion.

o Dynamic Data Structure: Allows insertion and deletion of elements while maintaining
sorted order.

o Hierarchical Structure: Naturally supports operations like finding minimum, maximum,


predecessor, and successor.

• Key Characteristics:

o Recursive Definition: Each subtree of a node is itself a BST.

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:

The Library 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.

4. Leaf Nodes (Single Bookshelves):

o Bookshelves that contain only a few books and don’t lead to other aisles are
like leaf nodes.

5. Height of the Tree (Library Size):

o The more levels of bookshelves, the larger (or taller) the library is.

6. Searching (Finding a Book):

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 This efficient searching process is like performing a Binary Search.

7. Insertion (Adding New Books):


o When a new book is added, it's placed on the appropriate shelf following the
alphabetical rules (BST properties).
8. Deletion (Removing Books):

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

. o Leaf = A node with no children.

o Height = Longest path from the root to a leaf.

• 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

• Adding a node by comparing values recursively until a suitable empty position is


found.

• Time Complexity: O(h), where h is the height of the tree.

• Pseudo Code:
INSERT(root, key)

IF root IS NULL

Create a new node with key

RETURN new node

IF key < root.key

root.left = INSERT(root.left, key)

ELSE IF key > root.key


root.right = INSERT(root.right, key)

RETURN root

• Deletion

• Removing a node while maintaining BST properties. Cases:

1. Leaf Node: Simply remove the node.

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.

• Time Complexity: O(h).

• Pseudo Code:
DELETE(root, key)

IF NOT root RETURN NULL

IF key < root.key THEN root.left = DELETE(root.left, key)

ELSE IF key > root.key THEN root.right = DELETE(root.right, key)

ELSE
IF NOT (root.left AND root.right) RETURN root.left OR root.right
temp = FIND_MIN(root.right)

root.key, root.right = temp.key, DELETE(root.right, temp.key)

RETURN root

FIND_MIN(node)

WHILE node.left DO node = node.left

RETURN node

• Searching

• Traversing the tree based on comparisons.

• Time Complexity: O(h).

• Pseudo Code:
SEARCH(root, key)
IF NOT root OR root.key == key

RETURN root

IF key < root.key

RETURN SEARCH(root.left, key)

ELSE

RETURN SEARCH(root.right, key)

• Traversals
• In-Order (Left, Root, Right): Returns sorted order.

• Pre-Order (Root, Left, Right): Useful for tree copying.

• Post-Order (Left, Right, Root): Useful for deleting trees.

Pseudo Code:
4Applications of Binary Search Trees (BST)
1. Searching and Sorting: Efficient searching, insertion, and deletion with average complexity of O(log n).

2. Databases: Used for indexing and range queries.

3. Symbol Table / Dictionary Implementation: Efficiently implements associative arrays (e.g., Map and Set in C++ STL).

4. File Systems: Efficient directory structure management.

5. Auto-completion and Spell Checking: Fast lookups for suggestions or corrections.

5 Advantages of Binary Search Tree (BST)

1. Efficient Searching: O(log n) average time complexity for balanced trees.

2. Efficient Insertion and Deletion: O(log n) time complexity for balanced trees.

3. Inorder Traversal: Allows sorted data retrieval without extra sorting.

4. Space Efficiency: Requires O(n) space, with memory efficiency compared to hash tables.

5. Dynamic Structure: Automatically adjusts size with insertions and deletions.

Limitations of Binary Search Tree (BST)

1. Performance Degradation: Unbalanced trees can degrade to O(n) complexity.

2. Requires Balancing: Maintaining balance (e.g., AVL or Red-Black trees) adds complexity.

3. Space Overhead: Requires extra space for pointers.

4. Not Ideal for Large Datasets: Hash tables are often faster for large datasets.

5. Complex Implementation: Handling balancing and insertion/deletion can be error-prone.


Conclusion

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.

You might also like