Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer
Proporties
- A red–black tree is a type of self-balancing binary search tree.
- The self-balancing is provided by painting each node with one of two colors (these are typically called ‘red’ and ‘black’, hence the name of the trees) in such a way that the resulting painted tree satisfies certain properties that don’t allow it to become significantly unbalanced.
- The leaf nodes of red–black trees do not contain data. These leaves need not be explicit in computer memory—a null child pointer can encode the fact that this child is a leaf—but it simplifies some algorithms for operating on red–black trees if the leaves really are explicit nodes. To save memory, sometimes a single sentinel node performs the role of all leaf nodes; all references from internal nodes to leaf nodes then point to the sentinel node.
- In addition to the requirements imposed on a binary search trees, with red–black trees:
- A node is either red or black.
- The root is black. (This rule is sometimes omitted. Since the root can always be changed from red to black, but not necessarily vice-versa, this rule has little effect on analysis.)
- All leaves (NIL) are black. (All leaves are same color as the root.)
- Every red node must have two black child nodes.
- Every path from a given node to any of its descendant leaves contains the same number of black nodes.
- These constraints enforce a critical property of red–black trees: that the path from the root to the furthest leaf is no more than twice as long as the path from the root to the nearest leaf.
Operations
Search
The searching in a Red-Black Tree is the same as that in a BST.
Insert
- The first step of isertion in Red-Black Tree is almost the same as that in the BST, except for adding the new node as a red internal node with two black leaf nodes.
- After that, to maintian the proporties, 5 cases should be considered. ‘N’ will denote the current node(‘P’ will denode the parent of ‘N‘, ‘G’ will denode grandparent of ‘N‘, ‘U’ will denode the uncle of ‘N‘) :
- N is the root of the tree. In this case, just repaint it black;
- P is red. In this case, nothing needs to do.
- P and U are all red. In this case, repaint P and U black, the repaint G red. Next G will become N, and check from case 1 again.
- P is red but U is black, and N is right child of P while P is left child of G(or N is left child of P while P is right child of G). In this case, performe left(or right) rotation to P, then becomes case 5;
- P is red but U is black, and N is left child of P while P is left child of G(or N is right child of P while P is right child of G). In this case, performe right(or left) rotation to G, the switch the color of P and G.
Remove
- The first step of removing in a Red-Black tree is the same as that in a BST: if an internal one, replace it with the leftest node in the right subtree.
- After that, 3 cases should be considered:
- If a node with one child, delete it and paint it’s child black;
- If a red leaf node, delete it directly;
- If a black leaf node;
- If the sibling is black:
- Sibling node has none red child: delete the node, repaint the sibling red and the parent black. If the parent was red, done; else, parent becomes the current node, continue;
- Sibling node has one red outer child: delete the node, if red child on the left, rotate right, else rotate left. Then repaint sibling the same color as the parent, repaint the parent and the red child black;
- Sibling node has one red iner child: delete the node, if red child on the right, first rotate left then rotate right, else first rotate right then rotate left. Then repaint the red child the same as the parent, repaint the parent black;
- Sibling node has two red children: delete the node, if sibling in the left, rotate first rotate left then rotate right, else first rotate right then rotate left. Then repaint the new root the same color as the previous root, repaint the previous root black.
- If the sibling is red:
- If a left child, rotate left, switch the color of parent and sibling and delete it;
- If a right child, rotate right, switch the color of parent and sibling and delete it.
- If the sibling is black: