![]() |
VOOZH | about |
Red Black Tree is a self-balancing binary search tree where each node has an extra bit representing its color, either red or black. By constraining how nodes are colored during insertions and deletions, red-black trees ensure the tree remains approximately balanced during all operations, allowing for O(logn) search, insert, and delete operations.
Here, we will explore, how to implement insertion, deletion, and balancing logic to construct a fully functional red-black tree in Python.
Red-black trees are binary search trees that add the extra constraint of node coloring to ensure the tree remains balanced during operations. Here are some key properties of red-black trees:
By constraining coloring during insertions and deletions, red-black trees ensure the maximum height of the tree does not exceed 2log(n+1), where n is the number of nodes. This guarantees O(logn) worst-case performance for search, insert, and delete operations.
The balance of red-black trees comes from how coloring changes during insertions and deletions. Specific balancing rules are followed that require certain coloring operations and tree rotations to retain the black-height property of the tree.
We will implement a red-black tree node class that extends the typical binary search tree node structure by adding a color attribute:
value, left, and right attributes to represent the data value and links to children nodes.color attribute tracks if the node is ‘red’ or ‘black’. New nodes added during insertion are colored red by default.parent attribute helps track parent nodes when doing rotations and color changes.Below is the implementation of Red-Black Tree Node in Python:
We will first see the algorithm for inserting new nodes into the red-black tree. This must follow specific rules to ensure the tree remains balanced. The insertion in Red Black Tree occurs in two parts:
1. Standard BST Insertion: Insertion begins by adding the new node with a red color as a standard BST insertion.
2. Fixing Red-Black Properties: After regular BST insertion, we may have violated red-black tree properties. There are two main red-black tree invariants that must be maintained after insertion:
There are specific cases that can occur after adding a red child that violate these invariants:
The insertion fixing logic requires left and right rotations to restructure the tree in certain cases. These operations change the position of nodes to move the red violation upward while maintaining the BST ordering, similar to AVL tree rotations.
Below is the implementation of Left Rotation of RB Tree in Python:
Below is the implementation of Right Rotation of RB Tree in Python:
Implementing deletion in a red-black tree also requires maintaining the coloring invariants after removing nodes. Deletion in Red Black Tree occurs in two parts:
1. Standard BST Deletion: We first implement the standard BST node removal process.
2. Red-Black Tree Fixing After Deletion: Similar to insertion, a deletion can break the red-black tree invariants. There are two main cases:
Below is the implementation of Deletion in Red Black Tree in Python:
Putting everything together, here is an implementation of a complete functional red-black tree in Python:
Inorder traversal of the Red-Black Tree: 10 20 25 30 40 50 Inorder traversal of the Red-Black Tree after deleting 20 10 25 30 40 50