























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of balanced trees, specifically focusing on 2-3-4 trees and their evolution into red-black trees. Learn about the properties, insertion process, and time complexity of these tree structures.
Typology: Lecture notes
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























b e h n r
a (^) c d f g (^) i l m p s x
--- (^) log ( N + 1 ) ≤ height ≤ log( N + 1 )
k
b e h n r
on each level requires O(log N 1/2^ ) by binary search
as we had hoped for!
using only 2, 3, or 4 children per node
get O(log (^) d N) search time! Right? log dN = log N/log d
g (^) d d g
f^ d g^ d f g
As we travel down the tree, if we encounter any 4-node we will break it up into 2-nodes. This guarantees that we will never have the problem of inserting the middle element of a former 4- node into its parent 4-node.
a (^) f i l p r x
a (^) f i l p r
g
g
n
c (^) t
x
c n t
Whoa, cowboy
a (^) f i l p r x
a (^) f i l p r x
c (^) t
n
n
c t
g
g
Whoa, cowboy
Time complexity:
Notes:
What do we know about 2-3-4 Trees?
Can we get 2-3-4 tree advantages in a binary
tree format???
Welcome to the world of Red-Black Trees!!!
Siskel
Ebert
Roberto
Note how 2-3-4 trees relate to red-black trees
2-3-4 (^) Red-Black
Now we see red-black trees are just a way of representing 2-3-4 trees!
or
N # of internal nodes L # leaves (= N + 1) H height B black height
Property 1: 2 B^ ≤ N + 1 ≤ 4 B
Property 2:
This implies that searches take time O(logN)!
Property 3:
--- (^) log ( N + 1 ) ≤ B ≤ log( N + 1 )
log ( N + 1 ) ≤ H ≤ 2 log( N + 1 )
Let: n be the new node p be its parent g be its grandparent
Case 1: Incoming edge of p is black
g
p
n
Pretty easy, huh?
Well... it gets messier...
No violation
We call this a “ right rotation ”
Case 2: Incoming edge of p is red, and
its sibling is black
g
p
n
g
p
n
Uh oh!
Much Better!
What if the new node is between its parent and grandparent in the inorder sequence?
We must perform a “double rotation”
(which is no more difficult than a “single” one)
g
p
n
p g
n
This would be called a “left-right double rotation”
And this would be called a “right-left double rotation”
g
p
n
g p
n