A Binary Search Tree (BST) is a specialized form of a binary tree where:
log2N
, where N
is the number of nodes, ensuring efficient operations.Let's explore the structure of a Binary Search Tree with the illustration below:
As depicted in the illustration, the right subtree contains all elements greater than the root, while the left subtree contains all elements less than the root. Importantly, each subtree within a BST is itself a Binary Search Tree, with the left subtree's root value being less than the current node and the right subtree's root value being greater.
For any node in a Binary Search Tree with existing left or right children, the following relationship holds:
Left Child < Node < Right Child
In typical implementations of Binary Search Trees, duplicate node values are not permitted. However, in some special cases, a Binary Search Tree can accommodate duplicate values in the following manners:
In a Binary Search Tree, the maximum height is generally maintained at approximately log2(N), where N is the number of nodes. This is significantly more efficient than a plain Binary Tree, whose maximum height can reach N in the case of a skewed tree. This height difference greatly reduces the time required to search for a node in a BST compared to a simple Binary Tree.
For instance, to find an element, you compare it with the BST's root node. If it matches, the search is successful. If the element is greater than the root, you search in the right subtree, which contains all greater values. The left subtree is ignored in this case since it only contains lesser values.
To ensure optimal performance, several balanced variants of BSTs are used, such as:
AVL Tree: : A self-balancing BST where the height difference between left and right subtrees (balance factor) of any node is at most 1. Rotations are used to maintain balance after insertions and deletions.
Red-Black Tree:: A self-balancing BST where each node has an extra bit for color (red or black). Balancing is ensured by following specific rules about node coloring and rotations.