VOOZH about

URL: https://www.geeksforgeeks.org/python/red-black-tree-in-python/

⇱ Red Black Tree in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Red Black Tree in Python

Last Updated : 23 Jul, 2025

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(log⁡n) 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.

Properties of Red-Black Tree:

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:

  1. Every node is colored red or black.
  2. The root node is always black.
  3. All leaf nodes are black and have NULL child pointers.
  4. Both children of every red node are black.
  5. Every path from a node to any of its descendant leaf nodes contains the same number of black nodes.

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(log⁡n) 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.

Red-Black Tree Node Implementation in Python:

We will implement a red-black tree node class that extends the typical binary search tree node structure by adding a color attribute:

  • Each node contains the standard value, left, and right attributes to represent the data value and links to children nodes.
  • The color attribute tracks if the node is ‘red’ or ‘black’. New nodes added during insertion are colored red by default.
  • The parent attribute helps track parent nodes when doing rotations and color changes.

Below is the implementation of Red-Black Tree Node in Python:

3. Implementation of Red-Black Tree Insertion 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:

  1. No red node can have a red child (red nodes must have black children).
  2. All paths from a node to descendant leaves must contain the same number of black nodes.

There are specific cases that can occur after adding a red child that violate these invariants:

  • Case 1: If the parent is black, the tree is still valid.
  • Case 2: If the parent is red, we have a red violation.

4. Implementation of Red Black Tree Rotation in Python:

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.

Left Rotation:

Below is the implementation of Left Rotation of RB Tree in Python:

Right Rotation:

Below is the implementation of Right Rotation of RB Tree in Python:

5. Implementation of Red-Black Tree Deletion 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:

  • Case 1: Deleted node was black.
  • Case 2: Deleted node was red.

Below is the implementation of Deletion in Red Black Tree in Python:

Complete Red-Black Tree Implementation in Python

Putting everything together, here is an implementation of a complete functional red-black tree in Python:


Output
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 


Comment
Article Tags: