Professional Documents
Culture Documents
00001000
01000000
00010000
00000010
00100000
00000001
00000100
10000000
A Red-Black Tree is a self-balancing binary search tree that maintains
balance during insertions and deletions. It is named "Red-Black"
because each node in the tree has an extra attribute, which is a color
that can be either red or black. The color of the nodes is used to ensure
that the tree remains balanced.
Here are the key properties of a Red-Black Tree:
1. Node Color: Each node is colored either red or black.
2. Root Property: The root is always black.
3. Leaf Property: Every leaf (NIL or null node) is black.
4. Red Property: If a red node has children, then the children are
always black.
5. Depth Property: For each node, any simple path from this node to
any of its descendant leaves contains the same number of black
nodes.
These properties ensure that the tree is approximately balanced,
preventing the tree from becoming too skewed and maintaining a height
of O(log n), where n is the number of nodes in the tree.
Operations:
Insertion:
When a new node is inserted into the Red-Black Tree, it is initially
colored red. After the insertion, the tree may violate the Red-Black
properties, so a series of rotations and color adjustments are performed
to restore the balance.
1. Color the new node red.
2. Rebalance the tree:
• If the parent of the newly inserted node is black, the tree
remains valid.
• If the parent is red, and the uncle of the new node is also
red, recolor the parent and uncle to black, and recolor the
grandparent to red. Then, recursively check and fix the Red-
Black properties starting from the grandparent.
• If the parent is red, but the uncle is black or null, perform
rotations and color adjustments to restore balance.
Deletion:
When a node is deleted from the Red-Black Tree, it is replaced by its
successor or predecessor, and then the tree is rebalanced.
1. If the node to be deleted has no children or one child:
• Remove the node and replace it with its child (or null).
• If the removed node is black, additional adjustments are
needed to maintain the Red-Black properties.
2. If the node to be deleted has two children:
• Find its in-order successor (or predecessor).
• Replace the node to be deleted with its successor (or
predecessor).
• Recursively delete the successor (which has at most one
child).
• If the successor or original node removed was black,
additional adjustments are needed.
Red-Black Tree Diagrams:
Here are diagrams illustrating a Red-Black Tree during the insertion
process:
These diagrams demonstrate the Red-Black Tree properties being
maintained during the insertion of nodes. Similar diagrams can be
created for deletion operations.
Remember that these diagrams are simplified for illustration, and real-
world Red-Black Trees may have additional nodes and complexities