Binary Search Trees and Red-Black Trees
Binary Search Trees and Red-Black Trees
• HW partners
ures
2l
e ct
5l Randomized
ec
tu
Algs
re
s
MIDTERM
str
Asymptotic
Da ture
1 st
uc
cla
ta s
ss Analysis
Sorting
Divide and e n ces
Re curr
conquer MIDTERM
Dynamic
Greedy Algs Programming
MIDTERM
Longe Graphs!
st, Sh MIDTERM
Max a ortes
nd M t,
1l
in… l e c tures The
10
ec
tur
Future!
e
But first!
• A brief wrap-up of divide and conquer.
Divide and
Conquer: Big problem
Smaller Smaller
problem problem
Recurse! Recurse!
• Practice helps!
• The examples we see in Lecture and in HW are meant to
help you practice this skill.
• There are even more algorithms in the optional texts!
• Check out CLRS Chapter 4, or Algorithms Illuminated
Chapter 3 for even more examples of divide and conquer
algorithms.
More detailed schedule on the website!
ures
2l
e ct
5l Randomized
ec
tu
Algs
re
s
MIDTERM
str
Asymptotic
Da ture
1 st
uc
cla
ta s
ss Analysis
Sorting
Divide and e n ces
Re curr
conquer MIDTERM
Dynamic
Greedy Algs Programming
MIDTERM
Longe Graphs!
st, Sh MIDTERM
Max a ortes
nd M t,
1l
in… l e c tures The
10
ec
tur
Future!
e
Today
• Begin a brief foray into data structures!
• See CS 166 for more!
• Binary search trees
• You may remember these from CS 106B
• They are better when they’re balanced.
• (Sorted) arrays:
1 2 3 4 5 7 8
• Linked lists:
HEAD 3 2 1 8 5 7 4
• O(n) INSERT/DELETE:
• First, find the relevant element (we’ll see how below), and
then move a bunch elements in the array:
1 2 3 4 54.5 7 8
eg, insert 4.5
• O(log(n)) SEARCH:
1 2 3 4 5 7 8
eg, Binary search to see if 3 is in A.
(Not necessarily sorted)
Linked lists
7 5 3 4 1 2 8
• O(1) INSERT:
eg, insert 6
HEAD 7 5 3 4 1 2 8
• O(n) SEARCH/DELETE:
HEAD 7 5 3 4 1 2 8
eg, search for 1 (and then you could delete it by manipulating pointers).
Motivation for Binary Search Trees
TODAY!
(Balanced)
The parent of 3 is 5 3 7
2 is a descendant of 5
Each node has a pointer to its 2 4 8
left child, right child, and parent.
Both children of 1 are NIL.
(I won’t usually draw them). 1 These nodes
The height of this tree is 3.
(Max number of edges from the NIL NIL are leaves.
root to a leaf).
From your pre-lecture exercise…
3 4
5
8 7
1
2
From your pre-lecture exercise…
3 4
5
8 7
1
2
From your pre-lecture exercise…
3 5
1
4 7
2
8
From your pre-lecture exercise…
3 7
1
2 4 8
From your pre-lecture exercise…
1
Aside: this should look familiar
kinda like QuickSort
3 4
5
8 7
1
2
Which of these is a BST?
1 minute Think-Pair-Share
Binary Search Trees
• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
5 5
3 7 3 7
2 4 8 2 4 8
NOT a Binary
1 Binary Search Tree 1 Search Tree
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
2
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
2
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
2
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4 5
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4 5 7
Aside: In-Order Traversal of
BSTs
• Output all the elements in sorted order!
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
• Runs in time O(n). 2 3 4 5 7 Sorted!
Back to the goal
Fast SEARCH/INSERT/DELETE
Can we do these?
SEARCH in a Binary Search Tree
definition by example
2 4 8
Write pseudocode
2 4 8
You thought about this in your pre-
x= 2
lecture exercise too!
5 5 5 5
3 3 2
Case 1: if 3 is a leaf,
just delete it.
2
This triangle
is a cartoon
for a subtree
Write pseudocode for all of these!
3 7
Trees have depth
O(log(n)). Done! Wait a
2 4 6 8 second…
What to do?
It’s actually pretty often!
• Idea 0:
• Keep track of how deep the tree is getting.
• If it gets too tall, re-do everything from scratch.
• At least Ω(n) every so often….
YOINK! X Y Y
Y C A B X A X
B fell
A B That’s not
bin ary!!
C down. B C
CLAIM:
this still has BST property.
This seems helpful
3 YOINK!
5
2 5 3 7
4 7 2 4 6 8
6 8
Strategy?
• Whenever something seems unbalanced, do
rotations until it’s okay again.
t r ee !
- B l a ck
R ed 3
5
7
Maintain balance by stipulating that black
nodes are balanced, and that there aren’t 2 4 6 8
too many red nodes.
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
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 share
Conjecture:
Note, this is just a
the height of a red-black tree conjecture to build
intuition! We’ll prove a
with n nodes is at most 2 log(n) rigorous statement on
the next slide.
The height of a RB-tree with n non-NIL nodes
is at most x
3 7 3 7 3 7
INSERT: Case 1 3 7
Example: insert 0
6
3 7
0
INSERT: Many cases
6 6 6
3 7 3 7 3 7
INSERT: Case 2 3 7
0
6
INSERT: Case 2 3 7
No!
0
6
We need a bit more context 3 7
-1 Example: insert 0
3 7
6
We need a bit more context 3 7
-1 Example: insert 0
3 7
0
6
We need a bit more context 3 7
-1 Example: insert 0
6 Flip
colors!
3 7
0
6
But what if that was red? 3 7
-1 Example: insert 0
3 7
0
6
More context… 3 7
-3
What if it looks like this?
-1 Example: insert 0
3 7
0
6
More context… 3 7
-3
What if it looks like this?
-1 Example: insert 0
-4 -1
-2 6
3 7
Want to
insert 0
here.
Example, part I
-3
-4 -1
-2 6
3 7
0
Example, part I
-3
-4 -1
-2 6 Flip colors!
3 7
0
Example, part I
-3 Need to know how
to insert into trees
that look like this…
-4 -1
Want to
-2 6 insert 6 here.
3 7
0
INSERT: Many cases That’s this
case!
6 6 6
3 7 3 7 3 7
INSERT: Case 3 3 7
6
Example: Insert 0.
• Maybe with a
3 7 subtree below it.
0
Recall Rotations
• Maintain Binary Search Tree (BST) property, while
moving stuff around.
YOINK! X Y Y
Y C A B X A X
A B That’s not
bin ary!!
C B C
CLAIM:
this still has BST property.
6
Inserting into a Red-Black Tree 3 7
• Make a new red node.
• Insert it as you would normally.
• Fix things up if needed. What if it looks like this?
3 7 0 6
0 7
Example, part 2
-3
-4 -1 Want to
insert 6 here.
-2 6
3 7
0
Example, part 2
YOINK!
YOINK!
-3
-1
-4 -1
-3 6
-2 6
3 7
3 7 -4 -2
0 0
Example, part 2 YOINK!
-1
-3 6
3 7
-4 -2
0
Example, part 2 TA-DA!
-1
-3 6
3 7
-4 -2
0
Many cases
6 6 6
3 7 3 7 3 7
Fun exercise!
2 4 6 8
Next time
• Hashing!