Red-Black Trees are a type of self-balancing binary search tree that maintain balance through specific properties and rules, ensuring O(log n) time complexity for operations like insertion, deletion, and searching. They are preferred over AVL trees in scenarios with frequent insertions and deletions due to fewer rotations required. Applications of Red-Black Trees include their use in programming libraries, CPU scheduling, and various algorithms in computer science.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views15 pages
Red Black Tree
Red-Black Trees are a type of self-balancing binary search tree that maintain balance through specific properties and rules, ensuring O(log n) time complexity for operations like insertion, deletion, and searching. They are preferred over AVL trees in scenarios with frequent insertions and deletions due to fewer rotations required. Applications of Red-Black Trees include their use in programming libraries, CPU scheduling, and various algorithms in computer science.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15
Red Black Tree
Introduction to Red-Black Tree
• When it comes to searching and sorting data, one of the most fundamental data structures is the binary search tree. However, the performance of a binary search tree is highly dependent on its shape, and in the worst case, it can degenerate into a linear structure with a time complexity of O(n). • This is where Red Black Trees come in, they are a type of balanced binary search tree that use a specific set of rules to ensure that the tree is always balanced. This balance guarantees that the time complexity for operations such as insertion, deletion, and searching is always O(log n), regardless of the initial shape of the tree. • Red Black Trees are self-balancing, meaning that the tree adjusts itself automatically after each insertion or deletion operation. It uses a simple but powerful mechanism to maintain balance, by coloring each node in the tree either red or black. • Red-Black tree is a binary search tree in which every node is colored with either red or black. It is a type of self balancing binary search tree. It has a good efficient worst case running time complexity. Properties of Red Black Tree: • The Red-Black tree satisfies all the properties of binary search tree in addition to that it satisfies following additional properties – 1. Root property: The root is black. 2. External property: Every leaf (Leaf is a NULL child of a node) is black in Red-Black tree. 3. Internal property: The children of a red node are black. Hence possible parent of red node is a black node. 4. Depth property: All the leaves have the same black depth. 5. Path property: Every simple path from root to descendant leaf node contains same number of black nodes. • The result of all these above-mentioned properties is that the Red-Black tree is roughly balanced. 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. • Every leaf (i.e. NULL node) must be colored BLACK. 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. Comparison with AVL Tree • The AVL trees are more balanced compared to Red-Black Trees, but they may cause more rotations during insertion and deletion. • So if your application involves frequent insertions and deletions, then Red-Black trees should be preferred. • And if the insertions and deletions are less frequent and search is a more frequent operation, then AVL tree should be preferred over the Red-Black Tree. • The main difference between the AVL tree and the Red-Black tree is that the AVL tree is strictly balanced, while the Red-Black tree is not completely height-balanced. So, the AVL tree is more balanced than the Red-Black tree, but the Red-Black tree guarantees O(log2n) time for all operations like insertion, deletion, and searching. • Insertion is easier in the AVL tree as the AVL tree is strictly balanced, whereas deletion and searching are easier in the Red-Black tree as the Red-Black tree requires fewer rotations. How does a Red-Black Tree ensure balance? • A simple example to understand balancing is, that a chain of 3 nodes is not possible in the Red-Black tree. We can try any combination of colors and see if all of them violate the Red-Black tree property. Insertion in Red Black tree The rules used to create the Red-Black tree: 1. If the tree is empty, then we create a new node as a root node with the color black. 2. If the tree is not empty, then we create a new node as a leaf node with a color red. 3. If the parent of a new node is black, then exit. 4. If the parent of a new node is Red, then we have to check the color of the parent's sibling of a new node. 1. If the color is Black, then we perform rotations and recoloring. 2. If the color is Red then we recolor the node. We will also check whether the parents' parent of a new node is the root node or not; if it is not a root node, we will recolor and recheck the node. Example-10, 18, 7, 15, 16, 30, 25, 40, 60 Deletion in Red Back tree Case 1: If the node is Red, which is to be deleted, we simply delete it. • If we want to delete the internal node that has one child. First, replace the value of the internal node with the value of the child node and then simply delete the child node. • If we want to delete the internal node that has two child nodes. In this case, we have to decide from which we have to replace the value of the internal node (either left subtree or right subtree). We have two ways: • Inorder predecessor: We will replace with the largest value that exists in the left subtree. • Inorder successor: We will replace with the smallest value that exists in the right subtree. Deletion in Red Back tree Case 2: If the node is Red, which is to be deleted, we simply delete it. • If we want to delete the internal node that has one child. First, replace the value of the internal node with the value of the child node and then simply delete the child node. • If the root node is also double black, then simply remove the double black and make it a single black. Case 3: If the double black's sibling is black and both its children are black. • Remove the double black node. • Add the color of the node to the parent (P) node. 1. If the color of P is red then it becomes black. 2. If the color of P is black, then it becomes double black.
• The color of double black's sibling changes to red.
• If still double black situation arises, then we will apply other cases Deletion in Red Back tree Case 4: If double black's sibling is Red. • Swap the color of its parent and its sibling. • Rotate the parent node in the double black's direction. • Reapply cases. Case 5: If double black's sibling is black, sibling's child who is far from the double black is black, but near child to double black is red. • Swap the color of double black's sibling and the sibling child which is nearer to the double black node. • Rotate the sibling in the opposite direction of the double black. • Apply case 6 Deletion in Red Back tree Case 6: If double black's sibling is black, far child is Red • Swap the color of Parent and its sibling node. • Rotate the parent towards the Double black's direction • Remove Double black • Change the Red color to black. Applications • Most of the self-balancing BST library functions like map, multiset, and multimap in C++ ( or java packages like java.util.TreeMap and java.util.TreeSet ) use Red-Black Trees. • It is used to implement CPU Scheduling Linux. Completely Fair Scheduler uses it. • It is also used in the K-mean clustering algorithm in machine learning for reducing time complexity. • Moreover, MySQL also uses the Red-Black tree for indexes on tables in order to reduce the searching and insertion time. • Red Black Trees are used in the implementation of the virtual memory manager in some operating systems, to keep track of memory pages and their usage. • Many programming languages such as Java, C++, and Python have implemented Red Black Trees as a built-in data structure for efficient searching and sorting of data. • Red Black Trees are used in the implementation of graph algorithms such as Dijkstra’s shortest path algorithm and Prim’s minimum spanning tree algorithm. • Red Black Trees are used in the implementation of game engines Advantages • Red Black Trees have a guaranteed time complexity of O(log n) for basic operations like insertion, deletion, and searching. • Red Black Trees are self-balancing. • Red Black Trees can be used in a wide range of applications due to their efficient performance and versatility. • The mechanism used to maintain balance in Red Black Trees is relatively simple and easy to understand Disadvantages • Red Black Trees require one extra bit of storage for each node to store the color of the node (red or black). • Complexity of Implementation. • Although Red Black Trees provide efficient performance for basic operations, they may not be the best choice for certain types of data or specific use cases.