13-May-04 22 - 1
©2004 PM - IKI10100
AA- AA -Trees Trees
AA-tree holds the property of Red-Black Tree with one extra condition:
Left children may not be red
If an internal node has only one child, the child must be a red right one
In deletion, we can always replace an internal node with the smallest node in its right subtree. That smallest node will either be a leaf or have a read child and can be easily bypassed and remove.
13-May-04 22 - 2
©2004 PM - IKI10100
Level of a Node:
Level of a Node:
z Level 1 if the node is a leaf
z The level of its parent, if the node is red
z One less than the level of its parent, if the node is black
z The level represents the number of left links on the path to null
AA AA -Tree - Tree
5
15 85 20
30
40 65
70
80 90
10 55
50 60
35
5
15
30 70
10
50 60
3 2 1 Level
85
Deletion?
Deletion?
AA-Trees
1. Remove with immediate successor, which is guaranteed always in level 1
2. Let T be the root of a subtree in which T’s
children becomes two less than T’s level,
then T’s level needs to be lowered. If T has
a horizontal link, then its right child’s level
must also be lowered.
13-May-04 22 - 5
©2004 PM - IKI10100
Worst Case of Deletion Worst Case of Deletion
AA-Trees
If 1 is deleted, six nodes on the same level.
1
2 5
3 4 6 7
13-May-04 22 - 6
©2004 PM - IKI10100
Worst Case of Deletion Worst Case of Deletion
AA-Trees
2 5
3 4 6 7
2 5
3
4 6 7
Skew (5) + Split (3)
Worst Case of Deletion Worst Case of Deletion
AA-Trees
2
3 5
4 6 7
Skew (5) + Split (5)
2 5
3
4 6 7
75
Kondisi Awal Kondisi Awal
42 Delete 32
65 45
12 35 43 49 63
87
68 95 97
96
35 menggantikan posisi 32 32
13-May-04 22 - 9
©2004 PM - IKI10100
75
35 menggantikan 35 menggantikan 32 32
42 Delete 32
65 45
35
12 43 49 63
87
68 95 97
96 2
1 0
Level 35 berbeda lebih dari 1 dengan anak kanannya.
35 harus turun 1 level
13-May-04 22 - 10
©2004 PM - IKI10100
75
Violation!
Violation!
42 Delete 32
65 45
12 35 43 49 63
87
68 95 97
96 1 1
3
Skew 12 dengan 35!
75
42 unbalance!
42 unbalance!
42 Delete 32
65
45
12 35 43 49 63
87
68 95 97
96 1 1
3
42 harus turun 1 level
75
Violation: 45
Violation: 45 thd thd 65 65
42 Delete 32
45 65
12 35 43 49 63
87
68 95 97
96 1 1
2
Skew 45 dgn 65!
13-May-04 22 - 13
©2004 PM - IKI10100
75
Violation: double right links Violation: double right links
42 Delete 32
45 65
12 35 43 49 63
87
68 95 97
96 1 1
2
Split 45!
13-May-04 22 - 14
©2004 PM - IKI10100
75
Violation: double right links Violation: double right links
42 Delete 32
65 45
12 35 43 49 63
87
68 95 97
96
Split 87
75
Solved Solved
42 Delete 32
65 45
12 35 43 49 63
87
68 95 97
96
75
Delete 42!
Delete 42!
42 65
45
12 35 43 49 63
87
68 95 97
96
13-May-04 22 - 17
©2004 PM - IKI10100
2- 2 -3 3- -4Tree 4Tree
k b e h
a c d f g i
n r
l m p s x
13-May-04 22 - 18
©2004 PM - IKI10100
B B -tree of order M (M - tree of order M (M -ary - ary) )
1. The data items are stored at leaves
2. The non-leaf nodes store up to M-1 keys to guide the searching; key i represents the smallest key in subtree i+1
3. The root is either a leaf or has between two and M children
4. All non-leaf nodes (except the root) have between
M / 2and M children
5. All leaves are the same depth and have between
L / 2and L children, for some L
5- 5 -ary ary tree of 31 nodes tree of 31 nodes B B -tree of order 5 - tree of order 5
2 4 6
8 10 12 14 16
18 20 22 24
26 28 30 31 32
35 36 37 37 39
41 42 44 46
48 49 50
51 52 53
54 56 58 59
66 68 69 70
72 73 74 76
78 79 81
83 84 85
87 88 90
92 93 95
97 98 99
41 66 87
8 18 26 35 48 51 54 72 78 83 92 97
M = 5; L = 5
13-May-04 22 - 21
©2004 PM - IKI10100
M and L calculation M and L calculation
Data of citizens in Florida is kept in 10,000,000 items Each key is 32 bytes (representing name)
One record is 256 bytes
Suppose one block holds 8,192 bytes.
M – 1 keys are needed for branches for a total 32M – 32 bytes.
Assume each branch is 4 bytes, then total memory for a nonleaf is 32M – 32 + 4M = 36M – 32 bytes.
The largest value of M which is total size of non-leaf no more than 8,192 is M = 228.
B-Trees
13-May-04 22 - 22
©2004 PM - IKI10100
M and L calculation M and L calculation
Since each data record is 256 bytes, then we would be able to fit 32 records in a block. Hence, L = 32.
B-Trees
M L M-1 keys
Insert 57 Insert 57
2 4 6
8 10 12 14 16
18 20 22 24
26 28 30 31 32
35 36 37 37 39
41 42 44 46
48 49 50
51 52 53
54 56 58 59
66 68 69 70
72 73 74 76
78 79 81
83 84 85
87 88 90
92 93 95
97 98 99
41 66 87
8 18 26 35 48 51 54 72 78 83 92 97
B-tree of order 5
After insertion 57 After insertion 57
2 4 6
8 10 12 14 16
18 20 22 24
26 28 30 31 32
35 36 37 38 39
41 42 44 46
48 49 50
51 52 53
54 56 57 58 59
66 68 69 70
72 73 74 76
78 79 81
83 84 85
87 88 90
92 93 95
97 98 99
41 66 87
8 18 26 35 48 51 54 72 78 83 92 97
B-tree of order 5
Insert 55 !
13-May-04 22 - 25
©2004 PM - IKI10100
After insertion 55 After insertion 55
2 4 6
8 10 12 14 16
18 20 22 24
26 28 30 31 32
35 36 37 38 39
41 42 44 46
48 49 50
51 52 53
54 55 56
66 68 69 70
72 73 74 76
78 79 81
83 84 85
87 88 90
92 93 95
97 98 99
41 66 87
8 18 26 35 48 51 54 72 78 83 92 97
B-tree of order 5
57 58 59
57
Insert 40 !
13-May-04 22 - 26
©2004 PM - IKI10100
After insertion 40 After insertion 40
B-tree of order 5
2 4 6
8 10 12 14 16
18 20 22 24
41 42 44 46
48 49 50
51 52 53
54 55 56
66 68 69 70
72 73 74 76
78 79 81
83 84 85
87 88 90
92 93 95
97 98 99
26 41 66 87
8 18 48 51 54 72 78 83 92 97
57 58 59
57
26 28 30 31 32
38 39 40
35 38
35 36 37