Hello everyone when we search related to the data structure we always get the list of main topics. In this list we always see one name 'TREES'.
AVL Tree can be defined as height balanced binary search tree in which each node is associated with a balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.
Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree will be unbalanced and need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))
If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-tree.
Operations on AVL tree
Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are performed in the same way as they are performed in a binary search tree. Searching and traversing do not lead to the violation in property of AVL tree. However, insertion and deletion are the operations which can violate this property and therefore, they need to be revisited.
1. INSERTION OPERATION
Insertion in AVL tree is performed in the same way as it is performed in a binary search tree. However, it may lead to violation in the AVL tree property and therefore the tree may need balancing. The tree can be balanced by applying rotations.
2 . DELETION OPERATION
Deletion can also be performed in the same way as it is performed in a binary search tree. Deletion may also disturb the balance of the tree therefore, various types of rotations are used to rebalance the tree.
If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree
Time Complexity
The rotation operations (left and right rotate) take constant time as only a few pointers are being changed there. Updating the height and getting the balance factor also takes constant time.
So the time complexity of AVL insert remains same as BST insert which is O(h) where h is the height of the tree. Since AVL tree is balanced, the height is O(Logn). So time complexity of AVL insert is O(Logn).
RB TREE
A red-black tree is a kind of self-balancing binary search tree where each node has an extra bit, and that bit is often interpreted as the color (red or black). These colors are used to ensure that the tree remains balanced during insertions and deletions.
Although the balance of the tree is not perfect, it is good enough to reduce the searching time and maintain it around O(log n) time, where n is the total number of elements in the tree. This tree was invented in 1972 by Rudolf Bayer.
It must be noted that as each node requires only 1 bit of space to store the color information, these types of trees show identical memory footprint to the classic (uncolored) binary search tree.
Rules That Every Red-Black Tree Follows
- Every node has a color either red or black.
- The root of the tree is always black.
- There are no two adjacent red nodes (A red node cannot have a red parent or red child).
- Every path from a node (including root) to any of its descendants NULL nodes has the same number of black nodes.
A discrepancy can decision from a parent and a child both having a red color. This type of discrepancy is determined by the location of the node concerning grandparent, and the color of the sibling of the parent.
Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min, insert, delete , Etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree.
If we make sure that the height of the tree remains O(log n) after every insertion and deletion, then we can guarantee an upper bound of O(log n) for all these operations. The height of a Red-Black tree is always O(log n) where n is the number of nodes in the tree.
Time complexity
Thus, the power of a red-black tree is that the average and worst-case scenario to search, insert, and delete from the tree is always O(log n) time, guaranteed.
The space complexity of a red-black tree is no different from a BST, and depends on the number of total nodes: O(n).
AVL tree VS RB tree
on the basis of :
1. Binary search tree
The Red-Black tree is a binary search tree, and the AVL tree is also a binary search tree.
2. Rules
The following rules are applied in a Red-Black Tree:
- The node in a Red-Black tree is either red or black in color.
- The color of the root node should be black.
- The adjacent nodes should not be red. In other words, we can say that the red node cannot have red children, but the black node can have black children.
- There should be the same number of black nodes in every path; then, only a tree can be considered a Red-Black tree.
- The external nodes are the nil nodes, which are always black in color.
Rule of the AVL tree:
Every node should have the balance factor either as -1, 0 or 1.
3. How can the tree be considered as a balanced tree or not?
In the case of a Red-Black tree, if all the above rules are satisfied, provided that a tree is a binary search tree, then the tree is said to be a Red-black tree.
In the case of the AVL tree, if the balance factor is -1, 0, or 1, then the above tree is said to be an AVL tree.
4. Tools used for balancing
If the tree is not balanced, then two tools are used for balancing the tree in a Red-Black tree:
- Recoloring
- Rotation
If the tree is not balanced, then one tool is used for balancing the tree in the AVL tree:
1. Rotation
5. Efficient for which operation
In the case of the Red-Black tree, the insertion and deletion operations are efficient. If the tree gets balanced through the recoloring, then insertion and deletion operations are efficient in the Red-Black tree.
In the case of the AVL tree, the searching operation is efficient as it requires only one tool to balance the tree.
6. Time complexity
In the Red-Black tree case, the time complexity for all the operations, i.e., insertion, deletion, and searching is O(logn).
In the case of AVL tree, the time complexity for all the operations, i.e., insertion, deletion, and searching is O(logn).
- The height of the AVL tree is always balanced. The height never grows beyond log N, where N is the total number of nodes in the tree.
- It gives better search time complexity when compared to simple Binary Search trees.
- AVL trees have self-balancing capabilities.
- AVL trees are mostly used for in-memory sorts of sets and dictionaries.
- AVL trees are also used extensively in database applications in which insertions and deletions are fewer but there are frequent lookups for data required.
- Real-world uses of red-black trees include TreeSet, TreeMap, and Hashmap in the Java Collections Library. Also, the Completely Fair Scheduler in the Linux kernel uses this data structure.
- Linux also uses red-black trees in the mmap and munmap operations for file/memory mapping.
- A red-black tree is a particular implementation of a self-balancing binary search tree, and today it seems to be the most popular choice of implementation.
- Binary search trees are used to implement finite maps, where you store a set of keys with associated values.



Informative ๐ฅ
ReplyDeleteVery good ๐๐!!
ReplyDeleteinformative
ReplyDeleteExcellent work guys.
ReplyDeleteGood work๐!
ReplyDeleteAmazing & informative blog very well written
ReplyDeleteGreat work
ReplyDeleteVery good work.keep it up.
ReplyDeleteAmazing work.....very keen observation you guys have done !
ReplyDeleteAll the concepts are explained very nicely and in depth. Fantastic work !!!!!
ReplyDeleteNICE DONE✌. The concept got pretty much clear after reading your blog. THKS!!
ReplyDeletenice info! liked your page!!
ReplyDelete✌️✌️๐
ReplyDelete