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

  1. Search: Begins at the root and recursively or iteratively proceeds left or right based on comparisons with the target value.
  2. Insertion: Places a new element into its proper position according to the left/right subtree ordering rules.
  3. Deletion: Removing an element requires handling three structural scenarios:
  4. Node has no children (leaf node): Simply remove the node.
  5. Node has one child: Replace the node with its child.
  6. 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]

  1. 50 becomes the root.
  2. 30 < 50 → inserted into the left subtree of 50.
  3. 70 > 50 → inserted into the right subtree of 50.
  4. 20 < 50 and 20 < 30 → inserted into the left subtree of 30.
  5. 40 < 50 but 40 > 30 → inserted into the right subtree of 30.
  6. 60 > 50 but 60 < 70 → inserted into the left subtree of 70.
  7. 80 > 50 and 80 > 70 → inserted into the right subtree of 70.

Resulting Tree:

       50
      /  \
    30    70
   /  \   / \
  20  40 60  80

Insertion Algorithm Steps

  1. Start at the root node.
  2. If the new value is smaller than the current node, move to the left child.
  3. If the new value is larger than the current node, move to the right child.
  4. 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 n is 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.