0% found this document useful (0 votes)
33 views18 pages

Black Trees

Red-black trees are a type of self-balancing binary search tree where each node is colored either red or black. They satisfy three properties: 1) every path from root to leaf has the same number of black nodes, 2) the root and leaves are black, and 3) two red nodes cannot be adjacent. Insertion and deletion may violate these, requiring recoloring or rotations to restore balance. Operations take O(log n) time, making red-black trees more efficient than unsorted or linked lists but with simpler structure than fully balanced AVL trees.

Uploaded by

楷文
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views18 pages

Black Trees

Red-black trees are a type of self-balancing binary search tree where each node is colored either red or black. They satisfy three properties: 1) every path from root to leaf has the same number of black nodes, 2) the root and leaves are black, and 3) two red nodes cannot be adjacent. Insertion and deletion may violate these, requiring recoloring or rotations to restore balance. Operations take O(log n) time, making red-black trees more efficient than unsorted or linked lists but with simpler structure than fully balanced AVL trees.

Uploaded by

楷文
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Red-Black Trees

Search Tree Structures

Binary Search Trees (BSTs) Multi-way Search


Trees

Optimal AVL Red-Black B-Trees Tries


BSTs Trees Trees
2-3-4 2-3
Trees Trees

10/27/2023 Red-Black Trees 2


Red-Black Trees property

• A red-black tree is a binary search tree in which every node is colored

either red or black.

• Any red-black trees satisfies the following properties:

• RB1. The root and all external nodes are colored black.

• RB2. No root-to-external-node path has two consecutive red nodes.

• RB3. All root-to-external-node paths have the same number of black nodes.
10/27/2023 Red-Black Trees 3
Red-Black Trees Insertion

1. Find proper external node.

2. Insert and color node red.

3. No black depth violation but may violate the red-black parent-child


relationship.

10/27/2023 Red-Black Trees 4


Color adjustments
• Red child, red parent. Parent has a red sibling.
a a

b u b u

v w v w

z z

6-1 6-2
10/27/2023 Red-Black Trees 5
Rotation
• Red child, red parent. Parent no sibling or parent has a black
sibling. a
a
b u
b z
v
v u

7-1 7-2
10/27/2023 Red-Black Trees 6
A sequence of insertions into a red-black tree

initial tree insertion of 7 insertion of 12, after restructuring


which causes a
double red

insertion of 15, after recoloring insertion of 3 insertion of 5


which causes a (the root remains
double red black)
10/27/2023 Red-Black Trees 7
Red-Black Trees Delete

When we delete a node in a red-black tree, the following red-black tree


properties may be violated:

• There are no two consecutive red nodes.

• Every path from any node to all its leaf nodes has the same number of
black nodes.

10/27/2023 Red-Black Trees 8


Red-Black Trees Delete steps
• Let y be the node to be removed

• If y was red, no property could get violated, so just remove it.

• Otherwise, remove it and call the tree-fix algorithm on y’s child x (the
node which replaced the position of y)

• The tree-fix algorithm has four subcases

10/27/2023 Red-Black Trees 9


Tree Fix algorithm cases: case (1)
x is red

• The simplest case

• x has a black token and is colored red, so just color it black and
remove token a we are done!

• In other cases x is black

10/27/2023 Red-Black Trees 10


Tree Fix algorithm cases: case (2)
x’s sibling is red
Left_rotate(y)

Colors of y and z
were swapped

Remarks:
• the roots of subtrees C and D
are black
Right_rotate(y) Colors of x and y
• the second is the symmetric were swapped
case, when x is the right
child
10/27/2023 Red-Black Trees 11
Tree Fix algorithm cases: case (3)
x’s sibling is black and both nephews are black

Remarks:
• nephews are roots of subtrees C and D
• the black token is passed one level up

10/27/2023 Red-Black Trees 12


Tree Fix algorithm cases: case (4)

x’s sibling is black and at least one nephew is red


Colors of y and z
Right_rotate(w) Left_rotate(y) were swapped.
Far nephew is
colored black and
black token is removed.
Colors of z and w
were swapped

Colors of z and y
Left_rotate(x) Right_rotate(z) were swapped.
Far nephew is
colored black and
black token is removed.
Colors of x and y
were swapped
10/27/2023 Red-Black Trees 13
A sequence of removals from a red-black tree

initial tree removal of 3

after restructuring
removal of 12, causing a double black
(handled by restructuring)
10/27/2023 Red-Black Trees 14
Red-Black Trees efficiency
• Let n be the number of nodes.
• All operations work in time O(log n)! – much more efficient than
linked list or arrays implementation of sorted list!

Sorted List Search Insertion Deletion


with arrays O(log n) O(n) O(n)
with linked list O(n) O(n) O(n)
with RB trees O(log n) O(log n) O(log n)

10/27/2023 Red-Black Trees 15


Red-Black Trees Advantage

• self-balancing

• Efficient

• Simple structure

10/27/2023 Red-Black Trees 16


Red-Black Trees Disadvantage

• not completely balanced

• Search performance is worse than AVL trees

• The algorithm is more complex

• extra space usage

10/27/2023 Red-Black Trees 17


Thank for you listening

10/27/2023 Red-Black Trees 18

You might also like