TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL... J. Maluszynski, IDA, Link ¨opings Universitet, 2004. Page 2

advertisement
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 1
J. Maluszynski, IDA, Linköpings Universitet, 2004.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 2
Lecture 7 – Overview
AVL Trees
Why more trees?
AVL = Adel’son-Vel’skii and Landis, 1962
J. Maluszynski, IDA, Linköpings Universitet, 2004.
The idea: Keep a balance information at each node
BST’s: good time complexity if balanced
but O n time for lookup / insert / delete if degenerated
The balance b v of a node v:
How to keep balance on insert / delete?
Balanced search trees:
LeftHeight v :
0 if v has no left child
1 Height LC v
otherwise
How to do lookup, insert, and delete in logarithmic time, worst case?
[Lewis/Denenberg 7.1]
2-3 Trees, (a b)-Trees
[Lewis/Denenberg 7.2]
Splay Trees see Lecture 8
[Lewis/Denenberg 7.3]
bv
AVL Trees
RightHeight v :
0 if v has no right child
otherwise
1 Height RC v
RightHeight v
Le f tHeight v
T is an AVL-tree if b v
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 3
J. Maluszynski, IDA, Linköpings Universitet, 2004.
1 0 1 for every node v
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 4
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Worst AVL trees
AVL-Tree Insertion (1): Simple case
A worst AVL-tree of height h:
as few nodes nh as possible to achieve height h
AVL-tree representation: BST with balance entry in the nodes
0
+1
Construction: Node balance always
1, subtrees always worst AVL trees
Insert
Draw some worst AVL trees and check:
h 0 1 2 3 4 5
nh 1 2 4 7 12 20
h+2
h
h+1
h+1
h+1
nh
1
nh
2
nh
1
Fibonacci numbers Fi
We can prove:
nh
Fi
Fh
1
3
AVL Tree Height Theorem:
Fi 2:
1
h
0, 1, 1, 2, 3, 5, 8, 13, ...
-1
0
(by induction)
1 44 log2 n
Insert
h
h+2
h
h+1
Hence: LookUp in O log n steps
h+1
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 5
Page 6
J. Maluszynski, IDA, Linköpings Universitet, 2004.
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Single Right Rotation
AVL-Tree Insertion (2): Rebalancing
Right Rotation
C
Insertion may destroy the balance.
Insertion with rebalancing:
Insert(50)
1. search downwards the tree,
mark the critical node:
the lowest ancestor
of the insertion node
with balance 1
(before insertion)
A
0
-2
42
1
0
22
1
11
20
51
24
h
T3
0
0
0
h
48
44
0
0
C
0
-1
12
6
A
Critical Node for 50
0
h
h
h
T1
T2
T2
T3
52
49
0
0
h
T1
0
2. insert
50
3. rotate at critical node
if necessary
Subtree at CN keeps same height
0
no rotations are necessary above CN.
Pseudocode: see [Lewis/Denenberg p. 226]
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 7
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Single Right Rotation Example
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 8
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Single Left Rotation
Right Rotation at 42
Left Rotation
C
A
0
+2
12
42
48
12
0
-1
C
0
-2
6
0
h
h
42
1
A
1
T1
0
T3
h
h
22
6
0
1
11
22
0
0
48
T2
T3
0
11
0
A right or left rotation takes constant time.
h
T1
h
T2
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 9
Page 10
J. Maluszynski, IDA, Linköpings Universitet, 2004.
J. Maluszynski, IDA, Linköpings Universitet, 2004.
Which rotations are necessary?
Double Rotation
A
A
1. Right rotation around C
+2
CN = critical node
+2
C
-1
+2
B
C
h
1
T4
0
h-1
h
T1
T1
T2
h-1
h-1
T3
Necessary if the balances of
CN (here A) and CN’s child
on the search path (here C)
have different signs
T3
0
2. Left rotation around A
T4
First rotation
left at CN
right at RC CN
right at CN
left at LC CN
-
Second rotation
left at CN
right at CN
-
B
A
C
-1
0
Constant time
h-1
h
No changes to ancestors
T2
Page 11
h
T3
T4
T1
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
h
Rotations
1
2
0
1
2
0
T2
Moving from CN
right-right
right-left
left
left-left
left-right
right
h
old b CN
+1
+1
+1
-1
-1
-1
B
J. Maluszynski, IDA, Linköpings Universitet, 2004.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
AVL Deletion
2. If the balance changed was previously 0, deletion is completed
3. Otherwise (balance was not 0) rotation may be necessary
and grandparent’s balance may also be affected
Worst case:
rebalancing update/rotation along the whole path towards the root
J. Maluszynski, IDA, Linköpings Universitet, 2004.
AVL Deletion Example
Delete(21)
1. Delete v as from BST:
remove v if v is a leaf (parent’s balance changes)
replace v by its only child if it has one child
(parents balance changes)
replace v by its inorder successor and delete the inorder successor
(successor’s parent balance changes)
Page 12
42
42
12
48
6
21
11
20
44
12
56
49
24
48
6
20
11 rotate
inorder
predecessor
22
42
44
12
56
49
24
48
6
24
11
28
20
28
22
28
44
22
56
49
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 13
J. Maluszynski, IDA, Linköpings Universitet, 2004.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 14
J. Maluszynski, IDA, Linköpings Universitet, 2004.
2-3 Trees
AVL Tree Properties
AVL trees: binary, one key per node, leaves may have different depths
AVL tree with n nodes has height O log n
hence look-up, insertion, deletion in time O log n
2-3 trees:
each leaf has the same depth, contains 1 or 2 keys
an interior node
contains 1 key and has 2 children LC, RC (2-node), or
contains 2 keys and has 3 children LC, MC, RC (3-node)
binary search
for a 2-node v with key a,
each key in the subtree rooted at LC a is
each key in the subtree rooted at RC a is
a, and
a
for a 3-node w with keys a b ,
ternary search
each key in the subtree rooted at LC w is a, and
each key in the subtree rooted at MC w is between a and b, and
each key in the subtree rooted at RC w is b
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 15
J. Maluszynski, IDA, Linköpings Universitet, 2004.
2-3 Tree Insertion
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 16
J. Maluszynski, IDA, Linköpings Universitet, 2004.
2-3 Tree Insertion Example
Insert(26)
Insert k :
18
search for a leaf v as insertion point
add k to v.
If v in overflow (3 keys),
move middle key of v to the parent w of v
11
7
22
12
21
27
99
23 25
22
11
7
21
12
18
18
27
23 25 26
99
25
Repeat while w in overflow (3 keys / 4 children):
split w into siblings w1 and w2,
register w1 and w2 instead of w as children of the parent of w
w parent w
18
If the root was split, set a new root node on top to hold w1, w2.
11
25
22
11
27
22
27
See [Lewis/Denenberg, Fig. 7.8]
In the worst case O log n time
7
12
21
23
26
99
7
12
21
23
26
99
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 17
J. Maluszynski, IDA, Linköpings Universitet, 2004.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
Page 18
J. Maluszynski, IDA, Linköpings Universitet, 2004.
2-3 Tree Deletion Steps
2-3 Tree Deletion
Delete k :
1. Empty root:
Search for node v with key k.
X
If v is a 1-leaf, remove it,
otherwise replace k by the first key in v’s inorder successor (leaf).
This may create an illegal leaf node w without keys.
X
2. Empty non-root node:
while w is illegal, do one of the following:
(a) if w has a sibling w with 2 keys immediately to its left or right:
let s be the key in the parent u of w and w separating w, w ;
move s from u to w, replace s in u by nearest key in w ,
(stealing)
if w, w interior nodes: make nearest child of w a child of w. Done.
(b) w has sibling w with 1 key immediately to its left or right: (merging)
merge w and w to a new 3-node w with keys s and that of w
w parent w , may have become illegal now, iterate...
(c) if w is the root, delete w (its child, if ex., becomes root). Done.
TDDB57 DALG – Lecture 7: Balanced binary search trees: AVL Trees.
O log n time
Underflow of keys may proliferate up to the root
Page 19
J. Maluszynski, IDA, Linköpings Universitet, 2004.
(a b)-Trees
K
Keys only
External leaves added
All data stored as external
leaves
The internal nodes contain
only keys
C
C
P
F
M
Data with keys
Application: storing on external devices with large a, b.
Nodes can be organized internally as binary trees.
B-tree of order b: an (a b)-tree with b
2a
1
S
S
X
2a
S
2a’:Symmetric case
omitted
U
U
S
Z
T
Z
T
2b
2b’: Symmetric case
omitted
S
Z
S
Z
Download