0% found this document useful (0 votes)
10 views

RB Trees

Uploaded by

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

RB Trees

Uploaded by

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

Advanced DS Data Structure and Algorithms

Spring 2024

Red Black Tree

eduko.spikotech.com

University of Engineering and Technology


Lahore Pakistan
Self-Balancing
Binary Search Trees

14/10/2024 9:51 pm 2
No matter what lives underneath A,B,C,
Idea 1: Rotations this takes time O(1). (Why?)

• Maintain Binary Search Tree (BST) property, while


moving stuff around.
Note: A, B, C, X, Y are
variable names, not the
contents of the nodes.

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.

Even for Lucky this is pretty vague.


What do we mean by “seems
unbalanced”? What’s “okay”?

Lucky the Lackadaisical Lemur

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.

There are actually several


ways to do this, but today
we’ll see…

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

No! No! No!

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!

This is “pretty balanced” Lucky the


lackadaisical
lemur

• To see why, intuitively, let’s try to build a


Red-Black Tree that’s unbalanced.

One path can be at most twice


as long another if we pad it
with red nodes.

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

• Define b(x) to be the number of black


nodes in any path from x to NIL. y z
• (excluding x, including NIL).
• Claim:
• There are at least 2b(x) – 1 non-NIL
nodes in the subtree underneath x.
(Including x).
• [Proof by induction – on board if time] NIL
Then: Claim: at least 2b(x) – 1 nodes in this
WHOLE subtree (of any color).
𝑛𝑛 ≥ 2𝑏𝑏 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟 − 1 using the Claim
ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒�
≥2 2 −1 b(root) >= height/2 because of RBTree rules.
Rearranging:
ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒�
14/10/2024 9:51 pm 𝑛𝑛 + 1 ≥ 2 2 ⇒ ℎ𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒 ≤ 2log(𝑛𝑛 + 1) 13
This is great!
• SEARCH in an RBTree is immediately O(log(n)), since the depth of an
RBTree is O(log(n)).

• What about INSERT/DELETE?


• Turns out, you can INSERT and DELETE items from an RBTree in time O(log(n)),
while maintaining the RBTree property.
• That’s why this is a good property!

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!

Ollie the over-achieving ostrich


14/10/2024 9:51 pm 27
What have we learned?
• Red-Black Trees always have height at most 2log(n+1).
• As with general Binary Search Trees, all operations are
O(height)
• So all operations with RBTrees are O(log(n)).

14/10/2024 9:51 pm 28
Conclusion: The best of both worlds

Binary Search
Sorted Arrays Linked Lists
Trees*

Search O(log(n)) O(n) O(log(n))

Delete O(n) O(n) O(log(n))

Insert O(n) O(1) O(log(n))


14/10/2024 9:51 pm 29
Thank You

14/10/2024 9:51 pm 30

You might also like