B Trees - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

B Trees

Description:

... ceil(5/2) = 3 children (thus at least 2 keys). Each leaf node must contain at least ceil(5 ... R is at a leaf, but for this leaf, ceil(m/2)-1 2 is the result. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 21
Provided by: greenc4
Category:
Tags: ceil | trees

less

Transcript and Presenter's Notes

Title: B Trees


1
B Trees
A multiway tree of order m is a tree where each
node has at most m children. If arranged for
searching, we have a multiway search tree of
order m. Here is an example of a multiway tree
(not arranged for searching)
E
K
P
M
B
D
G
Q
T
W
B
D
Q
T
W
2
Example definition of a tree node.
A node in a tree of order 4 may be defined like
this typedef struct nodetype int count
// number of keys ltdatagt keys3 struct
nodetype branch4 nodetype, nodeptr
3
Conditions for a multiway search tree
  • Keys in each node are in ascending order
  • Binary search properties apply to all
    branches.
  • All leaves are at the bottom level. This
    encourages balance.
  • All internal nodes (except the root node) have
    at least ceil(m/2) nonempty children. This
    encourages even spread.
  • The root node can have as few as 2 children if
    it is an internal node, or no children if it is a
    leaf.
  • Each leaf node must contain at least ceil(m/2)
    -1 keys.

ceil is the ceiling of an expression. For
example, ceil(3) 3, ceil(2.2) 3, ceil (2.9)
3.
4
An Example
  • We will use a searchable btree of order 5 as
    example. Thus its properties are
  • All internal nodes must have at least ceil(5/2)
    3 children (thus at least 2 keys).
  • Each leaf node must contain at least ceil(5/2)-1
    2 keys.

Usually, btrees have orders gtgt 5, but this is
just a simple example. In this example, we will
insert the following data into an empty
btree C, N, G, A, H, E, K, Q, M, F, W, L, T, Z,
D, P, R, X, Y, and S.
5
Rules for insertion into a btree
  • To insert a key, search for it in the tree
  • If not found, the search ends at a leaf.
  • It there's room, insert the new item there.
  • If the node is full, split the node nearly in
    the middle, with the median key moving up into
    the parent node. (This may require a split at the
    parent). Note that splitting the node at the
    median strives to maintain balance.

6
Insert C, N, G, and A
A
C
G
N
This is a simple insertion since each node can
take up to 4 keys. Note that we must keep the
nodes ordered to maintain the search
properties of the tree.
7
Insert H
The root node is full, so we must split it. The
median of A, C, G, H, and N is G and thus it is
moved up to become the new root.
G
A
C
H
N
8
Insert E, K, and Q
Here, there is room in all insertion positions so
no splits are needed.
G
A
C
E
H
K
N
Q
9
Insert M
M is to be inserted into a node that is full. We
thus split H,K,M,Q,and N at the median (M here)
and M moves up.
G
M
N
Q
A
C
E
H
K
10
Insert F, W, L, and T
These insertions require no splits.
G
M
N
Q
T
W
H
K
L
A
C
E
F
11
Insert Z
Z is to be inserted into the rightmost leaf,
which is full. We thus have to split it, and T
(the median) goes up to the root.
G
M
T
N
Q
W
Z
H
K
L
A
C
E
F
12
Insert D
D is to be inserted into the left leaf, which is
full. We thus have to split it, and D (the
median) goes up to the root.
D
G
M
T
N
Q
W
Z
H
K
L
E
F
A
C
13
Insert P, R, X, and Y
These insertions do not cause any splits
D
G
M
T
H
K
L
E
F
A
C
N
P
Q
R
W
X
Y
Z
14
Insert S
S belongs in the node containing N,P,Q,R and it
is full so we split it and send Q (the median) to
the parent, which is also full, so we split it in
turn, sending M (the median) up to become the new
root.
M
D
G
Q
T
H
K
L
E
F
A
C
W
X
Y
Z
N
P
R
S
15
Deletions
Now delete H. We first look it up and we notice
that it is at a leaf node. Since the node has 3
keys gt ceil(5/2)-12, simply remove H and
shift the contents of the node over.
M
D
G
Q
T
E
F
A
C
W
X
Y
Z
N
P
K
L
R
S
Deleted H from this node.
16
Delete T
T is not in a leaf. We thus find its successor
(in this case, W). We move W up to replace T. In
all cases, we reduce deletions to a deletion in a
leaf with this method.
M
D
G
Q
W
E
F
A
C
N
P
K
L
X
Y
Z
R
S
17
Delete R
R is at a leaf, but for this leaf, ceil(m/2)-1lt2
is the result. We have to borrow a key from the
parent, and replenish the parent from a sibling
of the leaf where the deletion occurred (if a
sibling has extra keys). Here, the successor W of
S (the last key in the node where the deletion
occurred) is moved dpwn from the parent, and X is
moved up.
M
D
G
Q
W
E
F
A
C
N
P
K
L
R
S
X
Y
Z
M
D
G
Q
X
E
F
A
C
N
P
Y
Z
K
L
S
W
18
Delete E
The node containing E has no extra keys, and its
siblings have no extra keys as well. So the leaf
has to be combined with one of the siblings. This
includes moving the parent's key down. Here we
combine F with the leaf containing A and C.
M
D
G
Q
X
D
G
Q
X
E
F
A
C
E
F
A
C
N
P
Y
Z
K
L
N
P
Y
Z
K
L
S
W
S
W
M
Q
X
Q
X
G
N
P
K
N
P
K
A
C
D
F
Y
Z
L
S
W
Y
Z
L
S
W
19
Delete E ct'd
But now, the node containing G has only 1 key. If
this node had a sibling to its immediate left or
right that had extra keys, we would again borrow
a key.
M
G
Q
X
Q
X
problem!
N
P
Y
K
L
N
P
Y
K
L
A
C
D
F
Z
S
W
Z
S
W
Suppose that the node containing Q,X has one more
key, say to the right of Q. We would move M down
to the node with too few keys (G) and move Q up
to where M was. Note that in this case, the old
left subtree of Q (N, P) would have to become the
right subtree of M. However, here we are unable
to borrow a key from a sibling, so we have to
combine again with a sibling, and move down the
M. The tree shrinks in height by one level.
20
Delete E ct'd
M
G
Q
X
Q
X
problem!
N
P
Y
Z
K
L
N
P
Y
Z
K
L
A
C
D
F
S
W
S
W
G
M
Q
X
N
P
Y
Z
K
L
N
P
Y
Z
K
L
A
C
D
F
S
W
S
W
Write a Comment
User Comments (0)
About PowerShow.com