2-3-4 Trees and Red

advertisement
CS 16: Balanced Trees
2-3-4 Trees and RedBlack Trees
erm
204
CS 16: Balanced Trees
2-3-4 Trees Revealed
• Nodes store 1, 2, or 3 keys and have
2, 3, or 4 children, respectively
• All leaves have the same depth
k
nr
beh
a
cd
fg
lm
i
p
sx
1
--- log ( N + 1 ) ≤ height ≤ log ( N + 1 )
2
erm
205
CS 16: Balanced Trees
2-3-4 Tree Nodes
• Introduction of nodes with more than 1 key,
and more than 2 children
a
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
CS 16: Balanced Trees
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
CS 16: Balanced Trees
Insertion into 2-3-4 Trees
• Insert the new key at the lowest internal
node reached in the search
• 2-node becomes 3-node
g
dg
d
• 3-node becomes 4-node
f
d f g
dg
• What about a 4-node?
• We can’t insert another key!
erm
208
CS 16: Balanced Trees
Top Down Insertion
• In our way down the tree, whenever we
reach a 4-node, we break it up into two 2nodes, and move the middle element up
into the parent node
e
e
fn
n
dfg
d
• Now we can perform the
insertion using one of the
previous two cases
• Since we follow this
method from the root down
to the leaf, it is called
top down insertion
erm
209
g
fn
de
g
CS 16: Balanced Trees
Splitting the Tree
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 4node into its parent 4-node.
g
c n t
a
fil
g
pr
n
c
a
erm
Whoa, cowboy
x
t
pr
fil
210
x
CS 16: Balanced Trees
g
n
c
a
t
x
pr
fil
n
c
g
t
Whoa, cowboy
a
erm
fil
pr
211
x
CS 16: Balanced Trees
n
c
g
t
Whoa, cowboy
a
x
pr
fil
n
g
ci
a
f
t
x
pr
l
n
ci
a
erm
fg
t
l
pr
212
x
CS 16: Balanced Trees
Time Complexity of Insertion
in 2-3-4 Trees
Time complexity:
• A search visits O(log N) nodes
• An insertion requires O(log N) node splits
• Each node split takes constant time
• Hence, operations Search and Insert each
take time O(log N)
Notes:
• Instead of doing splits top-down, we can
perform them bottom-up starting at the insertion node, and only when needed. This
is called bottom-up insertion.
• A deletion can be performed by fusing
nodes (inverse of splitting), and takes
O(log N) time
erm
213
CS 16: Balanced Trees
Beyond 2-3-4 Trees
What do we know about 2-3-4 Trees?
• Balanced
Siskel
• O(log N) search time
• Different node structures
Ebert
Roberto
Can we get 2-3-4 tree advantages in a binary
tree format???
Welcome to the world of Red-Black Trees!!!
erm
214
CS 16: Balanced Trees
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
CS 16: Balanced Trees
2-3-4 Tree Evolution
Note how 2-3-4 trees relate to red-black trees
2-3-4
Red-Black
or
Now we see red-black trees are just a way of
representing 2-3-4 trees!
erm
216
CS 16: Balanced Trees
More Red-Black Tree
Properties
N
L
H
B
# 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 )
This implies that searches take time O(logN)!
erm
217
CS 16: Balanced Trees
Insertion into Red-Black Trees
1.Perform a standard search to find the leaf
where the key should be added
2.Replace the leaf with an internal node with
the new key
3.Color the incoming edge of the new node
red
4.Add two new leaves, and color their
incoming edges black
5.If the parent had an incoming red edge, we
now have two consecutive red edges! We
must reorganize tree to remove that
violation. What must be done depends on
the sibling of the parent.
R
R
G
G
erm
218
CS 16: Balanced Trees
Insertion - Plain and Simple
Let:
n be the new node
p be its parent
g be its grandparent
Case 1: Incoming edge of p is black
No violation
g
p
STOP!
n
Pretty easy, huh?
Well... it gets messier...
erm
219
CS 16: Balanced Trees
Restructuring
Case 2: Incoming edge of p is red, and
its sibling is black
Uh oh!
g
Much
Better!
p
p
n
n
We call this a “right rotation”
•
•
•
•
erm
No further work on tree is necessary
Inorder remains unchanged
Tree becomes more balanced
No two consecutive red edges!
220
g
CS 16: Balanced Trees
More Rotations
Similar to a right rotation, we can do a
“left rotation”...
g
p
p
g
n
Simple, huh?
erm
221
n
CS 16: Balanced Trees
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)
g
n
p
p
n
This would be called a
“left-right double rotation”
erm
222
g
CS 16: Balanced Trees
Last of the Rotations
And this would be called a
“right-left double rotation”
g
n
g
p
n
erm
223
p
CS 16: Balanced Trees
Bottom-Up Rebalancing
Case 3: Incoming edge of p is red and its
sibling is also red
g
g
p
p
n
n
• We call this a “promotion”
• Note how the black depth remains unchanged for all of the descendants of g
• This process will continue upward beyond
g if necessary: rename g as n and repeat.
erm
224
CS 16: Balanced Trees
Summary of Insertion
• If two red edges are present, we do either
• a restructuring (with a simple or double
rotation) and stop, or
• a promotion and continue
• A restructuring takes constant time and is
performed at most once. It reorganizes an
off-balanced section of the tree.
• Promotions may continue up the tree and
are executed O(log N) times.
• The time complexity of an insertion is
O(logN).
erm
225
CS 16: Balanced Trees
An Example
Start by inserting “REDSOX” into an empty tree
E
D
R
O
S
X
Now, let’s insert “C U B S”...
erm
226
CS 16: Balanced Trees
A Cool Example
C
E
D
R
S
O
X
E
D
C
R
O
S
X
erm
227
CS 16: Balanced Trees
An Unbelievable Example
E
U
D
R
S
O
C
X
E
D
C
R
O
S
X
Oh No!
What should we do?
erm
U
228
CS 16: Balanced Trees
E
D
R
S
O
C
X
Double Rotation
U
E
D
C
R
U
O
S
erm
229
X
CS 16: Balanced Trees
A Beautiful Example
E
B
D
R
U
O
C
S
X
E
R
D
What
now?
C
S
B
erm
U
O
230
X
CS 16: Balanced Trees
E
R
D
Rotation
S
B
E
R
C
B
U
D O
S
erm
U
O
C
231
X
X
CS 16: Balanced Trees
A Super Example
E
S
R
C
U
D O
B
S
X
E
R
C
B
U
D O
Holy Consecutive
Red Edges, Batman!
S
X
S
erm
232
We could’ve
placed it on
either side
CS 16: Balanced Trees
E
R
C
U
D O
B
Use the
Bat-Promoter!!
S
S
E
BIFF!
R
C
B
X
U
D O
S
X
S
erm
233
CS 16: Balanced Trees
E
Rotation
R
C
U
D O
B
S
X
S
R
The SUN lab
and Red-Bat
trees are safe...
...for now!!!
O S
C
B
erm
U
E
D
X
S
234
Download