DATA STRUCTURES
🇬🇧UK Edition🇨🇦Canada Edition🌐Global Tech

Red-Black Trees: Self-Balancing Binary Search Tree Rotation Rules

Prof. Sophia Sterling
Prof. Sophia Sterling
Database Systems Lead
Updated 03/09/20267 min read0 views

Red-Black Tree Invariants

1. Every node is either Red or Black.
2. The root is always Black.
3. Red nodes cannot have Red children (No consecutive Red nodes).
4. Every path from a node to descendant NULL leaves contains the same number of Black nodes.

cpp
// C++ Tree Rotation Concept
struct Node {
    int data;
    bool color; // true for Red, false for Black
    Node *left, *right, *parent;
};

void rotateLeft(Node* &root, Node* &x) {
    Node* y = x->right;
    x->right = y->left;
    if (y->left != nullptr) y->left->parent = x;
    y->parent = x->parent;
    if (x->parent == nullptr) root = y;
    else if (x == x->parent->left) x->parent->left = y;
    else x->parent->right = y;
    y->left = x;
    x->parent = y;
}

Frequently Asked Questions (FAQ)

Q: Why use Red-Black Tree over AVL Tree?

Red-Black Trees require fewer rotations during insertion/deletion, making them faster for write-heavy workloads.

Prof. Sophia Sterling
Written by Prof. Sophia SterlingAdSense EEAT Verified

Database Systems Lead. Reviewed by ByteVortex Senior Editorial Board for UK/CA academic and industry accuracy.