**Self-Balancing Binary Search Trees** are * height-balanced* binary search trees that automatically keeps height as small as possible when insertion and deletion operations are performed on tree. The height is typically maintained in order of Log n so that all operations take O(Log n) time on average.

**Examples : **

Red Black Tree

AVL Tree:

**Language Implementations : **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 do Self-Balancing-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)).
1) Left Rotation
2) 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.

We have already discussed AVL tree, Red Black Tree and Splay Tree. In this acrticle, we will compare the efficiency of these trees:

Metric | RB Tree | AVL Tree | Splay Tree |
---|---|---|---|

Insertion inworst case |
O(1) |
O(logn) |
Amortized O(logn) |

Maximum heightof tree |
2*log(n) |
1.44*log(n) |
O(n) |

Search inworst 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 pernode |
Only two pointers withno extra information |

Deletion inworst case |
O(logn) |
O(logn) |
Amortized O(logn) |

Mostly used |
As universal data structure |
When frequent lookups are required |
When same element isretrieved again and again |

Real world Application |
Multiset, Multimap, Map, Set, etc. |
Database Transactions |
Cache implementation, Garbage collection Algorithms |

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Second minimum element using minimum comparisons
- Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Test case generator for Tree using Disjoint-Set Union
- Queries to check if any non-repeating element exists within range [L, R] of an Array
- Find the Alpha Score of the Given Steps (Using BST)
- Interval Tree using GNU Tree-based container
- String hashing using Polynomial rolling hash function
- How Coronavirus outbreak can end | Visualize using Data structures
- Kth Smallest element in a Perfect Binary Search Tree
- Lexicographical smallest number after at most K consecutive swaps

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.