RB Trees
RB Trees
Spring 2024
eduko.spikotech.com
14/10/2024 9:51 pm 2
No matter what lives underneath A,B,C,
Idea 1: Rotations this takes time O(1). (Why?)
YOINK! X Y Y
Y C A B X A X
A B C
B fell
down. B C
CLAIM:
14/10/2024 9:51 pm this still has BST property. 3
This seems helpful
3 YOINK!
5
2 5 3 7
4 7 2 4 6 8
6 8
14/10/2024 9:51 pm 4
Strategy?
• Whenever something seems unbalanced, do rotations until it’s okay
again.
14/10/2024 9:51 pm 5
Idea 2: have some proxy for balance
• Maintaining perfect balance is too hard.
• Instead, come up with some proxy for balance:
• If the tree satisfies [SOME PROPERTY], then it’s pretty balanced.
• We can maintain [SOME PROPERTY] using rotations.
14/10/2024 9:51 pm 6
RB Tree Introduction
CLRS Chapter 13 (13.1)
14/10/2024 9:51 pm 7
Red-Black Trees
• A Binary Search Tree that balances itself!
• No more time-consuming by-hand
balancing!
• Be the envy of your friends and neighbors
with the time-saving…
5
3 7
Maintain balance by stipulating that
black nodes are balanced, and 2 4 6 8
that there aren’t too many red
nodes.
It’s just good sense!
14/10/2024 9:51 pm 8
Red-Black Trees
obey the following rules (which are a proxy for balance)
• Every node is colored red or black.
• The root node is a black node.
• NIL children count as black nodes.
• Children of a red node are black nodes. 5
• For all nodes x:
• all paths from x to NIL’s have the same 3 7
number of black nodes on them.
2 4 6 8
I’m not going to draw the NIL
children in the future, but they NIL NIL NIL NIL NIL NIL NIL NIL
14/10/2024 9:51 pm 9
are treated as black nodes.
• Every node is colored red or black.
Examples(?) • The root node is a black node.
• NIL children count as black nodes.
• Children of a red node are black nodes.
• For all nodes x:
• all paths from x to NIL’s have the same
Which of these number of black nodes on them.
are red-black trees?
Yes! (NIL nodes not drawn)
1 minute think
1 minute pair+share
14/10/2024 9:51 pm 10
Why these rules???????
5
• This is pretty balanced.
• The black nodes are balanced 3 7
• The red nodes are “spread out”
so they don’t mess things up
too much.
2 4 6 8
• We can maintain this property
as we insert/delete nodes, by
using rotations.
9
This is the really clever idea!
This Red-Black structure is a proxy for balance.
It’s just a smidge weaker than perfect balance, but we can actually maintain it!
14/10/2024 9:51 pm 11
Let’s build some intuition!
Conjecture:
the height of a red-black tree
with n nodes is at most 2 log(n)
14/10/2024 9:51 pm 12
The height of a RB-tree with n non-NIL nodes
is at most 2log(𝑛𝑛 + 1) x
14/10/2024 9:51 pm 14
Rotation
CLRS Chapter 13 (13.2)
14/10/2024 9:51 pm 15
Rotations
14/10/2024 9:51 pm 16
14/10/2024 9:51 pm 17
14/10/2024 9:51 pm 18
Tree Insertion
CLRS Chapter 13 (13.3)
14/10/2024 9:51 pm 19
Case 1: Z’s uncle is red
14/10/2024 9:51 pm 20
Case 2: Z’s uncle is Black is z is right child
14/10/2024 9:51 pm 21
14/10/2024 9:51 pm 22
Case 3: Z’s Uncle is Black and Z is left child
14/10/2024 9:51 pm 23
14/10/2024 9:51 pm 24
Tree Insert
14/10/2024 9:51 pm 25
Deletion
CLRS Chapter 13 (13.4)
14/10/2024 9:51 pm 26
Deleting from a Red-Black tree
Fun exercise!
14/10/2024 9:51 pm 28
Conclusion: The best of both worlds
Binary Search
Sorted Arrays Linked Lists
Trees*
14/10/2024 9:51 pm 30