Beruflich Dokumente
Kultur Dokumente
erm
204
beh
cd
fg
lm
sx
1
--- log ( N + 1 ) height log ( N + 1 )
2
erm
205
2-node:
same as a binary node
<a
>a
a b
3-node:
<a
2 keys, 3 links
>b
>a
<b
a b c
4-node:
3 keys, 4 links
<a
>c
>a
<b
erm
206
>b
<c
Why 2-3-4?
Why not minimize height by maximizing
children in a d-tree?
Let each node have d children so that we
get O(log d N) search time! Right?
logdN = log N/log d
That means if d = N1/2, we get a height of 2
However, searching out the correct child
on each level requires O(log N1/2) by
binary search
2 log N1/2 = O(log N) which is not as good
as we had hoped for!
2-3-4-trees will guarantee O(log N) height
using only 2, 3, or 4 children per node
erm
207
dg
d f g
dg
erm
208
dfg
erm
209
fn
de
c n t
fil
g
pr
n
c
a
erm
Whoa, cowboy
pr
fil
210
n
c
pr
fil
n
c
t
Whoa, cowboy
erm
fil
pr
211
n
c
t
Whoa, cowboy
pr
fil
n
ci
pr
l
n
ci
erm
fg
pr
212
erm
213
Siskel
Ebert
Roberto
erm
214
Red-Black Tree
A red-black tree is a binary search tree with the
following properties:
edges are colored red or black
no two consecutive red edges
on any root-leaf path
same number of black edges
on any root-leaf path
(= black height of the tree)
edges connecting leaves are black
Black
Edge
erm
Red
Edge
215
Red-Black
or
216
# of internal nodes
# leaves (= N + 1)
height
black height
Property 1: 2B N + 1 4B
Property 2:
1
--- log ( N + 1 ) B log ( N + 1 )
2
Property 3:
log ( N + 1 ) H 2 log ( N + 1 )
217
R
G
erm
218
g
p
STOP!
n
Pretty easy, huh?
Well... it gets messier...
erm
219
Restructuring
Case 2: Incoming edge of p is red, and
its sibling is black
Uh oh!
g
Much
Better!
p
n
n
erm
More Rotations
Similar to a right rotation, we can do a
left rotation...
g
p
p
g
n
Simple, huh?
erm
221
Double Rotations
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)
n
p
p
n
erm
222
n
g
p
n
erm
223
Bottom-Up Rebalancing
Case 3: Incoming edge of p is red and its
sibling is also red
224
Summary of Insertion
erm
225
An Example
Start by inserting REDSOX into an empty tree
E
D
R
O
S
X
erm
226
A Cool Example
C
E
D
R
S
E
D
C
R
O
S
X
erm
227
An Unbelievable Example
E
U
D
R
S
E
D
R
O
S
X
Oh No!
What should we do?
erm
U
228
E
D
R
S
Double Rotation
U
E
D
R
U
O
S
erm
229
A Beautiful Example
E
B
D
R
U
E
R
D
What
now?
B
erm
230
E
R
Rotation
E
R
C
B
D O
S
erm
231
A Super Example
E
D O
E
R
C
B
D O
Holy Consecutive
Red Edges, Batman!
X
S
erm
232
We couldve
placed it on
either side
E
R
D O
B
Use the
Bat-Promoter!!
S
S
BIFF!
C
B
D O
S
X
S
erm
233
Rotation
D O
X
S
R
The SUN lab
and Red-Bat
trees are safe...
...for now!!!
O S
C
B
erm
S
234