Self-Balancing Binary Search Trees
Last Updated :
28 Mar, 2023
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 logN so that all operations take O(logN) time on average.
Examples: The most common examples of self-balancing binary search trees are
- AVL Tree
- Red Black Tree and
- Splay Tree
AVL 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.

Example of AVL Tree
Basic operations on AVL Tree include:
- Insertion
- Searching
- Deletion
To learn more about this, refer to the article on AVL Tree.
Red-Black 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.

Example of Red-Black Tree
Some Properties of Red-Black Tree:
- Root property: The root is black.
- External property: Every leaf (Leaf is a NULL child of a node) is black in Red-Black tree.
- Internal property: The children of a red node are black. Hence possible parent of red node is a black node.
- Depth property: All the leaves have the same black depth.
- Path property: Every simple path from the root to descendant leaf node contains the same number of black nodes
To learn more about this, refer to the article on “Red-Black Tree“.
Splay 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.
Basic operations that are performed in a splay tree are:
- Insertion
- Searching
- Deletion
- Rotation (There are two types of rotation in a Splay tree named zig rotation and zag rotation)
To learn more about this, refer to the article on “Splay Tree“.
Language Implementations of self-balancing BST:
- set and map in C++ STL.
- TreeSet and TreeMap in Java. Most of the library implementations use Red Black Tree.
- Python standard library does not support Self Balancing BST. In Python, we can use bisect module to keep a set of sorted data. We can also use PyPi modules like rbtree (implementation of red-black tree) and pyavl (implementation of AVL tree).
How does Self-Balancing Binary Search Tree maintain height?
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)).
- Left Rotation
- Right Rotation
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 T3
Keys 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.
Comparisons among Red-Black Tree, AVL Tree and Splay Tree:
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 |
Conclusion:
AVL Tree is considered to be Strict as we have to maintain Balance Factor i.e. abs(Height of Left Sub-Tree – Height of Right Sub-Tree) ≤ 1, and In Red Black Tree, it is not considered to be as Strict as AVL Tree, due to which there are less number of rotations are required to make the Unbalanced BST -> Balanced as compared to AVL Tree. Also, In summary, if the application requires fast search times, an AVL Tree may be the better choice. If the application requires fast insertion and deletion times, a Red-Black Tree may be the better choice. It’s also important to consider other factors such as the size and complexity of the data being stored, as well as the memory and processing resources available.
Similar Reads
Binary Search Tree
A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right chi
3 min read
Introduction to Binary Search Tree
Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the
3 min read
Applications of BST
Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward. The left subtree of a node contains only nod
2 min read
Applications, Advantages and Disadvantages of Binary Search Tree
A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p
2 min read
Insertion in Binary Search Tree (BST)
Given a BST, the task is to insert a new node in this BST. Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is f
15+ min read
Searching in Binary Search Tree (BST)
Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
7 min read
Deletion in Binary Search Tree (BST)
Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios: Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BST Deleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node
10 min read
Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order
Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: Output: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 100 Input: Output:
11 min read
Balance a Binary Search Tree
Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height. Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height. Input: Output: Explanation: The above
10 min read
Self-Balancing Binary Search Trees
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 logN so that all operations take O(logN) time on average
4 min read