![]() |
VOOZH | about |
Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree.
The height is typically maintained in order of O(log n) so that all operations take O(log n) time.
Examples: The most common examples of self-balancing binary search trees are AVL Tree. Red Black Tree and Splay Tree
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The values above the nodes in the below diagram show difference between left and right subtrees.
👁 Example-of-an-AVL-Tree-11Basic operations on AVL Tree include, Insertion, search and delete
To learn more about this, refer to the article on AVL Tree.
Red-Black tree is a self-balancing binary search tree in which every node is colored with either red or black. The root and leaf nodes (i.e., NULL nodes) are always marked as black.
Properties of Red-Black Tree:
The first tree in the below diagram is not red black tree as there are two consecutive black nodes and the second tree is Red Black tree as it follows all properties.
👁 New-Project-8To learn more about this, refer to the article on "Red-Black Tree".
Splay is a self-balancing binary search tree. The basic idea behind splay trees is to bring the most recently accessed or inserted element to the root of the tree by performing a sequence of tree rotations, called splaying.
To learn more about this, refer to the article on "Splay Tree".
A typical operation done by trees is rotation. Following are two basic operations that can be performed to re-balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
T1, T2 and T3 are subtrees of the tree rooted with y (on the left side) or x (on the right side)
y x
/ \ Right Rotation / \
x T3 - - - - - - - - - > T1 y
/ \ <- - - - - - - - - / \
T1 T2 Left Rotation T2 T3Keys in both of the above trees follow the following order
keys(T1) < key(x) < keys(T2) < key(y) < keys(T3)
So BST property is not violated anywhere.
In this article, we will compare the efficiency of these trees:
| Metric | Red-Black Tree | AVL Tree | Splay Tree |
|---|---|---|---|
| Insertion in worst case | O(logN) | O(logN) | Amortized O(logN) |
| Maximum height of tree | 2*log(n) | 1.44*log(n) | O(n) |
| Search in worst case | O(logN), Moderate | O(logN), Faster | Amortized O(logN), Slower |
| Efficient Implementation requires | Three pointers with color bit per node | Two pointers with balance factor per node | Only two pointers with no extra information |
| Deletion in worst case | O(logN) | O(logN) | Amortized O(logN) |
| Mostly used | As universal data structure | When frequent lookups are required | When same element is retrieved again and again |
| Real world Application | Database Transactions | Multiset, Multimap, Map, Set, etc. | Cache implementation, Garbage collection Algorithms |