Red Black Tree

Introduction

https://www.geeksforgeeks.org/red-black-tree-set-1-introduction-2/

A red-black tree is a kind of self-balancing binary search tree where each node has an extra bit, and that bit is often interpreted as the colour (red or black).

This tree was invented in 1972 by Rudolf Bayer.

Rules That Every Red-Black Tree Follows:
  • Every node has a colour either red or black.
  • The root of tree is always black.
  • All leafs (NULL nodes) are black
  • There are no two adjacent red nodes (A red node cannot have a red parent or red child).
  • Every path from a node (including root) to any of its descendant NULL node has the same number of black nodes (Black Height).
Time Complexity

O(log n): for Search, Insert and Delete
n is the total number of elements in the red-black tree

Comparison with AVL Tree:

The AVL trees are more balanced compared to Red-Black Trees, but they may cause more rotations during insertion and deletion.

Black Height of a Red-Black Tree :

Black height is the number of black nodes on a path from the root to a leaf. Leaf nodes (NULL) are also counted black nodes.
From the above properties 3 and 4, we can derive, a Red-Black Tree of height h has black_height >= h/2.


Insert

https://www.geeksforgeeks.org/red-black-tree-set-2-insert/

two tools to do the balancing:

  • Recoloring
  • Rotation

Always try recolouring first, if it doesn’t work, then go for rotation.

The algorithms have mainly two cases depending upon the colour of the uncle:

  • If the uncle is red, do recolour.
  • If the uncle is black, do rotations and/or recolouring.
Logic
  1. Insert the node similarly to that in a Binary Searh Tree and assign a red colour to it.
  2. If the node is a root node, then change its colour to black, else check the colour of the parent node.
  3. If the parent node's colour is black then don’t change the colour, else check the colour of the node’s uncle.
  4. If the node’s uncle has a red colour then change the colour of the node’s parent and uncle to black and that of grandfather to red and repeat the same process for grandfather.
Uncle is Red
  1. If the node’s uncle is black then there are 4 possible cases:
    (The letters in LR are the positions (left/right) of node P and X, respectively.)
    a. Left Left Case (LL rotation)
    LL rotation

b. Left Right Case (LR rotation)


image.png

c. Right Right Case (RR rotation)


image.png

d. Right Left Case (RL rotation)


image.png

Now, after these rotations, if the colours of the nodes are miss matching then recolour them. (case examples? does this make sense?)

Algorithm

Let x be the newly inserted node.

  1. Perform standard BST insertion and make the newly inserted nodes RED.
  2. If x is the root, change the colour of x as BLACK (Black height of complete tree increases by 1).
  3. Do the following if the color of x’s parent is not BLACK and x is not the root.
  • If x’s uncle is RED (Grandparent must have been black from property 4)
    • (i) Change the colour of parent and uncle as BLACK.
    • (ii) Colour of grandparent as RED.
    • (iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
  • b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g) (similar to AVL Tree) :
    • (i) Left Left Case (p is left and x is left)
    • (ii) Left Right Case (p is left and x is right)
    • (iii) Right Right Case (Mirror of case i)
    • (iv) Right Left Case (Mirror of case ii)

Delete

https://www.geeksforgeeks.org/red-black-tree-set-3-delete-2/

Insertion Vs Deletion
  • In both operation, recoloring and rotations are used to maintain the Red-Black properties.
  • To decide the appropriate case
    • In insert operation, check color of uncle;
    • In delete operation, check color of sibling.
  • the main violated property:
    • after insertion: two consecutive reds
    • after deletion: the change of black height in subtrees
  • To understand deletion, notion of double black is used: When a black node is deleted and replaced by a black child, the child is marked as double black.
    Then the main task becomes to convert this double black to single black.
Deletion Steps
  1. Perform standard BST delete.
    When performing standard delete operation in BST, it always end up deleting a node which is an either leaf or has only one child. (For an internal node, successor is deleted.)
    Let v be the node to be deleted and u be the child that replaces v (Note that u is NULL when v is a leaf, and color of NULL is considered as Black).
  2. Simple Case: If either u or v is red, mark the replaced child as black (No change in black height).
    Note that only one of u and v can be red, as v is parent of u and two consecutive reds are not allowed in red-black tree.
Simple Case
  1. If Both u and v are Black.
    3.1) Color u as double black.
    (The task reduces to convert this double black to single black.)
    Note that If v is leaf, then u is NULL and color of NULL is considered black. So the deletion of a black leaf also causes a double black.
double black

3.2) When the current node u is double black, and not the root.
Let sibling of node u be s.

(a): If sibling s is black and at least one of sibling’s children is red, perform rotations.
Let the red child of s be r.
It can be divided into four subcases depending upon positions of s and r:
(i) Left Left Case (s is left and r is left, or both children of s are red)
(ii) Left Right Case (s is left and r is right)
(iii) Right Right Case (mirror of case (i))
(iv) Right Left Case (mirror of case (ii))

RR
RL

(b): If sibling is black and its both children are black, perform recoloring, and recur for the parent if parent is black. (red + double black = single black)

both children are black

(c): If sibling is red, perform a rotation to move old sibling up, recolor the old sibling and parent.
This mainly converts the tree to black sibling case (by rotation) and leads to case (a) or (b).
This case can be divided in two subcases: (Left / Right Case)
(i) Left Case (s is left)
(ii) Right Case (mirror of case (i))

sibling is red

3.3) If u is root, make it single black and return (Black height of complete tree reduces by 1).

你可能感兴趣的:(Red Black Tree)