red black tree
DESCRIPTION
BST, RBT, TreeTRANSCRIPT
![Page 1: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/1.jpg)
Algorithms
Sandeep Kumar PooniaHead Of Dept. CS/IT
B.E., M.Tech., UGC-NET
LM-IAENG, LM-IACSIT,LM-CSTA, LM-AIRCC, LM-SCIEI, AM-UACEE
![Page 2: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/2.jpg)
Algorithms
BST
Red Black Tree
![Page 3: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/3.jpg)
Binary search trees
● Binary search trees are an important data
structure for dynamic sets.
● Accomplish many dynamic-set operations in
O(h) time, where h=height of tree.
● we represent a binary tree by a linked data
structure in which each node is an object.
● T:root points to the root of tree T .
![Page 4: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/4.jpg)
Binary search trees
● Each node contains the attributes
■ key (and possibly other satellite data).
■ left: points to left child.
■ right: points to right child.
■ p: points to parent. T.root.p = NIL.
![Page 5: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/5.jpg)
Binary search trees
● Stored keys must satisfy the binary-search-
tree property.
■ If y is in left subtree of x, then y.key <= x.key.
■ If y is in right subtree of x, then y.key >= x.key.
![Page 6: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/6.jpg)
Binary search trees
(a) A binary search tree on 6 nodes with height 2.
(b) A less efficient binary search tree with height 4 that
contains the same keys.
![Page 7: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/7.jpg)
Binary Search Trees
A binary search tree Not a binary search tree
![Page 8: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/8.jpg)
Binary search trees
● The binary-search-tree property allows us to
print out all the keys in a binary search tree in
sorted order by a simple recursive algorithm,
called an inorder tree walk.It takes ‚ time to
walk an n-node
binary search tree
![Page 9: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/9.jpg)
Binary search trees
● A: prints elements in sorted (increasing) order
● This is called an inorder tree walk
■ Preorder tree walk: print root, then left, then right
■ Postorder tree walk: print left, then right, then root
![Page 10: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/10.jpg)
Tree traversal
● Used to print out the data in a tree in a certain
order
● Pre-order traversal
■ Print the data at the root
■ Recursively print out all data in the left subtree
■ Recursively print out all data in the right subtree
![Page 11: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/11.jpg)
Preorder, Postorder and Inorder
● Preorder traversal
■ node, left, right
■ prefix expression
○ ++a*bc*+*defg
![Page 12: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/12.jpg)
Preorder, Postorder and Inorder
● Postorder traversal
■ left, right, node
■ postfix expression
○ abc*+de*f+g*+
● Inorder traversal
■ left, node, right.
■ infix expression
○ a+b*c+d*e+f*g
![Page 13: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/13.jpg)
Binary search trees
the running time of
TREE-SEARCH is
O(h), where h is
the height of the
tree.
![Page 14: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/14.jpg)
Binary search trees
![Page 15: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/15.jpg)
![Page 16: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/16.jpg)
insert
● Proceed down the tree as you would with a find
● If X is found, do nothing (or update something)
● Otherwise, insert X at the last spot on the path traversed
● Time complexity = O(height of the tree)
![Page 17: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/17.jpg)
delete
● When we delete a node, we need to consider
how we take care of the children of the deleted
node.
■ This has to be done such that the property of the
search tree is maintained.
![Page 18: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/18.jpg)
delete
Three cases:
(1) the node is a leaf
■ Delete it immediately
(2) the node has one child
■ Adjust a pointer from the parent to bypass that node
![Page 19: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/19.jpg)
delete
(3) the node has 2 children
■ replace the key of that node with the minimum element at the right subtree
■ delete the minimum element
○ Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2.
● Time complexity = O(height of the tree)
![Page 20: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/20.jpg)
Balanced Binary Search Trees
A binary search tree can implement any of the basic dynamic-setoperations in O(h) time. These operations are O(lgn) if tree is“balanced”.
There has been lots of interest in developing algorithms to keep binarysearch trees balanced, including
1st type: insert nodes as is done in the BST insert, then rebalance tree Red-Black trees AVL trees Splay trees
2nd type: allow more than one key per node of the search tree: 2-3 trees 2-3-4 trees B-trees
![Page 21: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/21.jpg)
Red-Black Trees (RBT)
Red-Black tree: BST in which each node is colored red or black.
Constraints on the coloring and connection of nodes ensure thatno root to leaf path is more than twice as long as any other, sotree is approximately balanced.
Each RBT node contains fields left, right, parent, color, and key.
L
E
F
T
PARENT R
I
G
H
T
KEY
COLOR
![Page 22: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/22.jpg)
Red-Black Trees
● Red-black trees:
■ Binary search trees augmented with node color
■ Operations designed to guarantee that the height
h = O(lg n)
● First: describe the properties of red-black trees
● Then: prove that these guarantee h = O(lg n)
● Finally: describe operations on red-black trees
![Page 23: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/23.jpg)
Red-Black Properties
● The red-black properties:
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
○ Note: this means every “real” node has 2 children
3. If a node is red, both children are black
○ Note: can’t have 2 consecutive reds on a path
4. Every path from node to descendent leaf contains
the same number of black nodes
5. The root is always black
![Page 24: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/24.jpg)
Red-Black Trees
● Put example on board and verify properties:
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf contains
the same number of black nodes
5. The root is always black
● black-height: # black nodes on path to leaf
■ Label example with h and bh values
![Page 25: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/25.jpg)
Black Height bh(x)
Black-height of a node x: bh(x) is the number of black nodes (including
the NIL leaf) on the path from x to a leaf, not counting x itself.
20
22
2521
18
17 19
2
1 1 1
2
1
1
Every node has ablack-height, bh(x).
For all NIL leaves,bh(x) = 0.
For root x,bh(x) = bh(T).
0 0 0 0 0 0 0 0
![Page 26: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/26.jpg)
Height of a Red-black Tree
● Example:
● Height of a node:
■ Number of edges in a
longest path to a leaf.
● Black-height of a node
bh(x) is the number of
black nodes on path
from x to leaf, not
counting x.
26
17
30 47
38 50
41
nil[T]
h=4
bh=2
h=3
bh=2
h=2
bh=1
h=2
bh=1h=1
bh=1
h=1
bh=1
h=1
bh=1
![Page 27: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/27.jpg)
Height of Red-Black Trees
● What is the minimum black-height of a node
with height h?
● A height-h node has black-height h/2
● Proof : By property 4,
● h/2 nodes on the path from the node to a leaf
are red.
● Hence are black.
![Page 28: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/28.jpg)
Height of Red-Black Trees
● The subtree rooted at any node x contains >= 2bh(x)_1
internal nodes.
● Proof :By induction height of x =0x is a leafbh(x)=0.
The subtree rooted at x has 0 internal nodes. 20 -1 = 0.
● Let the height of x be h and bh(x) = b.
● Any child of x has height h -1 and
● black-height either b (if the child is red) or
● b -1 (if the child is black)
● By induction each child has >= 2bh(x)-1-1 internal nodes
● Thus, the subtree rooted at x contains >= 2(2bh(x)-1-1)+1
● = 2bh(x)-1(internal Nodes)
![Page 29: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/29.jpg)
Height of Red-Black Trees
● Theorem: A red-black tree with n internal
nodes has height h 2 lg(n + 1)
● How do you suppose we’ll prove this?
● Proof: The subtree rooted at any node x contains
● >= 2bh(x)_1 internal nodes.
● A height-h node has black-height h/2
● Thus n 2h/2 -1
● n + 1 2h/2
● lg(n+1) h/2 h 2lg(n+1)
![Page 30: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/30.jpg)
RB Trees: Worst-Case Time
● So we’ve proved that a red-black tree has
O(lg n) height
● Corollary: These operations take O(lg n) time:
■ Minimum(), Maximum()
■ Successor(), Predecessor()
■ Search()
● Insert() and Delete():
■ Will also take O(lg n) time
■ But will need special care since they modify tree
![Page 31: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/31.jpg)
Red-Black Trees: An Example
● Color this tree: 7
5 9
1212
5 9
7
Red-black properties:
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
![Page 32: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/32.jpg)
● Insert 8
■ Where does it go?
Red-Black Trees:
The Problem With Insertion
12
5 9
7
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
![Page 33: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/33.jpg)
● Insert 8
■ Where does it go?
■ What color
should it be?
Red-Black Trees:
The Problem With Insertion
12
5 9
7
8
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
![Page 34: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/34.jpg)
● Insert 8
■ Where does it go?
■ What color
should it be?
Red-Black Trees:
The Problem With Insertion
12
5 9
7
8
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
![Page 35: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/35.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
![Page 36: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/36.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
11
![Page 37: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/37.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Can’t be red! (#3)
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
11
![Page 38: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/38.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Can’t be red! (#3)
○ Can’t be black! (#4)
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
11
![Page 39: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/39.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Solution:
recolor the tree
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
11
![Page 40: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/40.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
11
![Page 41: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/41.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
■ What color?
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
12
5 9
7
8
11
10
![Page 42: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/42.jpg)
Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
■ What color?
○ A: no color! Tree
is too imbalanced
○ Must change tree structure
to allow recoloring
■ Goal: restructure tree in
O(lg n) time
12
5 9
7
8
11
10
![Page 43: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/43.jpg)
RB Trees: Rotation
● Our basic operation for changing tree structure
is called rotation:
● Does rotation preserve inorder key ordering?
● What would the code for rightRotate()
actually do?
y
x C
A B
x
A y
B C
rightRotate(y)
leftRotate(x)
![Page 44: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/44.jpg)
rightRotate(y)
RB Trees: Rotation
● A lot of pointer manipulation
■ x keeps its left child
■ y keeps its right child
■ x’s right child becomes y’s left child
■ x’s and y’s parents change
y
x C
A B
x
A y
B C
![Page 45: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/45.jpg)
rightRotate(y)
RB Trees: Rotation
● A lot of pointer manipulation
■ x keeps its left child
■ y keeps its right child
■ y’s right child becomes x’s left child
■ x’s and y’s parents change
y
x C
A B
x
A y
B C
leftRotate(x)
![Page 46: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/46.jpg)
RB Trees: Rotation
Left-Rotate (T, x)
1. y right[x] // Set y.
2. right[x] left[y] //Turn y’s left subtree into x’s right subtree.
3. if left[y] nil[T ]
4. then p[left[y]] x
5. p[y] p[x] // Link x’s parent to y.
6. if p[x] == nil[T ]
7. then root[T ] y
8. else if x == left[p[x]]
9. then left[p[x]] y
10. else right[p[x]] y
11. left[y] x // Put x on y’s left.
12. p[x] y
![Page 47: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/47.jpg)
RB Trees: Rotation
right-Rotate (T, y)
1. x right[y]
2. left[y] right[x]
3. If right[x] nil[T ]
4. then p[right[x]] y
5. p[x] p[y]
6. if p[y] == nil[T ]
7. then root[T ] x
8. else if y == left[p[y]]
9. then left[p[y]] x10. else right[p[y]] x11. right[x] y
12. p[y] x
![Page 48: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/48.jpg)
Rotation Example
● Rotate left about 9:
12
5 9
7
8
11
![Page 49: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/49.jpg)
Rotation Example
● Rotate left about 9:
5 12
7
9
118
![Page 50: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/50.jpg)
Red-Black Trees: Insertion
● Remember:
1. Insert nodes one at a time, and after every
Insertion balance the tree.
2. Every node inserted starts as a Red node.
3. Consult the cases, Every time two Red nodes
touch must rebalance at that point.
4. The root will always be Black.
![Page 51: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/51.jpg)
Red-Black Trees: Insertion
● If we insert, what color to make the new node?
■ Red? Might violate property 3(If a node is red, both
children are black).
■ Black? Might violate property 4(Every path from node
to descendent leaf contains the same number of black nodes).
Insertion: the basic idea
■ Insert x into tree, color x red
■ Only r-b property 3 might be violated (if p[x] red)
○ If so, move violation up tree until a place is found where it
can be fixed
■ Total time will be O(lg n)
![Page 52: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/52.jpg)
Reminder: Red-black Properties
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
![Page 53: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/53.jpg)
Insertion in RB Trees
● Insertion must preserve all red-black properties.
● Should an inserted node be colored Red? Black?
● Basic steps:
■ Use Tree-Insert from BST (slightly modified) to
insert a node x into T.
○ Procedure RB-Insert(x).
■ Color the node x red.
■ Fix the modified tree by re-coloring nodes and
performing rotation to preserve RB tree property.
○ Procedure RB-Insert-Fixup.
![Page 54: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/54.jpg)
Algorithm: Insertion
Balanced trees: Red-black trees
We have detected a need for balance when z is red and his parent too.
• If z has a red uncle: colour the parent and uncle black, and
grandparent red.
z
![Page 55: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/55.jpg)
Algorithm: Insertion
Balanced trees: Red-black trees
We have detected a need for balance when z is red and his parent too.
• If z has a red uncle: colour the parent and uncle black, and
grandparent red.• If z is a left child and has a black uncle: colour the parent black and
the grandparent red, then rotateRight(z.parent.parent)
![Page 56: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/56.jpg)
Algorithm: Insertion
Balanced trees: Red-black trees
We have detected a need for balance when z is red and his parent too.
• If z has a red uncle: colour the parent and uncle black, and
grandparent red.• If z is a left child and has a black uncle: colour the parent black and
the grandparent red, then rotateRight(z.parent.parent)
• If z is a right child and has a black uncle, then rotateLeft(z.parent)
and
![Page 57: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/57.jpg)
Algorithm: Insertion
Balanced trees: Red-black trees
4
7
1
2Double red violation!
It also shows it’s
unbalanced…
Let’s insert 4, 7, 12, 15, 3 and 5.
![Page 58: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/58.jpg)
Algorithm: Insertion
Balanced trees: Red-black trees
7
4 12
Let’s insert 4, 7, 12, 15, 3 and 5.
15
Double red violation.
We can’t have a better
balance, and there is a
red uncle…3
What should we do?
Nothing, no
double red. 5
![Page 59: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/59.jpg)
InsertionRB-Insert(T, z)
1. y nil[T]
2. x root[T]
3. while x nil[T]
4. do y x
5. if key[z] < key[x]
6. then x left[x]
7. else x right[x]
8. p[z] y
9. if y = nil[T]
10. then root[T] z
11. else if key[z] < key[y]
12. then left[y] z
13. else right[y] z
RB-Insert(T, z) Contd.
14. left[z] nil[T]
15. right[z] nil[T]
16. color[z] RED
17. RB-Insert-Fixup (T, z)
How does it differ from the Tree-
Insert procedure of BSTs?
Which of the RB properties might
be violated?
Fix the violations by calling RB-
Insert-Fixup.
![Page 60: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/60.jpg)
Insertion – Fixup
● Which property might be violated?
1. OK(Every node is either red or black)
2. If z is the root, then there’s a violation. Otherwise,
OK(The root is black)
3. OK(Every leaf (NIL) is black)
4. If z.p is red, there’s a violation: both z and z.p are
red(If a node is red, then both its children are black)
● OK(For each node, all simple paths from the node to descendant
leaves contain the same number of black nodes)
● Remove the violation by calling RB-INSERT-FIXUP:
![Page 61: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/61.jpg)
Insertion – Fixup
● Problem: we may have one pair of consecutive
reds where we did the insertion.
● Solution: rotate it up the tree and away…
Three cases have to be handled…
![Page 62: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/62.jpg)
Insertion – Fixup
RB-Insert-Fixup (T, z)
1. while color[p[z]] == RED
2. do if p[z] == left[p[p[z]]]
3. then y right[p[p[z]]]
4. if color[y] == RED
5. then color[p[z]] BLACK // Case 1
6. color[y] BLACK // Case 1
7. color[p[p[z]]] RED // Case 1
8. z p[p[z]] // Case 1
![Page 63: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/63.jpg)
Insertion – Fixup
RB-Insert-Fixup(T, z) (Contd.)
9. else if z == right[p[z]] // color[y] RED
10. then z p[z] // Case 2
11. LEFT-ROTATE(T, z) // Case 2
12. color[p[z]] BLACK // Case 3
13. color[p[p[z]]] RED // Case 3
14. RIGHT-ROTATE(T, p[p[z]]) // Case 3
15. else (if p[z] = right[p[p[z]]])(same as 10-14
16. with “right” and “left” exchanged)
17. color[root[T ]] BLACK
![Page 64: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/64.jpg)
Insertion – Fixup
A node z ’after insertion. Because both z and its parent z.p are red, a
violation of property 4 occurs. Since z’s uncle y is red, case 1 in the
code applies. We recolor nodes and move the pointer z up the tree
![Page 65: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/65.jpg)
Insertion – Fixup
Once again, z and its parent are both red, but z’s uncle y
is black. Since z is the right child of z.p, case 2 applies.
We perform a left rotation,
![Page 66: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/66.jpg)
Insertion – Fixup
Now, z is the left child of its parent, and case 3 applies.
Recoloring and right rotation yield the tree
![Page 67: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/67.jpg)
Insertion – Fixup
![Page 68: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/68.jpg)
Correctness
Loop invariant:
● At the start of each iteration of the while loop,
■ z is red.
■ If p[z] is the root, then p[z] is black.
■ There is at most one red-black violation:
○ Property 2: z is a red root, or
○ Property 4: z and p[z] are both red.
![Page 69: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/69.jpg)
Correctness – Contd.
● Initialization:
● Termination: The loop terminates only if p[z] is
black. Hence, property 4 is OK.
The last line ensures property 2 always holds.
● Maintenance: We drop out when z is the root (since
then p[z] is sentinel nil[T ], which is black). When we
start the loop body, the only violation is of property 4.
■ There are 6 cases, 3 of which are symmetric to the other 3.
We consider cases in which p[z] is a left child.
■ Let y be z’s uncle (p[z]’s sibling).
![Page 70: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/70.jpg)
Case 1 – uncle y is red
● p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red and there are no other violations of property 4.
● Make p[z] and y black now z and p[z] are not both red. But property 5
might now be violated.
● Make p[p[z]] red restores property 5.
● The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
C
A D
B
z
y
C
A D
B
new z
z is a right child here.
Similar steps if z is a left child.
p[z]
p[p[z]]
![Page 71: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/71.jpg)
Case 1 – uncle y is red
We take the same action whether z is a right child or z is a left child.
Each subtree has a black root, and each has the same black-height.
The code for case 1 changes the colors of some nodes, preserving property
5: all downward simple paths from a node to a leaf have the same number
of blacks. The while loop continues with node z’s grandparent z.p.p as
the new z. Any violation of property 4 can now occur only between the new
z, which is red, and its parent, if it is red as well.
![Page 72: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/72.jpg)
Case 2 – y is black, z is a right child
● Left rotate around p[z], p[z] and z switch roles now z is a left
child, and both z and p[z] are red.
● Takes us immediately to case 3.
C
A
B
z
y
C
B
A
z
y
p[z]p[z]
![Page 73: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/73.jpg)
Case 3 – y is black, z is a left child
● Make p[z] black and p[p[z]] red.
● Then right rotate on p[p[z]]. Ensures property 4 is maintained.
● No longer have 2 reds in a row.
● p[z] is now black no more iterations.
B
A
C
B
A
y
p[z]
C
z
![Page 74: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/74.jpg)
Cases 2 and 3 of the procedure RB-INSERT-FIXUP
As in case 1, property 4 is violated in either case 2 or case 3 because z
and its parent z.p are both red. Each subtree has a black root , and each
has the same black-height. We transform case 2 into case 3 by a left
rotation, which preserves property 5: all downward simple paths from a
node to a leaf have the same number of blacks. Case 3 causes some
color changes and a right rotation, which also preserve property 5. The
while loop then terminates, because property 4 is satisfied: there are no
longer two red nodes in a row.
![Page 75: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/75.jpg)
Algorithm Analysis
● O(lg n) time to get through RB-Insert up to the
call of RB-Insert-Fixup.
● Within RB-Insert-Fixup:
■ Each iteration takes O(1) time.
■ Each iteration but the last moves z up 2 levels.
■ O(lg n) levels O(lg n) time.
■ Thus, insertion in a red-black tree takes O(lg n) time.
■ Note: there are at most 2 rotations overall.
![Page 76: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/76.jpg)
Deletion● Deletion, like insertion, should preserve all the
RB properties.
● The properties that may be violated depends on
the color of the deleted node.
■ Red – OK. Why?
■ Black?
● Steps:
■ Do regular BST deletion.
■ Fix any violations of RB properties that may result.
![Page 77: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/77.jpg)
Deletion
RB-Delete(T, z)
1. if left[z] == nil[T] or right[z] == nil[T]
2. then y z
3. else y TREE-SUCCESSOR(z)
4. if left[y] == nil[T ]
5. then x left[y]
6. else x right[y]
7. p[x] p[y] // Do this, even if x is nil[T]
![Page 78: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/78.jpg)
Deletion
RB-Delete (T, z) (Contd.)
8. if p[y] == nil[T ]
9. then root[T ] x
10. else if y == left[p[y]]
11. then left[p[y]] x
12. else right[p[y]] x
13. if y == z
14. then key[z] key[y]
15. copy y’s satellite data into z
16. if color[y] == BLACK
17. then RB-Delete-Fixup(T, x)
18. return y
The node passed to
the fixup routine is the
lone child of the
spliced up node, or
the sentinel.
![Page 79: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/79.jpg)
RB Properties Violation
● If the delete node is red?
Not a problem – no RB properties violated
● If y is black, we could have violations of red-
black properties:
■ Prop. 1. OK.
■ Prop. 2. If y is the root and x is red, then the root has
become red.
■ Prop. 3. OK.
■ Prop. 4. Violation if p[y] and x are both red.
■ Prop. 5. Any path containing y now has 1 fewer black
node.
![Page 80: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/80.jpg)
RB Properties Violation
● Prop. 5. Any path containing y now has 1 fewer black node.
■ Correct by giving x an “extra black.”
■ Add 1 to count of black nodes on paths containing x.
■ Now property 5 is OK, but property 1 is not.
■ x is either doubly black (if color[x] == BLACK) or red & black (if color[x] == RED).
■ The attribute color[x] is still either RED or BLACK. No new values for color attribute.
■ In other words, the extra blackness on a node is by virtue of x pointing to the node.
● Remove the violations by calling RB-Delete-Fixup.
![Page 81: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/81.jpg)
Deletion – Fixup
RB-Delete-Fixup(T, x)
1. while x root[T ] and color[x] == BLACK
2. do if x == left[p[x]]
3. then w right[p[x]]
4. if color[w] == RED
5. then color[w] BLACK // Case 1
6. color[p[x]] RED // Case 1
7. LEFT-ROTATE(T, p[x]) // Case 1
8. w right[p[x]] // Case 1
![Page 82: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/82.jpg)
RB-Delete-Fixup(T, x) (Contd.)
/* x is still left[p[x]] */
9. if color[left[w]] == BLACK and color[right[w]] == BLACK
10. then color[w] RED // Case 2
11. x p[x] // Case 2
12. else if color[right[w]] == BLACK
13. then color[left[w]] BLACK // Case 3
14. color[w] RED // Case 3
15. RIGHT-ROTATE(T,w) // Case 3
16. w right[p[x]] // Case 3
17. color[w] color[p[x]] // Case 4
18. color[p[x]] BLACK // Case 4
19. color[right[w]] BLACK // Case 4
20. LEFT-ROTATE(T, p[x]) // Case 4
21. x root[T ] // Case 4
22. else (same as then clause with “right” and “left” exchanged)
23. color[x] BLACK
![Page 83: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/83.jpg)
Deletion – Fixup
● Idea: Move the extra black up the tree until x points
to a red & black node turn it into a black node,
● x points to the root just remove the extra black, or
● We can do certain rotations and recolorings and
finish.
● Within the while loop:
■ x always points to a nonroot doubly black node.
■ w is x’s sibling.
■ w cannot be nil[T ], since that would violate property 5 at
p[x].
● 8 cases in all, 4 of which are symmetric to the other.
![Page 84: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/84.jpg)
Case 1 – w is red
● w must have black children.
● Make w black and p[x] red (because w is red p[x] couldn’t have been red).
● Then left rotate on p[x].
● New sibling of x was a child of w before rotation must be black.
● Go immediately to case 2, 3, or 4.
B
A D
C E
B
A
x wD
C
E
x new
w
p[x]
![Page 85: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/85.jpg)
Case 2 – w is black, both w’s children
are black
● Take 1 black off x ( singly black) and off w ( red).
● Move that black to p[x].
● Do the next iteration with p[x] as the new x.
● If entered this case from case 1, then p[x] was red new x is
red & black color attribute of new x is RED loop
terminates. Then new x is made black in the last line.
B
A D
C E
x wB
A D
C E
new xcc
p[x]
![Page 86: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/86.jpg)
Case 3 – w is black, w’s left child is
red, w’s right child is black
● Make w red and w’s left child black.
● Then right rotate on w.
● New sibling w of x is black with a red right child case 4.
B
A D
C E
x wB
A C
D
cc
E
new wx
![Page 87: Red black tree](https://reader034.vdocuments.us/reader034/viewer/2022052523/556d0c8dd8b42ad34f8b4d67/html5/thumbnails/87.jpg)
Case 4 – w is black, w’s right child is
red
● Make w be p[x]’s color (c).
● Make p[x] black and w’s right child black.
● Then left rotate on p[x].
● Remove the extra black on x ( x is now singly black) without
violating any red-black properties.
● All done. Setting x to root causes the loop to terminate.
B
A D
C E
B
A
x wD
C
E
x
c
c’