A red-black tree is a self-balancing binary search tree with useful properties for efficient lookups and updates.
These properties ensure:
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. 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
Sr. No. | Algorithm | Time Complexity |
---|---|---|
1. | Search | O(log n) |
2. | Insert | O(log n) |
3. | Delete | O(log n) |
It is impossible to color a tree using both black and red colors without breaking the terms. However, if a tree can be colored without violating the terms, it is guaranteed to have a depth of at most
Black height is the number of black nodes on a path from the root to a leaf. Leaf nodes are also counted black nodes. From the above properties 3 and 4, we can derive, a Red-Black Tree of height h has black-height >= h/2.
Number of nodes from a node to its farthest descendant leaf is no more than twice as the number of nodes to the nearest descendant leaf.
Every Red Black Tree with n nodes has height <= 2Log2(n+1)
This can be proved using the following facts:
From the above points, we can conclude the fact that Red Black Tree with n nodes has a height less than or equal to
In a binary search tree (BST), rotations are operations that modify the structure of the tree by moving nodes around. These rotations help maintain the balance and order of the BST, ensuring efficient search and insertion operations.
A left rotation is a rotation operation that moves a node and its right child in a counter-clockwise direction. It is performed when the right subtree of a node becomes heavier or taller than the left subtree.
To perform a left rotation:
After a left rotation, the selected node becomes the new root of the subtree, and the tree becomes more balanced.
A right rotation is the opposite of a left rotation. It is a rotation operation that moves a node and its left child in a clockwise direction. It is performed when the left subtree of a node becomes heavier or taller than the right subtree.
To perform a right rotation:
After a right rotation, the selected node becomes the new root of the subtree, and the tree becomes more balanced.
The insertion operation in a Red-Black Tree has a time complexity of O(log n), where n is the number of nodes in the tree. This complexity arises due to the balancing and restructuring operations required to maintain the Red-Black Tree properties.