ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Design and Analysis of Algorithms
    COMP3138
    Progress0 / 53 topics
    Topics
    1. Introduction to Algorithm Design2. Data Structures3. Efficiency in Algorithms4. Analysis of Algorithms5. Mathematical Review6. Mathematical Analysis of Algorithms7. Types of Functions8. Order of Growth9. Asymptotic Notations10. Sorting Algorithms11. Selection Sort Algorithm12. Example and Analysis of Selection Sort13. Insertion Sort Algorithm14. Divide and Conquer Algorithms15. Merge Sort Algorithm16. Quick Sort Algorithm17. Bucket Sort Algorithm18. Radix Sort Algorithm19. Counting Sort Algorithm20. Heap Sort Basics21. Heap Algorithms22. Heap Properties and Examples23. Heap Operations24. Heap Sort Algorithm Analysis25. Heap Insertion and Deletion26. Tree Based Algorithms and Hashing27. Red Black Tree Basics28. Binary Search Tree Basics29. Tree Searching Algorithms30. Analysis of Tree Searching Algorithms31. Analysis of Insertion and Deletion in BST32. Hashing Basics33. Examples of Hash Functions34. Analysis of Collision Resolution Techniques35. Dynamic Programming36. 0-1 Knapsack Problem37. Fractional Knapsack Problem38. Longest Common Subsequence39. Shortest Path Finding40. Matrix Chain Multiplication41. Assembly Line Chain Problem42. Greedy Algorithms43. Prim's Algorithm44. Kruskal's Algorithm45. Dijkstra's Algorithm46. Huffman Coding47. NP-Completeness48. Polynomial Time Verification49. Reducibility50. NP-Completeness Proofs51. Randomized Algorithms52. Particle Swarm Optimization53. Genetic Algorithms
    COMP3138›Red Black Tree Basics
    Design and Analysis of AlgorithmsTopic 27 of 53

    Red Black Tree Basics

    7 minread
    1,221words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,221
      Code examples0
      DifficultyIntermediate