Upload
taqi-shah
View
217
Download
0
Embed Size (px)
Citation preview
8/3/2019 Lec 28 B Tree Operations
1/23
B-Tree Operations
8/3/2019 Lec 28 B Tree Operations
2/23
Multi way search trees(of order
m)A generalization of Binary Search Trees.
Each node has at mostm
children.
Ifk m is the number of children, then the node
has exactly k-1 keys.
The tree is ordered.
8/3/2019 Lec 28 B Tree Operations
3/23
B-tree of order 5 This means that (other then the root node)
all internal nodes have at least ceil(5 / 2) =
ceil(2.5) = 3 children and hence at least 2keys.
Of course, the maximum number ofchildren that a node can have is 5 (so that4 is the maximum number of keys).
Each leaf node must contain at least 2keys.
8/3/2019 Lec 28 B Tree Operations
4/23
Insertion
In B-Tree (Order 5)
We want to insert C N G A H E K Q M F W L T Z D P R X Y S
Order 5 means that a node can have a
maximum of 5 children and 4 keys.
All nodes other than the root must have a
minimum of 2 keys.
Height of B-Tree grows with splitting only.
8/3/2019 Lec 28 B Tree Operations
5/23
Contd Insert C
Insert N
Insert G
Insert A
8/3/2019 Lec 28 B Tree Operations
6/23
8/3/2019 Lec 28 B Tree Operations
7/23
Inserting E, K, and Q We will proceeds without requiring any
splits
8/3/2019 Lec 28 B Tree Operations
8/23
Inserting
Mreq
uires a sp
lit
Note that M happens to be the median key
and so is moved up into the parent node.
8/3/2019 Lec 28 B Tree Operations
9/23
Insert F, W, L, and T Need a split?
These are added without needing any
split.
8/3/2019 Lec 28 B Tree Operations
10/23
Insert Z Rightmost leaf must be split. The median
item T is moved up into the parent node.
8/3/2019 Lec 28 B Tree Operations
11/23
Insert D Causes the leftmost leaf to be split.
8/3/2019 Lec 28 B Tree Operations
12/23
Finally
Insert S
4th node splits.
The parent node is full, so it splits
8/3/2019 Lec 28 B Tree Operations
13/23
Delete Operation
8/3/2019 Lec 28 B Tree Operations
14/23
Delete H
H is in a leaf and the leaf has more than
the minimum number of keys, this is easy.
8/3/2019 Lec 28 B Tree Operations
15/23
8/3/2019 Lec 28 B Tree Operations
16/23
Delete T
8/3/2019 Lec 28 B Tree Operations
17/23
Delete R
Although R is in a leaf, this leaf does nothave an extra key.
The deletion results in a node with onlyone key, which is not acceptable for a B-tree of order 5.
If the sib
ling node to the immediate
left orright has an extra key, we can then borrow
a key from the parent and move a key upfrom this sibling.
8/3/2019 Lec 28 B Tree Operations
18/23
Contd In our specific case, the sibling to the right
has an extra key.
So, the successor W of S (the last key in
the node where the deletion occurred), is
moved down from the parent, and the X is
moved up. Of course, the S is moved over so that the
W can be inserted in its proper place.
8/3/2019 Lec 28 B Tree Operations
19/23
Contd
8/3/2019 Lec 28 B Tree Operations
20/23
Finally De
lete E
This one causes lots of problems
How?
Although E is in a leaf, the leaf has no extrakeys, nor do the siblings to the immediate rightorleft.
In such a case the leaf has to be combined withone of these two siblings.
This includes moving down the parent's key thatwas between those of these two leaves. In ourexample, let's combine the leaf containing F withthe leaf containing A C. We also move down theD.
8/3/2019 Lec 28 B Tree Operations
21/23
Contd
8/3/2019 Lec 28 B Tree Operations
22/23
Contd Parent node now contains only one key, G. This
is not acceptable.
Immediate left or right that had a spare key, thenwe would again "borrow" a key.
Since in our example we have no way to borrow
a key from a sibling, we must again combine
with the sibling, and move down the M from the
parent.
In this case, the tree shrinks in height by one.
8/3/2019 Lec 28 B Tree Operations
23/23
Final
B-Tree