Binary Search Tree (BST) is a type of binary tree where the left child contains only nodes with values lesser than the parent node, and the right child only nodes with values greater than the parent node. This property makes the BST efficient for searching, inserting, and deleting nodes.

Key Points

  • Search: Efficiently search for a value in the tree.
  • Insert: Insert a new value in the correct position.
  • Delete: Remove a node from the tree while maintaining the BST properties.

Example

Here's a simple representation of a BST:

    10
   /  \
  5    20
 / \    \
3   7    25

How it works

  • Search: Start at the root. If the target value is less than the root's value, go to the left child. If it's greater, go to the right child. Repeat this process until you find the value or reach a leaf node.
  • Insert: Start at the root. If the target value is less than the current node's value, go to the left child. If it's greater, go to the right child. Repeat this process until you reach a null node. Insert the new value there.
  • Delete: This is more complex. You need to handle three cases:
    • The node to delete has no children.
    • The node to delete has one child.
    • The node to delete has two children.

More Resources

For a deeper understanding of Binary Search Trees, check out our Advanced Binary Search Tree Tutorial.

Binary Search Tree