ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3138Red Black Tree Basics
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3138

    Design and Analysis of Algorithms

    Progress0 / 53 done
    Topics53
    1Introduction to Algorithm Design2Data Structures3Efficiency in Algorithms4Analysis of Algorithms5Mathematical Review6Mathematical Analysis of Algorithms7Types of Functions8Order of Growth9Asymptotic Notations10Sorting Algorithms11Selection Sort Algorithm12Example and Analysis of Selection Sort13Insertion Sort Algorithm14Divide and Conquer Algorithms15Merge Sort Algorithm16Quick Sort Algorithm17Bucket Sort Algorithm18Radix Sort Algorithm19Counting Sort Algorithm20Heap Sort Basics21Heap Algorithms22Heap Properties and Examples23Heap Operations24Heap Sort Algorithm Analysis25Heap Insertion and Deletion26Tree Based Algorithms and Hashing27Red Black Tree Basics28Binary Search Tree Basics29Tree Searching Algorithms30Analysis of Tree Searching Algorithms31Analysis of Insertion and Deletion in BST32Hashing Basics33Examples of Hash Functions34Analysis of Collision Resolution Techniques35Dynamic Programming360-1 Knapsack Problem37Fractional Knapsack Problem38Longest Common Subsequence39Shortest Path Finding40Matrix Chain Multiplication41Assembly Line Chain Problem42Greedy Algorithms43Prim's Algorithm44Kruskal's Algorithm45Dijkstra's Algorithm46Huffman Coding47NP-Completeness48Polynomial Time Verification49Reducibility50NP-Completeness Proofs51Randomized Algorithms52Particle Swarm Optimization53Genetic Algorithms
    7 min read
    Topic 27 of 53
    Topic Study Note Illustration
    27

    Red Black Tree Basics

    A Red-Black Tree (RBT) is a type of self-balancing binary search tree (BST) with additional properties that help maintain balance during insertion and deletion operations. The main...

    Red-Black Tree Basics

    A Red-Black Tree (RBT) is a type of self-balancing binary search tree (BST) with additional properties that help maintain balance during insertion and deletion operations. The main idea behind a red-black tree is to guarantee that the tree remains balanced (i.e., the height of the tree is kept logarithmic), which ensures that the time complexity of various operations such as search, insert, and delete is O(log n).

    The red-black tree achieves this balance by introducing a color property for each node, which helps to constrain the shape of the tree.


    Key Properties of Red-Black Trees

    There are five essential properties that every red-black tree must satisfy:

    1. Node Color:

      • Each node is either red or black.
    2. Root Property:

      • The root of the tree is always black.
    3. Leaf Property:

      • Every leaf node (NIL or NULL nodes) is black. These are the external nodes and don't hold any actual data.
    4. Red Node Property:

      • If a red node has children, then both of its children must be black. In other words, no two red nodes can be adjacent (this is called the "no red-red violation").
    5. Black Height Property:

      • Every path from a given node to its descendant NULL nodes must contain the same number of black nodes. This number is called the black height. This ensures that the tree remains balanced.

    How Red-Black Trees Maintain Balance

    The properties above ensure that the tree remains approximately balanced, which is crucial for keeping operations like insertion, deletion, and search at O(log n) time complexity.

    • The black height property implies that there are at most twice as many black nodes along any path as there are red nodes.
    • The black height is a crucial concept because it bounds the maximum height of the tree to 2 * log(n + 1), where n is the number of nodes. This ensures that even in the worst case, the height of the tree will be logarithmic, providing efficient access to any node.

    Red-Black Tree Operations

    1. Insertion in a Red-Black Tree:

      • When you insert a new node, you start by performing a regular binary search tree insertion (like in a standard BST).
      • After inserting the new node, it is colored red (since red nodes are allowed to be inserted).
      • The critical part of insertion is to fix any violations of the red-black tree properties using rotations and color changes. There are several cases that need to be handled depending on the parent, uncle, and grandparent nodes of the newly inserted node.

      Cases in Insertion:

      1. Case 1: No violation - If there’s no violation of red-black properties, simply finish the insertion.
      2. Case 2: Red-red violation - If the parent of the newly inserted node is red, then you perform a series of rotations and recoloring operations to fix the violation.
        • Uncle is red: Perform a color flip (recolor the parent, uncle to black, and grandparent to red). This may propagate up the tree.
        • Uncle is black: Perform rotations (left or right) and recolor nodes as needed to maintain the red-black properties.
    2. Deletion in a Red-Black Tree:

      • When a node is deleted, the tree might violate some red-black properties, particularly the black height or the no-red-red violation. These violations are fixed using rotations and color adjustments.
      • If the deleted node is black, it can lead to a double-black violation (i.e., the black height is reduced). This needs to be fixed by performing rotations and recoloring.

      Cases in Deletion:

      1. Case 1: Deleted node is red - No major issue; simply remove the node.
      2. Case 2: Deleted node is black - The deletion may cause a reduction in the black height. You need to perform a series of rotations and recoloring to restore the tree's balance and satisfy the red-black properties.

    Rotations in Red-Black Trees

    To fix violations in the tree, we often use two types of rotations:

    1. Left Rotation:

      • A left rotation is performed when the right child of a node is taller or when we need to shift a node to the left to restore balance.
      • A left rotation takes a node and makes its right child the new root of the subtree, moving the original root to the left.

      Example of a Left Rotation:

      Before rotation:
            10
              \
               20
      
      After rotation:
           20
          /
        10
      
    2. Right Rotation:

      • A right rotation is the mirror of the left rotation. It is performed when the left child of a node is taller or when we need to shift a node to the right to restore balance.
      • A right rotation makes the left child of a node the new root of the subtree, moving the original root to the right.

      Example of a Right Rotation:

      Before rotation:
            20
           /
         10
      
      After rotation:
           10
            \
            20
      

    Time Complexity of Red-Black Tree Operations

    • Insertion: O(log n), because the tree remains balanced and the height of the tree is logarithmic.
    • Deletion: O(log n), since the tree is balanced and we only need to traverse and possibly rotate along the height of the tree.
    • Search: O(log n), due to the logarithmic height of the tree, ensuring quick access to nodes.

    Example of a Red-Black Tree

    Consider the following sequence of insertions and how the tree adjusts:

    1. Insert 10:

      10 (Black)
      
    2. Insert 20:

        10 (Black)
          \
          20 (Red)
      
    3. Insert 30:

        10 (Black)
          \
          20 (Black)
            \
            30 (Red)
      

      Here, we have a violation (two consecutive red nodes). We perform a left rotation and recoloring.

      After the rotation:

            20 (Black)
           /  \
        10 (Red) 30 (Red)
      
    4. Insert 15:

            20 (Black)
           /  \
        10 (Red) 30 (Red)
            \
           15 (Red)
      

      Now, we have a red-red violation (the parent 10 is red and the uncle 30 is red). We fix this by recoloring.

      After recoloring:

            20 (Black)
           /  \
        10 (Black) 30 (Black)
            \
           15 (Red)
      

    Advantages of Red-Black Trees

    • Efficient Operations: Red-Black Trees guarantee O(log n) time for search, insert, and delete operations, making them efficient for dynamic data storage.
    • Self-Balancing: Unlike a regular binary search tree, a red-black tree ensures that the tree remains balanced, thus avoiding the worst-case scenario of linear time complexity (which can happen in an unbalanced BST).
    • Widely Used: Red-Black Trees are widely used in the implementation of balanced search trees in standard libraries and databases (e.g., the C++ STL map and set and Java’s TreeMap).

    Disadvantages of Red-Black Trees

    • Complexity: The algorithms for maintaining the tree's balance (insertion, deletion) are more complex compared to simpler BSTs. This adds overhead in implementation.
    • Slower than AVL Trees: In some cases, AVL trees (another type of self-balancing tree) can be slightly faster for lookups because they are more strictly balanced. However, AVL trees may require more rotations during insertion and deletion.

    Conclusion

    A Red-Black Tree is a self-balancing binary search tree that guarantees O(log n) time complexity for search, insertion, and deletion operations. Its balancing is achieved by coloring the nodes red and black and ensuring specific properties that control the tree’s height. While more complex than a simple binary search tree, it provides a reliable and efficient data structure for maintaining sorted data, especially when frequent updates (insertions and deletions) are needed.

    ← Previous topic 26
    Tree Based Algorithms and Hashing
    Next topic 28 →
    Binary Search Tree Basics

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms