9. Binary Search Tree
Description: A Binary Search Tree (BST) is a node-based binary tree data structure where each node satisfies the following properties:
- All values in the left subtree of a node are strictly less than the node's value.
- All values in the right subtree of a node are strictly greater than the node's value.
This structure organizes data to enable efficient searching, insertion, and deletion operations, particularly when working with sorted or semi-sorted data collections.
Structure
- Root: The topmost node from which tree operations originate.
- Left Subtree: Contains nodes with values smaller than the parent node.
- Right Subtree: Contains nodes with values larger than the parent node.
- Leaves: Bottom nodes that do not have any child nodes.
Core Operations
- Search: Begins at the root and recursively or iteratively proceeds left or right based on comparisons with the target value.
- Insertion: Places a new element into its proper position according to the left/right subtree ordering rules.
- Deletion: Removing an element requires handling three structural scenarios:
- Node has no children (leaf node): Simply remove the node.
- Node has one child: Replace the node with its child.
- Node has two children: Find the in-order successor (smallest in the right subtree) or in-order predecessor (largest in the left subtree) to swap values, then delete that node.
Construction Example
Given the sequence of numbers: [50, 30, 70, 20, 40, 60, 80]
50becomes the root.30<50→ inserted into the left subtree of50.70>50→ inserted into the right subtree of50.20<50and20<30→ inserted into the left subtree of30.40<50but40>30→ inserted into the right subtree of30.60>50but60<70→ inserted into the left subtree of70.80>50and80>70→ inserted into the right subtree of70.
Resulting Tree:
50
/ \
30 70
/ \ / \
20 40 60 80
Insertion Algorithm Steps
- Start at the root node.
- If the new value is smaller than the current node, move to the left child.
- If the new value is larger than the current node, move to the right child.
- Repeat steps 2–3 until an empty slot (
null) is reached, and attach the new node there.
Time Complexity
- Search: O(log n) on average for balanced trees, where
nis the number of nodes. In the worst-case scenario (unbalanced/skewed tree), search degrades to O(n). - Insertion: Average O(log n), worst-case O(n).
- Deletion: Average O(log n), worst-case O(n).
Practical Applications
- Database Indexing: Used for quick record retrieval and ranged queries.
- Sorting Algorithms: Tree Sort builds a BST to output sorted data in-order.
- Dynamic Query Processing: Applied in routing algorithms and real-time lookup engines.
Advantages
- Fast lookups, insertions, and deletions when the tree remains balanced.
- Intuitive implementation offering strong dynamic data capabilities.
Disadvantages
- Performance depends on tree height. If elements are inserted in pre-sorted order, the tree degenerates into a linear linked list with O(n) performance.
- Self-balancing trees (such as AVL trees or Red-Black trees) are required to automatically maintain logarithmic height during dynamic modifications.
All rights remain with the original author (Archiveskk). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me