VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-to-red-black-tree/

⇱ Introduction to Red-Black Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Red-Black Tree

Last Updated : 14 Mar, 2026

A Red-Black Tree is a self-balancing binary search tree with a height limit of O(logN), enabling efficient search, insertion, and deletion operations in O(logN) time, unlike standard binary search trees which can take O(N) time.

  • Each node has an additional attribute: a color, which can be either red or black.
  • These colors are used to maintain balance during insertions and deletions, ensuring efficient data retrieval and manipulation.

Properties of Red-Black Trees

A Red-Black Tree has the following properties:

  1. Node Color: Each node is either red or black.
  2. Root Property: The root of the tree is always black.
  3. Red Node Property: Red nodes cannot have red children (Red nodes cannot be adjacent).
  4. Black Node Property: Every path from a node to its descendant leaves must have the same number of black nodes.
  5. Leaf Property: All leaves (NIL nodes) are black.

These properties (no two consecutive reds and same black height) ensure that the longest path from the root to any leaf is no more than twice as long as the shortest path, maintaining the tree's balance and efficient performance.

Balancing

A simple example to understand balancing is that a chain of 3 nodes is not possible in a Red-Black tree. You can try any color combination to see how they violate the Red-Black tree property.

Inferred properties

  • The number of black nodes from root to leaf (including NIL); blackHeight >= h/2.
  • Height of a red-black tree with N nodes is h <= 2log(N+1)
  • The black depth of a node is the number of black nodes from the root to that node.

Basic Operations on Red-Black Tree:

1. Insertion: Inserting a new node involves a two-step process: BST insertion, followed by fixing Red-Black property violations. Consider:

  • If the parent of the new node is black, no properties are violated.
  • If the parent is red, the tree might violate the Red Property, requiring fixes.

After inserting the new node as red, several cases may occur:

  • Case 1 (Uncle is Red): Recolor parent and uncle to black, grandparent to red. Then, move up the tree.
  • Case 2 (Uncle is Black): If node is a right child, perform a left rotation on the parent. If the node is a left child, perform a right rotation on the grandparent and recolor.

2. Searching: Searching in Red-Black Trees mirrors BST searching. Begin traversal from the root:

  1. If the target value equals the current node's value, the node is found.
  2. If less, move left; if greater, move right.
  3. Repeat until the target is found or a NIL node is reached.

3. Deletion: Deleting a node involves BST deletion, followed by fixing violations.

  1. Remove the node using standard BST rules.
  2. If a black node is deleted, a "double black" condition might arise, which requires specific fixes.

When deleting a black node, resolve "double-black" based on the sibling's color:

  1. If the sibling is red, rotate the parent, and recolor.
  2. If the sibling is black:
    • If all of the sibling's children are black, recolor the sibling and propagate the issue.
    • If at least one of the sibling's child is red:
      a. If the far child is red, rotate the parent and sibling, and recolor.
      b. If the near child is red, rotate the sibling and its child, then handle as above.

4. Rotation: Rotations are fundamental for maintaining the balanced structure of a Red-Black Tree (RBT). They preserve tree properties, ensuring the longest path from the root to any leaf is no more than twice the shortest path. These are two types:

i. Left Rotation: A left rotation at node x pivots the tree to the left, promoting its right child y to x's former position. The Transformation Steps are as follows:

  1. Detach Subtree: Move y's left subtree to become x's new right subtree.
  2. Shift Parent Link: Update y’s parent to be x’s current parent.
  3. Relink Parent: Update x’s parent to point to y instead of x.
  4. Promote Child: Set y’s left child to x.
  5. Finalize Parent: Set x’s parent to y.
👁 420851487

Pseudo-Code Implementation for Left Rotation

ii. Right Rotation: A right rotation at node x pivots the tree to the right, promoting its left child y to x’s former position.

  1. Detach Subtree: Move y’s right subtree to become x’s new left subtree.
  2. Shift Parent Link: Update y’s parent to be x’s current parent.
  3. Relink Parent: Update x’s parent to point to y instead of x.
  4. Promote Child: Set y’s right child to x.
  5. Finalize Parent: Set x’s parent to y.
👁 before_rotation

Pseudo-Code Implementation for Right Rotation

Here's a detailed implementation of a Red-Black Tree, including all of the operations mentioned above:


Output
Inorder Traversal -> 10(BLACK) 15(RED) 20(BLACK) 30(BLACK) 

Advantages

  • Because of their self-balancing property, they offer high efficiency in searching, insertion, and deletion, with a worst-case time complexity of O(log n).
  • Red-Black Trees have straightforward rules for insertion, deletion, and balance, making them relatively easy to implement.
  • Suitable for use in maps, sets, and priority queues.

Disadvantages

  • Red-Black Trees have more intricate insertion and deletion rules compared to simpler balanced trees like AVL trees.
  • Maintaining the Red-Black Tree properties introduces a minor overhead during insertion and deletion operations.

Applications of Red-Black Trees:

  • Powers high-performance containers such as map and set in C++ and TreeMap in Java.
  • In operating systems, it enables efficient process scheduling (e.g., Linux CFS) and virtual memory mapping.
  • It organizes directory structures and tracks disk blocks in file systems like XFS and Ext4.
  • It also handles high-speed packet filtering and routing table lookups in network routing.
  • Optimizing in-memory storage and data retrieval speed in key-value stores using database indexing.

Also Check:

Comment