Binary Search Trees (BST)

๐Ÿ“˜ Data Structure and Algorithm ๐Ÿ‘ 180 views ๐Ÿ“… Nov 05, 2025
โฑ Estimated reading time: 2 min

Binary Search Trees (BST)

A Binary Search Tree (BST) is a special type of binary tree that maintains order among elements to enable fast searching, insertion, and deletion.

Properties of BST

  • The left subtree of a node contains only nodes with keys less than the nodeโ€™s key.
  • The right subtree contains only nodes with keys greater than the nodeโ€™s key.
  • Both left and right subtrees are also binary search trees.
// Example (C++ structure)
struct Node {
    int data;
    Node* left;
    Node* right;
};

BST Operations

  • Search: Start from the root, move left or right depending on the key.
  • Insertion: Insert new node at the correct position following BST property.
  • Deletion: Three cases:
    • Node with no child โ€“ simply remove it.
    • Node with one child โ€“ replace it with its child.
    • Node with two children โ€“ replace it with its inorder successor or predecessor.

Time Complexity

OperationBest CaseWorst Case
SearchO(log n)O(n)
InsertO(log n)O(n)
DeleteO(log n)O(n)

Applications

  • Used in databases for indexing.
  • Used in search and sort operations.
  • Used in sets and maps implementation.

Conclusion

BSTs provide efficient data lookup, insertion, and deletion when balanced properly. They are a fundamental part of many algorithms and data management systems.


๐Ÿ”’ Some advanced sections are available for Registered Members
Register Now

Share this Post


โ† Back to Tutorials