Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-black tree properties

Similar presentations


Presentation on theme: "Red-black tree properties"— Presentation transcript:

1 Red-black tree properties
Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two consecutive red nodes -- i.e. the children of a red node must be black Every path from a node, x, to a descendant leaf contains the same number of black nodes -- the “black height” of node x. In these slides we don’t force the root to be black

2 Insertion: The First Step
Given a new node with a key value, the first step is to determine where to insert the new node. Since a Red-Black Tree is a Binary Search Tree (BST), use the BST Insert procedure to: determine the location where the new node’s key value belongs in the Red-Black Tree. insert the new node at that location. Color the new node red. Use the cases described in the following slides to restore any Red-Black Tree properties that are violated by the new node.

3 Red-black tree insertion: Case 0: New node’s parent is black.
newly inserted node No property is violated, so there’s nothing to do.

4 (x could also be a right child)
Red-black tree insertion: Case 1: New node’s parent and uncle are both red. This node is now red; possibly propagating a r-b violation upward that must also be fixed… newly inserted node x x (x could also be a right child) compare w/ Figure 13.5 color flip: x’s parent, uncle, and grandparent all change color

5 Red-black tree insertion: Case 3: New red node’s parent is red and uncle is black ; x is a left child & p(x) is a left child [a left/left situation] a newly red node (caused by either direct insertion or violation propagation from below) b See Fig. 13.6 a b x C A B B x = A Change color : p(x) to black - p(p(x)) to red Rotate right: grab p(x) and shake; p(p(x)) drops down to the right; right subtree of p(x) slides down to become a left subtree C

6 newly red node (caused by propagation from below)
Red-black tree insertion: Case 2: New red node’s parent is red and uncle is black; x is a right child & p(x) is a left child [a right/left situation] Reduce to Case 3! newly red node (caused by propagation from below) b b a x a x D D C A A B B C Rotate left: grab x and shake; p(x) drops down to the left; left subtree of x slides down to become a right subtree Note that this rotation grabs tree at x, whereas Case 3 grabs tree up one level at p(x).

7 Red-black tree insertion: Case 2: continued
was x; now is p(a ) b now acts as x a b D a C A B A B C D Now apply Case 3 at a

8 Other Cases for red x, red parent, black uncle
If x is right child and p(x) is right child Change colors: p(x) to black; p(p(x)) to red Rotate left (a right/right form of Case 3) If x is left child and p(x) is right child Rotate right Apply case above (a left/right form of Case 2) b a C B x A b a C B x A

9 Example: Inserting the sequence 1 9 2 8 3 7 4 6
No property is violated, so there’s nothing to do.

10 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Insert 9 and flip color of root

11 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Insert 2 as left child of 9. This causes a red/red violation! Plan: Apply a left/right Case 2: - Rotate right (grab 2) - Apply right/right form of Case 3

12 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Apply a left/right Case 2: - Rotate right (grab 2) - Now we have a right/right form of Case 3

13 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Applying right/right form of Case 3 - change color of 1 and 2 - rotate left (grab 2) Red-black property restored!

14 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Red-black property violated by inserting 8. An application of case 1 (color change) will suffice here because uncle is red.

15 Example: Inserting the sequence 1 9 2 8 3 7 4 6
A color flip restores the red-black property.

16 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 3 violates the red-black property

17 Example: Inserting the sequence 1 9 2 8 3 7 4 6
This is a left/left form of case 3, so change colors and rotate right to restore (grab 8).

18 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 7 causes a red/red violation; both 7’s parent and uncle are red, so a color flip suffices …

19 Example: Inserting the sequence 1 9 2 8 3 7 4 6
The color flip performed: 3, 9, and 8 all flip. This forms a red/red violation above…

20 Example: Inserting the sequence 1 9 2 8 3 7 4 6
To fix the red/red violation above, recolor the root to black.

21 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 4 causes a red/red violation; this is a left/right form of Case 2

22 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Apply left/right form of Case 2; perform a right rotation (grab 4) to produce a right/right form of Case 3

23 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Apply right/right form of Case 3; change colors and rotate left (grab 4)

24 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 6 causes a red/red violation … which can be fixed by a color flip (it’s a case 1 violation)

25 Example: Inserting the sequence 1 9 2 8 3 7 4 6
But flipping the colors of 6’s parent, uncle, and grandparent causes another tree violation at 4 ...

26 Example: Inserting the sequence 1 9 2 8 3 7 4 6
This is left/right Case 2: first grab at 4 and rotate right; this reduces the situation to right/right Case 3

27 Example: Inserting the sequence 1 9 2 8 3 7 4 6
Now apply right/right Case 3: change colors and rotate left (grab 4)

28 Inserting 1 9 2 8 3 7 4 6 into a binary search tree without balancing!


Download ppt "Red-black tree properties"

Similar presentations


Ads by Google