Content: TDDB56 – DALGOPT-D Algorithms and optimization • Motivation: keeping search trees balanced

advertisement
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
Content:
• Motivation: keeping search trees balanced
[Goodrich & Tamassia 9.2]
• AVL Tree
TDDB56 – DALGOPT-D
Algorithms and optimization
Lecture 6
• Multi-way search trees
[Goodrich & Tamassia 9.4]
• Information on B-trees
[Goodrich & Tamassia 9.6]
Search Trees:
AVL-Trees, Multi-way Search Trees, B-Trees
Jan Maluszynski - HT 2005
6.1
Jan Maluszynski - HT 2005
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
AVL Trees
Worst AVL Tree
6.2
• A worst AVL tree is a maximally unbalanced AVL
tree…
…or a tree of a given height with a minimum
number of internal nodes
(Example: insert 44, 17, 78, 32, 50, 88, 62)
Jan Maluszynski - HT 2005
6.3
Jan Maluszynski - HT 2005
6.4
1
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
AVL-Tree Insertion – Rebalance…
AVL-Tree Insertion… (Goodrich/Tamassia)
How to insert an item with key 50?
• Perform a LookUp(50)
• While searching for 50,
keep track of last passed
node with balance ≠ 0
Îcritical node
• If not found, insert at the
leaf where search ended
• Recompute balance on
the way back
• Check critical node!
If balance ∉ [-1..1], rebalance!
Jan Maluszynski - HT 2005
6.5
TDDB56 DALGOPT-D – Lecture 6
AVL-Tree Insertion & Re-balance…
Time to re-balance…
Jan Maluszynski - HT 2005
30
b:
b: -1
?
20
b: 12
10
b:
b: -1
0
60
b: 1
40
b: -1
15
b: 0
20
b: 1
70
b: 0
50
b: 0
10
b: 0
40
b:
b: -1
0
• Label the critical node
and its 2 children on the
path to ”15” as a, b, c,
such that a < b < c, in
an in-order traversal
• Re-structure the nodes
a, b and c so that
b has a and c as
childs!
70
b: 0
50
b: 0
6.6
30
b: ?
20
b: 2
a
k
10
b: -1
b
l
6.7
60
b: 0
1
Jan Maluszynski - HT 2005
TDDB56 DALGOPT-D – Lecture 6
Now try an item with key 15...
• Perform a LookUp(15)
• While searching for 15,
keep track of last passed
node with balance ≠ 0
Îcritical node
• If not found, insert at the
leaf where search ended
• Recompute balance on
the way back
• Check critical node!
If balance ∉ [-1..1], rebalance!
30
b:
b: -1
0
Jan Maluszynski - HT 2005
c
60
b: 1
n
15
b: 0
40
b: -1
70
b: 0
50
b: 0
m
6.8
2
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
Time to re-balance…
Removal of a node...
Same thing, but in reverse!
1. Perform a LookUp and Remove as in
an ordinary binary tree
2. Update the balance on the way back
to the root
3. If too unbalanced: Re-structure! ...but:
• Label the critical node
and its 2 children on the
30
b:
b: -1
?
path to ”15” as a, b, c,
such that a < b < c, in
60
b 15 20 c
an in-order traversal
b: 1
b: 0 b: 2
c
• Re-structure the nodes ab 15 20
a
10b: 0 b: 2
70
c 40
a, b and c so that
10 b
20
n b: -1 b: 0
b: -115
b:
b: -1
0
b: 0
2
b has a and c as
b: 0b 15
n
b: 0
children!
50
k
n
k
b: 0
• Update balance!
l
m
Jan Maluszynski - HT 2005
•
•
•
Label the critical node, the child on the deepest side,
and its child on the deepest side as a, b, c,
such that a < b < c, in an in-order traversal
Re-structure as previous
Go to #2 and continue update and check towards the
root (we may have to re-balance more than once!)
6.9
Jan Maluszynski - HT 2005
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
Tri-node restructuring = rotations....
Double rotations...
Other authors use left and right rotations:
Single left rotation:
a
• left part of the subb
tree (a and j) is lowered
j
• We have ”rotated (up)
k
b over a”...
b
Two rotations are needed when the nodes to
re-balance are placed in a zig-zag pattern...
1. Rotate up b over a
Note: Labeling of a, b
and c same as before!
2. Rotate up b over c
c
a
m
l
Jan Maluszynski - HT 2005
a
a
c
k
b
b
c
b
j
j
c
c
l
a
6.10
m
m
k
6.11
j
k
l
m
j
k
l
m
l
Jan Maluszynski - HT 2005
6.12
3
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
New approach: relax some condition...
2-3 trees (or 2-4 trees, or a,b-trees...)
• AVL-Tree: strict binary tree, accepts a small
unbalance...
• Recall:
Previously:
• A single ”pivot element”
• If larger we search to the right
• If smaller we search to the left
Full binary tree: nonempty; degree is either 0 or 2 for each
node
Perfect binary tree: full, all leaves have the same depth
Now:
• Multipple pivot elements
• No. of childs =
no. of pivot elements + 1
• Can we build and maintain a perfect tree (if we
skip ”binary”) ??
Î we would always know the worst
search time exactly!
Jan Maluszynski - HT 2005
6.13
5
2
8
5 10
2
8
12
Jan Maluszynski - HT 2005
6.14
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
2-3 trees (or 2-4 trees, or a,b-trees...)
Insert in (a,b)-tree where a=2 and b=3
• Each node is either a child,
or has c children where a ≤ c ≤ b
5
Insert(10)
• LookUp works aproximately as before
5 10
Insert(15)
• Insert must check that a node does not
overflow (then we split the node)
5 10 15
• Delete must check that a node does not
become empty (then we transfer or merge
nodes)
Jan Maluszynski - HT 2005
• As long as there is room in
the child we find, add element
to that child...
6.15
10
5
• If full, split and push the selected
pivot element upwards...
...may happen repeatedly
Insert(18)
15
5
10
10
Insert(17)
15 18
Jan Maluszynski - HT 2005
5
15 17 18
10 17
5
15
18
6.16
4
TDDB56 DALGOPT-D – Lecture 6
TDDB56 DALGOPT-D – Lecture 6
Delete in a (2,3)-tree
Delete in a (2,3)-tree
Three cases:
1. No constraints are broken by removal
2. A leaf is removed (becomes empty)
Î transfer some other key to that leaf,
2. A leaf is removed (becomes empty)
Î transfer some other key to that leaf, or
Î merge (fuse) it with a neighbor
…ok if we have a sibling with 2+ elements
30
Delete(25)
?
20
Transfer of
30 and 35
20
Delete(18)
15
18
25
10 17
20
?
5
15
35
10
10
18
30
15
18
30
40
5
15
17
6.17
TDDB56 DALGOPT-D – Lecture 6
Delete in a (2,3)-tree
Properties of a (2,3) tree
3. An internal node becomes empty
Root: replace with in-order pred. or succ.
Î repair inconsistencies with suitable
merge and transfer operations…
30
40
5
?
35
17
?
6.18
∑
17
20
17
40
i =0
?
10
30
• Always a perfect tree
h +1
• A minimal tree of height h will have n = 2 − 1
nodes (it’s a full binary tree with full set of
nodes at all levels)
• A maximal (2,3)-tree will have a branching
factor of 3, thus n = h 3i =(3h +1 − 1) / 2
Internal underflow...
...merge nodes
Replace...
...merge leafs
35
15 17
Jan Maluszynski - HT 2005
TDDB56 DALGOPT-D – Lecture 6
10
5
40
35 40
Jan Maluszynski - HT 2005
Delete(20)
35
35
5
35 40
30
5
15
35 40
30
?
5
5
20
10 17
10 17
No pivot ??
Too many children!
10 17
30
40
5 10
Jan Maluszynski - HT 2005
…2 keys in every node Î k = 3
35
30
h +1
−1
17 35
40
5 10
30
6.19
40
• Thus the height h = log 3 k 
Jan Maluszynski - HT 2005
No of keys
in a tree
6.20
5
TDDB56 DALGOPT-D – Lecture 6
B-Tree
• Used to keep an index over external data
(e.g. content of a disc)
• It’s only an (a,b)-tree where a = b/2
• We may now choose b so that b-1
references to children (other disc blocks)
fit into a single disc block
• By defining a = b/2 we will always fill up
a disc block when two blocks are merged!
Jan Maluszynski - HT 2005
6.21
6
Download