red-black tree · red-black tree red-black tree is a self-balancing binary search tree (bst) where...
TRANSCRIPT
![Page 1: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/1.jpg)
Red-Black Tree
SWE2016-44
![Page 2: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/2.jpg)
Example of a Red-Black Tree
2
![Page 3: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/3.jpg)
Red-Black Tree
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:
3
1) Every node has a color either red or black
![Page 4: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/4.jpg)
Red-Black Tree
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:
4
1) Every node has a color either red or black
2) Root of tree is always black
![Page 5: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/5.jpg)
Red-Black Tree
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:
5
1) Every node has a color either red or black
2) Root of tree is always black
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child)
![Page 6: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/6.jpg)
Red-Black Tree
Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules:
6
1) Every node has a color either red or black
2) Root of tree is always black
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child)
4) Every path from root to a NULL node has same number of black nodes
![Page 7: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/7.jpg)
Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST.
→ O(n) for a skewed Binary tree.
7
![Page 8: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/8.jpg)
Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST.
→ O(n) for a skewed Binary tree.
→ Since a Red-Black Tree ensures almost balanced, the height of the tree remains O(Log n) after every insertion and deletion.
→ Red-Black Tree is not always a balanced tree.
8
![Page 9: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/9.jpg)
How does a Red-Black Tree ensure balance?
We can try any combination of colors and see all of them violate Red-Black tree property.
9
![Page 10: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/10.jpg)
How does a Red-Black Tree ensure balance?
We can try any combination of colors and see all of them violate Red-Black tree property.
10
![Page 11: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/11.jpg)
Black Height of a Red-Black Tree
Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also counted black nodes.
11
![Page 12: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/12.jpg)
Black Height of a Red-Black Tree
Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also counted black nodes.
From properties 3 (no two adjacent red nodes) and 4 (same number of black nodes),
Black-height >= h/2.
12
![Page 13: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/13.jpg)
Height of a Red-Black Tree
Every Red Black Tree with n nodes has Height <= 2Log2(n+1).
13
![Page 14: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/14.jpg)
Height of a Red-Black Tree
Every Red Black Tree with n nodes has Height <= 2Log2(n+1).
Proof)
1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).
14
![Page 15: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/15.jpg)
Height of a Red-Black Tree
Every Red Black Tree with n nodes has Height <= 2Log2(n+1).
Proof)
1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).
2) From 1) and property 4, there is a root to leaf path with at most Log2 (n+1) black nodes: h' <= Log2 (n+1)
15
![Page 16: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/16.jpg)
Height of a Red-Black Tree
Every Red Black Tree with n nodes has Height <= 2Log2(n+1).
Proof)
1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).
2) From 1) and property 4, there is a root to leaf path with at most Log2 (n+1) black nodes: h' <= Log2 (n+1)
3) Black-height is at least h/2: h' >= h/2
16
![Page 17: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/17.jpg)
Height of a Red-Black Tree
Every Red Black Tree with n nodes has Height <= 2Log2(n+1).
Proof)
1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is at least 7). That is, k <= Log2 (n+1).
2) From 1) and property 4, there is a root to leaf path with at most Log2 (n+1) black nodes: h' <= Log2 (n+1)
3) Black-height is at least h/2: h' >= h/2
4) From 2) and 3), h <= 2Log2 (n+1)17
![Page 18: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/18.jpg)
Insertion
The goal of the insert operation is to insert key K into tree T, maintaining T’s red-black tree properties
18
![Page 19: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/19.jpg)
Insertion
If T is a non-empty tree, then we do the following:
1. Use the BST insert algorithm to add K to the tree
2. Color the node containing K red
3. Restore red-black tree properties (if necessary)
19
![Page 20: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/20.jpg)
Insertion
To restore the violated property, we use:
1. Recoloring
2. Rotation (Left, Right, Double)
We try recoloring first, if recoloring doesn’t work, then we go for rotation.
20
![Page 21: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/21.jpg)
Insertion
1) Perform standard BST insertion and make the color of newly inserted nodes as RED.
2) If x is root, change color of x as BLACK (Black height of complete tree increases by 1).
3) Do following if color of x’s parent is not BLACK and x is not root.
21
![Page 22: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/22.jpg)
Insertion
a. If x’s uncle is RED (Grand parent must have been black from property 4)I. Change color of parent and uncle as BLACK.
II. color of grand parent as RED.
III. Change x = x’s grandparent, repeat steps 2 and 3 for new x.
22
![Page 23: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/23.jpg)
Insertion
b. If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g)I. Left Left Case (p is left child of g and x is left child of p)
II. Left Right Case (p is left child of g and x is right child of p)
III. Right Right Case (Mirror of case i)
IV. Right Left Case (Mirror of case ii)
23
![Page 24: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/24.jpg)
Insertion
I. Left Left Case (p is left child of g and x is left child of p)
24
rotateRight(root, g)
swap(p→color, g→color)
![Page 25: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/25.jpg)
Insertion
II. Left Right Case (p is left child of g and x is right child of p)
25
rotateLeft(root, p)
rotateRight(root, g)
swap(x→color, g→color)
![Page 26: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/26.jpg)
Insertion
III. Right Right Case (Mirror of case i)
26
rotateLeft(root, g)
swap(p→color, g→color)
![Page 27: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/27.jpg)
Insertion
IV. Right Left Case (Mirror of case ii)
27
rotateRight(root, p)
rotateLeft(root, g)
swap(x→color, g→color)
![Page 28: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/28.jpg)
Insertion Analysis
• Go up the tree performing Case 3-a), which only recolors nodes.
• If Case 3-b) occurs, perform 1 or 2 rotations, and terminate.
→ Running time: O(log n) with O(1) rotations.
28
![Page 29: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/29.jpg)
Example of Insertion
29
11
1
14
2
7
15
![Page 30: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/30.jpg)
Example of Insertion
30
11
1
14
2
7
15
Insert 1111
![Page 31: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/31.jpg)
Example of Insertion
31
11
1
14
2
7
15
Insert 1111
![Page 32: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/32.jpg)
Example of Insertion
32
11
1
14
2
7
15
Insert 1
1
11
![Page 33: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/33.jpg)
Example of Insertion
33
11
1
14
2
7
15
Insert 14
1
11
14
![Page 34: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/34.jpg)
Example of Insertion
34
11
1
14
2
7
15
Insert 2
1
11
14
2
![Page 35: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/35.jpg)
Example of Insertion
35
11
1
14
2
7
15
Insert 2
1
11
14
2
If X’s uncle is RED and X’s parent is not BLACK,
change color of parent and uncle as BLACK
![Page 36: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/36.jpg)
Example of Insertion
36
11
1
14
2
7
15
Insert 2
1
11
14
2
![Page 37: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/37.jpg)
Example of Insertion
37
11
1
14
2
7
15
Insert 2
1
11
14
2
Color of Grand parent as RED
![Page 38: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/38.jpg)
Example of Insertion
38
11
1
14
2
7
15
Insert 2
1
11
14
2
![Page 39: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/39.jpg)
Example of Insertion
39
11
1
14
2
7
15
Insert 2
1
11
14
2
As 11 is a root node, change its color to black
![Page 40: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/40.jpg)
Example of Insertion
40
11
1
14
2
7
15
Insert 2
1
11
14
2
![Page 41: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/41.jpg)
Example of Insertion
41
11
1
14
2
7
15
Insert 7
1
11
14
2
7
![Page 42: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/42.jpg)
Example of Insertion
42
11
1
14
2
7
15
Insert 7
1
11
14
2
7
Left rotate(2) and recolor nodes
![Page 43: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/43.jpg)
Example of Insertion
43
11
1
14
2
7
15
Insert 7
2
11
14
71
![Page 44: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/44.jpg)
Example of Insertion
44
11
1
14
2
7
15
Insert 7
2
11
14
71
![Page 45: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/45.jpg)
Example of Insertion
45
11
1
14
2
7
15
Insert 15
2
11
14
71 15
![Page 46: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/46.jpg)
Exercise
46
Insert 3, 1, 5, 7, 6, 8, 9, 10
![Page 47: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/47.jpg)
Exercise
47
Insert 3, 1, 5, 7, 6, 8, 9, 10
3
6
8
51 97
10
![Page 48: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/48.jpg)
Deletion
Like Insertion, recoloring and rotations are used to maintain the Red-Black properties.
In delete operation, we check color of sibling to decide the appropriate case.
48
![Page 49: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/49.jpg)
Deletion Algorithm
1. Perform standard BST delete- A node which is either leaf or has only one child is deleted
- For an internal node: Copy the successor → call delete for successor
49
![Page 50: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/50.jpg)
Deletion Algorithm
1. Perform standard BST delete- A node which is either leaf or has only one child is deleted
- For an internal node: Copy the successor → call delete for successor
2. Handle cases where a node is leaf or has one child
50
![Page 51: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/51.jpg)
Deletion Algorithm
1. Perform standard BST delete- A node which is either leaf or has only one child is deleted
- For an internal node: Copy the successor → call delete for successor
2. Handle cases where a node is leaf or has one child
3. 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
51
![Page 52: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/52.jpg)
Deletion Algorithm
a) If Either u or v is RED- Mark the replaced child as black
b) If Both u and v are BLACK- Color u as a double black
- If u is root, make it single black and return52
v
u
v
uor → u
v
u→ u
![Page 53: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/53.jpg)
a) If Either u or v is RED
b) If Both u and v are BLACK
Deletion Algorithm
53
30
20 40
10
30
10 40v
u
Delete 20
30
20 40
30
NULL 40vDelete 20
50NULL NULL 50u
u
u
![Page 54: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/54.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
- Case 2: If s is black and its both children are BLACK
- Case 3: If s is RED
54
![Page 55: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/55.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
- Let the red child of s be r
I. Left Left Case (Mirror of III)
II. Left Right Case (Mirror of IV)
III. Right Right Case
IV. Right Left Case
55
30
NULL 40
50
su
r
p
![Page 56: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/56.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
III. Right Right Case (r → outer node)
56
30
20 40v
5035u
Delete 20
![Page 57: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/57.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
III. Right Right Case (r → outer node)
57
30
20 40v
5035
30
40u
5035
NULL
u
Delete 20
s
r
p
![Page 58: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/58.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
III. Right Right Case (r → outer node)
58
30
20 40v
5035
30
40u
5035
NULL
u
Delete 20
s
r
p Rotate Left (p)
Color exchange
(p&r→s, s→p)
![Page 59: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/59.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
III. Right Right Case (r → outer node)
59
30
20 40v
5035
30
40u
5035
NULL
u
40
50
35
30
Delete 20
s
r
p Rotate Left (p)
Color exchange
(p&r→s, s→p)
![Page 60: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/60.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
60
30
20 40v
35u
Delete 20
![Page 61: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/61.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
61
30
20 40v
35
30
40u
35
NULL
u
Delete 20
s
r
p
![Page 62: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/62.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
62
30
20 40v
35
30
40u
35
NULL
u
Delete 20
s
r
p Rotate Right (s)
Color exchange (r, s)
![Page 63: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/63.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
63
30
20 40v
35
30
40u
35
NULL
u
Delete 20
s
r
p 30
35u
40
NULL r
s
pRotate Right (s)
Color exchange (r, s)
![Page 64: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/64.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
64
30
20 40v
35
30
40u
35
NULL
u
Delete 20
s
r
p 30
35u
40
NULL s
r
pRotate Right (s)
Color exchange (r, s)
Same as case III
![Page 65: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/65.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
65
30
20 40v
35
30
40u
35
NULL
u
Delete 20
s
r
p 30
35u
40
NULL s
r
pRotate Right (s)
Color exchange (r, s)
Same as case III
Rotate Left (p)
Color exchange
(p&r→s, s→p)
![Page 66: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/66.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 1: If s is black and at least one of s’s children is RED
IV. Right Left Case
66
30
20 40v
35
30
40u
35
NULL
u
Delete 20
s
r
p 30
35u
40
NULL s
r
pRotate Right (s)
Color exchange (r, s) 35
30 40
Same as case III
Rotate Left (p)
Color exchange
(p&r→s, s→p)
![Page 67: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/67.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED
2) If parent is BLACK, recur for parent
67
![Page 68: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/68.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED
2) If parent is BLACK, recur for parent
68
30
20 40v
u
Delete 20
![Page 69: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/69.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED
2) If parent is BLACK, recur for parent
69
30
20 40v
u
Delete 20
30
40u NULL s
p
![Page 70: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/70.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED
2) If parent is BLACK, recur for parent
70
30
20 40v
u
Delete 20
30
40u NULL s
ps → RED
p → DB
30
40NULL
![Page 71: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/71.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 2: If s is black and its both children are BLACK1) Recolor p to BLACK and s to RED
2) If parent is BLACK, recur for parent
71
30
20 40v
u
Delete 20
30
40u NULL s
ps → RED
p → BLACK
30
40NULL
![Page 72: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/72.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
72
![Page 73: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/73.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
73
Delete 20
30
20 40v
5035u
![Page 74: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/74.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
74
Delete 20
30
20 40v
5035u
30
NULL 40u
5035
s
p
![Page 75: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/75.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
75
Delete 20
Rotate
p → RED
s → BLACK
30
20 40v
5035u
30
NULL 40u
5035
s
p 40
50
35
30
NULL
![Page 76: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/76.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
76
Delete 20
Rotate
p → RED
s → BLACK
30
20 40v
5035u
30
NULL 40u
5035
s
p 40
50
35
30
NULLu
p
s
![Page 77: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/77.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
77
Delete 20
Rotate
p → RED
s → BLACK
30
20 40v
5035u
30
NULL 40u
5035
s
p 40
50
35
30
NULLu
p
s
Case 2
![Page 78: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/78.jpg)
Deletion Algorithm
b) If Both u and v are BLACK- Let sibling of node be s
- Case 3: If s is RED1) Rotate p, Recolor p to RED and s to BLACK
2) Case I or Case II
78
Delete 20
Rotate
p → RED
s → BLACK
30
20 40v
5035u
30
NULL 40u
5035
s
p 40
50
35
30
NULLu
p
s
Case 2 40
50
35
30
NULLu
p
s
![Page 79: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/79.jpg)
Deletion Analysis
• Case 2 is the only case in which more iterations occur.
→ u moves up 1 level. Hence, O(log n) iterations.
• Each of cases 1 and 3 has at most ≤ 3 rotations in all
→ Running time: O(log n)
79
![Page 80: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/80.jpg)
Example of Deletion (1)
80
![Page 81: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/81.jpg)
Example of Deletion (2)
81
![Page 82: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/82.jpg)
Example of Deletion (3)
82
![Page 83: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/83.jpg)
Example of Deletion (4)
83
![Page 84: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/84.jpg)
Example of Deletion (5)
84
![Page 85: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/85.jpg)
Example of Deletion (6)
85
![Page 86: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/86.jpg)
Example of Deletion (7)
86
![Page 87: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/87.jpg)
Example of Deletion (8)
87
![Page 88: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/88.jpg)
Example of Deletion (9)
88
![Page 89: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/89.jpg)
Example of Deletion (10)
89
![Page 90: Red-Black Tree · Red-Black Tree Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules: 6 1) Every node has a color either red or black](https://reader035.vdocuments.site/reader035/viewer/2022062605/5fcae4051fa17e675720f878/html5/thumbnails/90.jpg)
Reference
• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September 29, 2004
• https://www.geeksforgeeks.org