Title: Chapter 25 Supplement AVL Trees, Splay Trees, 24 Trees, RedBlack Trees
1Chapter 25 SupplementAVL Trees, Splay Trees,
2-4 Trees, Red-Black Trees
2Topics
- AVL Trees
- Properties and maintenance
- Splay Trees
- Properties and maintenance
- 2-4 Trees
- Properties and maintenance
- Red-black Trees
- Properties and equivalence to 2-4 Trees
3Performance of a Binary Search Tree (1)
- Consider a dictionary with n items implemented by
means of a binary search tree of height h - the space used is O(n)
- It takes O(1) time at each node visit
- methods find, insert and remove take O(h) time
4Performance of a Binary Search Tree (2)
worse case no better than a list or array
implementation
- The height h is n in the worst case and log n in
the best case - Let h be the height of a dictionary storing n
keys - Since there are 2i keys at depth i 0, , h - 1
and at least one key at depth h, we have n ? 1
2 4 2h-1 1 2h-11 (geometric sum)
2h - Taking the log of both sides of n ? 2h gt h ?
log n - A binary search tree is an efficient
implementation of a dictionary only if the height
is small
best case
5AVL Trees
6Definition of an AVL Tree
- Goal Achieve logarithmic time for all
fundamental dictionary operations - Dictionary is a map that allows for multiple
entries to have the same key (like an English
dictionary) - Add a rule to the binary search tree definition
called the height-balance property - Characterizes the structure of a binary tree in
terms of the heights of its internal nodes - The height of a node v is the length of the
longest path from v to an external node
7Height-Balance Property
- For every internal node v of T, the heights of
the children of v can differ by at most 1
8AVL Tree Definition
- Any binary search tree that satisfies the
height-balance property is said to be an AVL tree
(keeps the height small)
An example of an AVL tree where the heights are
shown next to the nodes
9Tree Terminology
- Height of a tree maximum depth of any node
- Internal node node with at least one child
2
6
1
1
8
3
10Height of an AVL Tree
- Fact The height of an AVL tree storing n keys is
O(log n). - Proof Let us bound n(h) the minimum number of
internal nodes of an AVL tree of height h - Base case n(1) 1 because an AVL tree of height
1 must have at least 1 internal node - Base case n(2) 2 because an AVL tree of height
2 must have at least 2 internal nodes - For n gt 2, an AVL tree of height h contains the
root node, one AVL subtree of height n-1 and
another of height n-2. - That is, n(h) 1 n(h-1) n(h-2) (increasing
function) - Knowing n(h-1) gt n(h-2), we get n(h) gt 2n(h-2)
- So n(h) gt 2n(h-2), n(h) gt 4n(h-4), n(h) gt
8n(n-6), (by induction), n(h) gt 2in(h-2i) - Solving the base case we get n(h) gt 2 h/2-1
- Taking logarithms h lt 2log n(h) 2
- Thus the height of an AVL tree is O(log n)
11Insertion in an AVL Tree
- Insertion starts like it does as in a binary
search tree - Height-balance rule may be violated
- The tree may be have to be restructured
12Balanced Nodes
- An internal node v is balanced if the absolute
value of the difference between the heights of
the children of v is at most 1, otherwise it is
unbalanced - Every internal node of an AVL tree is balanced
13Starting the Trinode Restructuring
- Let z be the first node encountered going up from
w toward the root that is unbalanced - Let y be the child of z with the greater height
- y is an ancestor of w
- Let x be the child of y with the greater height
- x is an ancestor of w
- x is a grandchild of z and could be w
14Insertion in an AVL Tree
- Insert 54 (before restructured)
cz
ay
bx
w
before insertion
after insertion
Nodes 44 and 78 are unbalanced
15Restructuring an AVL Tree
- Input A node, x, of a search binary tree that
has both a parent, y, and a grandparent, z - Output Tree T after a trinode restructuring
(single or double rotation) - Let (a, b, c) be a left to right (inorder)
listing of nodes x, y, and z and let (T0, T1, T2,
T3) be the left to right listing (inorder) of the
four subtrees of x, y, and z not rooted at x, y,
or z - Replace the subtree rooted at z with a new
subtree rooted at b - Let a be the left child of b and let T0 and T1 be
the left and right subtrees of a, respectfully - Let c be the right child of b and let T2 and T3
be the left and right subtrees of c, respectfully
16Restructuring Single Rotations
a z
b y
single rotation
b y
c x
a z
c x
T
T
0
3
T
T
T
T
T
1
3
0
1
2
T
2
17Restructuring Double Rotations
18Insertion Example
44
z c
17
y a
78
88
32
50
x
x b
48
62
T
54
3
T
T
2
0
T
44
1
b
c
17
unbalanced...
62
a
32
78
50
...balanced
88
54
48
T
2
T
T
3
0
19Another Insertion Example
- Insert 49 (before restructured)
44
cz
17
78
by
T3
32
50
88
48
62
ax
49
T2
T0
w
before insertion
T1
after insertion
Nodes 44 and 78 are unbalanced
20Insertion in an AVL Tree
- Insert 49 (after restructured)
44
by
17
50
ax
cz
32
78
48
88
62
49
w
after insertion
21Removal in an AVL Tree
- Removal of a node begins as in a binary search
tree - Elevates one of its children into its place
- The operation may cause the height-balance
property to be violated
22Example of Removing a node from an AVL Tree
44
17
62
78
50
W
88
48
54
before deletion of 32
after deletion
Before restructuring
23Rebalancing after Removal
- Let z be the first unbalanced node encountered
while traveling up the tree from w (node removed) - Let y be the child of z with the greater height
- Let x be the child of y with the greater height
or - If both children have the same height then let x
be the child of y on the same side as y (e.g. if
y is a left child then x is a left child) - We perform restructure(x) to restore balance at z
- As this restructuring may upset the balance of
another node higher in the tree - One must continue checking for balance until the
root of T is reached
24Rebalancing after Removal
62
44
az
44
78
17
62
by
17
50
88
78
50
cx
T0
48
54
88
48
54
T0
T3
T1
T2
T1
T3
T2
25Running Times for AVL Trees
- Find is O(log n)
- height of tree is O(log n), no restructures
needed - insert is O(log n)
- initial find is O(log n)
- Restructuring up the tree, maintaining heights is
O(log n) - remove is O(log n)
- initial find is O(log n)
- Restructuring up the tree, maintaining heights is
O(log n)
26Splay Trees
27Splay Trees
- Does not use any explicit rules to enforce
balance - Related to a certain move-to-root operation
(called splaying) after every access in order to
keep the search tree balanced - Performed at the bottom most node reached during
an insert, deletion, or a search - Guarantees logarithm running times
28Splaying
- One splays x by moving x to the root of a tree
through a series of restructurings - Operation depends upon the relative positions of
x, it parent y, and (if it exists) xs
grandparent z
29zig-zig (with Right Children)
- The node x and its parent y are both left
children or both right children - One replaces z with x, making y a child of x and
z a child of y while maintaining the in-order
relationship
Z10
T1
Y20
X30
X30
T2
T4
T3
Y20
T4
Z10
T3
T1
T2
30zig-zig (with Left Children)
Z30
Y20
T4
X10
X10
T3
Y20
T1
T2
T1
Z30
T2
T4
T3
31zig-zag
- Either x and y is a left child and the other is a
right child - One replaces z with x, making x have y and z as
its children while maintaining the in-order
relationship
X20
Z10
Z10
Y30
Y30
T1
T2
T3
T4
T1
T4
X20
T2
T3
32Another zig-zag
X20
Z30
T4
y10
z30
Y10
T2
T3
T4
T1
T1
X20
T2
T3
33zig
- X does not have a grandparent
- One rotates x over y, making xs children be the
node y and one of xs former children w while
maintaining the in-order relationship
y10
T1
x20
w30
X20
T2
T3
T4
2
Y10
W30
T2
T3
T4
T1
34Another zig
Y30
T4
X20
X20
W10
Y30
W10
T3
T2
T3
T4
T1
T1
T2
35What Operation to Perform
- One performs a zig-zig or a zig-zag when x has a
grandparent - One performs a zig when x does not have a parent
- A splaying step consists of repeating the above
restructurings at x until x becomes the root of
the tree
36Splaying
- x is a left-left grandchild means x is a left
child of its parent, which is itself a left child
of its parent - p is xs parent g is ps parent
start with node x
is x a left-left grandchild?
is x the root?
zig-zig
yes
stop
right-rotate about g, right-rotate about p
yes
no
is x a right-right grandchild?
is x a child of the root?
zig-zig
no
left-rotate about g, left-rotate about p
yes
is x a right-left grandchild?
yes
zig-zag
is x the left child of the root?
left-rotate about p, right-rotate about g
no
yes
is x a left-right grandchild?
zig-zag
zig
zig
yes
right-rotate about the root
left-rotate about the root
right-rotate about p, left-rotate about g
yes
37Splaying Example (1)
8
10
3
4
11
6
12
7
5
z
y
15
13
x
17
Splaying node 14 Beginning of a zig-zag
14
T4
T1
T3
T2
38Splaying Example (2)
8
10
3
4
11
6
12
7
5
x
14
y
z
13
zig-zag complete
15
17
T2
T1
T3
T4
39Splaying Example (3)
8
10
3
4
z
11
6
y
12
7
5
x
14
13
Beginning of a zig-zig
15
17
40Splaying Example (4)
8
10
3
x
14
4
15
y
12
6
7
5
13
17
Z
11
zig-zig complete
41Splaying Example (5)
Z
8
y
10
3
x
14
4
15
12
6
7
5
13
17
11
T1
Beginning of a zig-zig
42Splaying Example (6)
x
y
14
10
Z
8
3
4
6
7
5
zig-zig complete
T1
43When to Splay
- The following rules dictate when splaying is
performed - When searching for a key
- If the key is found, one splays at the node where
the key is found - If the key is not found, one splays the parent of
the external node at which the search terminates
unsuccessfully - (The splaying done on the previous slides (at 14)
would be performed after searching for 14 or
unsuccessfully search for 14.5
44When to Splay Insertion (1)
When inserting a key, one splays the newly
created node
1
2
2
1
initial tree
Inserting 2
after splaying
45When to Splay Insertion (2)
3
2
2
2
1
3
1
1
Inserting 3
Updated tree
after splaying
46When to Splay Deletion (1)
When deleting a key, one splays the parent of the
node that is moved to the root (recall the
removal for binary search trees)
To delete node 8, move the right most node in the
left subtree to the root, and splay its parent
(in this case, splay 6)
8
8
10
3
10
3
4
4
11
11
6
6
7
5
7
5
removing 8
Initial tree
47When to Splay Deletion (2)
7
7
10
3
10
3
4
4
11
11
6
6
5
5
7 move to the root
beginning of splaying
48When to Splay Deletion (3)
7
7
10
6
10
6
4
4
11
11
5
5
3
3
splaying complete
Begin another splaying
49When to Splay Deletion (4)
6
7
4
10
3
5
11
T4
splaying complete
50(2,4) Trees
9
10 14
2 5 7
51Multi-Way Search Tree (1)
- Let v be a node of an ordered tree
- v is called a d-node if v has d children
- Definition of a Multi-Way Search Tree
- Each internal node has at least two children
- Each node is a d-node where d is ? 2
- Each internal d-node v with children v1 v2 vd
stores an ordered set of d key-value entries (k1
,x1),,(ki, xi), where k1 ? ? kd-1
52Multi-Way Search Tree (2)
- External nodes do not store any entries and serve
as placeholders - An n-entry multi-way search tree has n1 external
nodes - A binary search tree is a multi-way search tree
where each internal node and two children
11 24
2 6 8
15
27 32
30
Sample multi-way search tree
53Multi-Way Searching
- Similar to search in a binary search tree
- At each internal node with children v1 v2 vd
and keys k1 k2 kd-1 - k ki (i 1, , d-1) the search terminates
successfully - k lt k1 we continue the search in child v1
- ki-1 lt k lt ki (i 2, , d-1) we continue the
search in child vi - k gt kd we continue the search in child vd
- Reaching an external node terminates the search
unsuccessfully - Example search for 30
11 24
2 6 8
15
27 32
30
54(2,4) Trees
- A (2,4) tree (also called 2-4 tree or 2-3-4 tree)
is a multi-way search with the following
properties - Node-Size Property every internal node has at
most four children - Depth Property all the external nodes have the
same depth - Depending on the number of children, an internal
node of a (2,4) tree is called a 2-node, 3-node
or 4-node
10 15 24
2 8
12
27 32
18
External nodes are empty
55Height of a (2,4) Tree
- Theorem A (2,4) tree storing n items has height
O(log n) - Proof
- Let h be the height of a (2,4) tree with n items
- Since there are at least 2i items at depth i 0,
, h - 1 and no items at depth h, we have n ?
1 2 4 2h-1 2h - 1 - Thus, h ? log (n 1)
- Searching in a (2,4) tree with n items takes
O(log n) time
items
depth
1
0
2
1
2h-1
h-1
0
h
56Insertion
- To insert a new item (k, x) one first performs a
search for key k - If the tree has no entry with key k, the search
terminates at node z - Let v be the parent of z, one inserts the new
entry into node v and adds a new child w (an
external node) to v on the left of z - Entry (k,x,w) is added
- The depth property is always preserved
- The insert may cause an overflow
- Node v may become a 5-node
- This must be resolved
- The overflow may propagate to the parent node u
57Insertion
- Example inserting key 30 causes an overflow
10 15 24
v
27 32 35
2 8
12
18
10 15 24
v
2 8
12
27 30 32 35
18
58Overflow and Split
- We handle an overflow at a 5-node v with a split
operation - Replace v with two nodes v and v
- v is a 3-node with children v1, v2, an v3
storing keys k1 and k2 - v is a 2-node with children v4 an v5 storing
key k4 - If v was a root of a tree, create a new root u
else let u be the parent of v - Insert key k3 into u and make v and v children
of u - If v was a child i of u, then v and v become
children i and i1 respectfully
u
u
15 24 32
15 24
v
v'
v"
12
27 30 32 35
18
12
27 30
18
35
v1
v2
v3
v4
v5
v1
v2
v3
v4
v5
59Insertion
4 6
12
12
4 6
Homework assignment insert 10,8,13,16,2,1
5 12
12
3 4
15
60Analysis of Insertion
- An insertion in a (2,4) tree takes O(log n) time
61Multi-Way Inorder Traversal
- We can extend the notion of inorder traversal
from binary trees to multi-way search trees - Namely, we visit item (ki, oi) of node v between
the recursive traversals of the subtrees of v
rooted at children vi and vi 1 - An inorder traversal of a multi-way search tree
visits the keys in increasing order
11 24
8
12
2 6 8
15
27 32
2
4
6
14
18
10
30
1
3
5
7
9
11
13
19
16
15
17
62Removal (1)
- Removing an entry can always be reduced to the
case where the entry to be removed is stored at a
node v whose children are external nodes - One replaces the entry with its inorder successor
(or, equivalently, with its inorder predecessor)
and delete the latter entry
63Removal of a Non-leaf Node
- To delete key 24, we replace it with 27 (inorder
successor)
10 15 27
32 35
2 8
12
18
64Removal (2)
- After the entry stored at node v to be removed
has only external children, one simply remove the
entry from v - Removing entries (and a child) always preserves
the depth property - The size property may become violated
- If v was a 2-node, then it becomes a 1-node with
no entries (underflow) - Deleting an entry from a node v may cause an
underflow, where node v becomes a 1-node with one
child and no keys - To handle an underflow at node v with parent u,
we consider two cases
65Underflow and Transfer
- Case 1 an adjacent sibling w of v is a 3-node or
a 4-node - Transfer operation
- 1. move a child of w to v
- 2. move a key of w to parent u of w and v
- 3. move a key of u to v
- After a transfer, no underflow occurs
u
u
4 9
4 8
v
w
v
w
6 8
2
12
6
2
9
66Underflow and Fusion
- Case 2 if v has only one sibling or both
immediate siblings of v are 2-nodes - Fusion operation
- 1. one merges v with a sibling creating a new
node v - 2. move a key from the parent u of v to v
- After a fusion, the underflow may propagate to
the parent u
u
u
9 14
9
v
v'
w
2 5 7
10
12
10 14
2 5 7
67Analysis of Removal
- Let T be a (2,4) tree with n items
- Tree T has O(log n) height
- In a removal operation
- One visits O(log n) nodes to locate the node from
which to delete the entry - One handles an underflow with a series of O(log
n) fusions, followed by at most one transfer - Each fusion and transfer takes O(1) time
- Thus, removing an item from a (2,4) tree takes
O(log n) time
68Red-Black Trees
6
v
8
3
z
4
69Comparison of Trees
- AVL trees may require many restructures after
removals - 2-4 trees may require many fusing or split
operations to be performed after insertion or
removals - Red-Black trees do not have these drawbacks
- They require only o(1) structural changes after
an update
70Red-Black Trees
- A red-black tree is a binary search tree with
nodes colored red and black such that - Root Property the root is black
- External Property every external node is black
- Internal Property the children of a red node are
black - Depth Property all the external nodes have the
same black depth - Defined as the number of black ancestors minus
one - (node is an ancestor of itself)
9
15
4
21
6
2
12
7
Depth 2
71From (2,4) to Red-Black Trees
- Given a (2,4) tree, one can construct a
corresponding red-black tree by coloring each
node black and performing the following
transformation for each internal node v - If v is a 2-node, then keep the (black) children
of v as is - If v is a 3-node, then create a new red node w,
give vs first two (black) children to w and make
w and vs third child be the two children of v - If v is a 4-node, then create two new red nodes w
and z, give vs first two (black) children to w,
give vs last two (black) children to z, and make
w and z be the two children of v
72From (2,4) to Red-Black Trees
4
6
5
3
OR
2
7
3
5
72
73From Red-Black Trees to (2,4)
- Given a red-black tree, one can construct a
corresponding (2,4) tree by merging every red
node into its parent storing the entry from v at
its parent
73
74From Red-Black Trees to (2,4)
4
6
5
3
OR
2
7
3
5
74
75Height of a Red-Black Tree
- A red-black tree storing n entries has height
O(log n)
76Insertion (1)
- To perform insert (k) operation
- One searches for key k in the tree until an
external node is reached - The external node is replaced with an internal
node z storing (k) having two external node
children - If z is the root, one colors z black, else one
colors z red - One colors the children of z black
77Insertion (2)
- One needs to preserve the root, external, and
depth properties - If the parent v of z is black, the internal
property is preserved and process is complete - If the parent v of z is red, one has a double red
- A violation of the internal property which
requires a reorganization of the tree
78Insertion Causing a Double Red
- Example where the insertion of 4 causes a double
red
6
6
v
v
8
8
3
3
z
z
4
79Remedying a Double Red (Case 1)
- The sibling w of v is black (perform a trinode
restructuring) - Take node z, its parent v, and grandparent u and
label them a, b, c per the in-order search order
ua
ua
uc
uc
w
10
30
30
10
w
va
w
vb
w
vc
va
x
x
10
10
20
30
x
x
zc
zb
20
20
zb
20
30
zb
b
After the restructure, one colors node b black
and nodes a and c red
20
Adding node 20
c
10
a
30
80Remedying a Double Red (Case 2)
- The sibling of v is red
- The double red corresponds to a 2-4 tree overflow
- One performs a recoloring operation (per next
slide) - Equivalent of a 2-4 tree split
u
30
w
v
40
20
10 20 30 40
10
z
Adding 10
81Recoloring
- A recoloring remedies a child-parent double red
when the parent red node has a red sibling - The parent v and its sibling w become black and
the grandparent u becomes red, unless it is the
root - It is equivalent to performing a split on a
5-node - The double red violation may propagate to the
grandparent u (if so another recoloring must be
performed)
u
u
30
w
v
30
v
w
40
20
40
20
10
z
10
z
30
10 20 30 40
40
10 20
82Adding nodes to a Red-Black Tree
Adding 4,7,12,15,3,5,14
restructure
4
7
Add 12
7
12
4
Add 7
Add 15
Recoloring (root remains black
7
7
12
4
12
4
15
15
83Adding nodes to a Red-Black Tree (2)
Add 5
Add 3
7
7
12
4
12
4
3
3
5
15
15
Add 14
7
7
restructure
14
4
12
4
3
15
12
5
3
5
15
14
84Analysis of Insertion
- An insertion in a red-black tree takes O(log n)
time
85Removal (1)
- To perform operation remove, one commences with
the remove algorithm for binary search trees
86Removal (2)
- To remove the entry with key k from node v with
external child w - Let r be the sibling of w and x be the parent of
v - One removes nodes v and w
- One makes r a child of x
- If v was red (r must be black) or r was red (v
must be black), one colors r black and the
process is complete - Removal (A) and Removal (B) slides
- If r is black and v is black, one give r a
fictitious double black color to preserve the
depth property - Removal (C) slide
- Double black problem exists (violation)
corresponds to an underflow in the corresponding
(2,4) tree
87Removal (A)
- Where v is red (r must be black)
x
x
6
6
r
v
8
7
3
3
r
w
7
4
4
Complete
87
88Removal (B)
- Where r is red (v must be black)
r is colored black
x
x
6
6
v
8
7
3
3
r
w
7
4
4
Complete
88
89Removal (C)
- Where r is black (v is black)
- Removal of node 40 causes a depth problem
(signified by a double black)
30
v
30
r
20
40
20
w
10
r
10
double black signifying a depth problem
90The Sibling y of r is Black and has a Red Child
(Case 1)
- One performs trinode restructuring
- After restructuring
- a and c are colored black
- b is given the former color of x
- r is colored black
- At most one restructuring is needed
91Remedying a Double Black (Case 1)
xc
30
r
yb
20
za
10
b
20
xc
c
10
a
30
30
r
r
ya
10
20
zb
could be red or black
30
20
is the same color as
30
92The Sibling y of r is Black and Both Children are
Black (Case 2)
- One performs a recoloring, equivalent to a 2-4
fusion - One colors r black
- One colors y red
- If x is red, one colors it black otherwise, one
colors x double black - Limited to log (n1) recolorings
93Remedying a Double Black (Case 2)
x
10
10
x
30
30
r
r
y
y
20
20
Recoloring
Complete
x
x
30
30
r
r
y
y
20
20
94The Sibling y of r is Red (Case 3)
- One performs an adjustment operation
- If y is a right child of x, let z be the right
child of y otherwise let z be the left child of y - Execute the trinode restructuring that makes y
the parent of x - Color y black and x red
- After the adjustment, case 1 or case 2 applies
- At most one adjustment is needed
95Remedying a Double Black (Case 3)
xc
30
r
yb
20
za
10
b
20
c
10
a
30
r
96Remedying a Double Black
- The algorithm for remedying a double black node w
with sibling y considers three cases - Case 1 y is black and has a red child
- One performs a restructuring, equivalent to a
transfer, and we are done - Case 2 y is black and its children are both
black - One performs a recoloring, equivalent to a
fusion, which may propagate up the double black
violation - Case 3 y is red
- One performs an adjustment, equivalent to
choosing a different representation of a 3-node,
after which either Case 1 or Case 2 applies - Deletion in a red-black tree takes O(log n) time
97Removing nodes from a Red-Black Tree (1)
14
16
7
4
18
15
12
5
3
17
remove 3
14
16
7
4
18
15
12
5
17
97
98Removing nodes from a Red-Black Tree (2)
14
16
7
4
18
15
12
5
17
remove 12 causes a double black
14
16
7
4
18
15
5
17
98
99Removing nodes from a Red-Black Tree (3)
14
16
7
4
18
15
5
17
restructure
14
16
5
4
18
15
7
17
99
100Removing nodes from a Red-Black Tree (4)
14
16
5
remove 17
4
18
15
7
17
14
16
5
4
18
15
7
100
101Removing nodes from a Red-Black Tree (4)
14
16
5
remove 18 causes a double black
4
18
15
7
14
16
5
4
15
7
101
102Removing nodes from a Red-Black Tree (5)
14
16
5
4
15
7
recoloring
14
16
5
4
15
7
102
103Red-Black Tree Reorganization
104Red-Black Tree Reorganization