Title: Multiway
1Multi-way Trees
2- M-way trees
- So far we have discussed binary trees only.
- In this lecture, we go over another type of tree
called m-way trees or trees of order m. - In a binary tree
- Each node has only one key and
- Each node has up to two children
- In a m-way tree
- Each node hold at least 1 and at most m-1 keys
and - Each node has at most m children
3- B-Tree
- An example of m-way tree is called B-trees
- A B-tree of order m is a multiway search tree
with the following properties - The root has at least two subtrees unless it is a
leaf - Except the root and the leaves, every other node
has at most m children and at least m/2 children - This means every node hold at most m-1 keys and
at least (m/2) -1 keys - For example, a tree of order m5, holds at least
2 and at most 4 keys and 5 pointers. - Similarly, a tree of order m8, hold at least 3
and at most 7 keys in each node and has 8
pointers. - Every leaf node holds at most m-1 keys and at
least (m/2) -1 keys - All leaves are on the same level
4- Inserting a key into a B-Tree
- Some of the differences of B-trees compare to
binary trees is that - All leaves are in the last level of the tree.
Note that this was not necessarily the case for
the binary tree - The tree is built bottom-up rather than up to
bottom as it is in binary trees - In general there are three cases to consider when
we insert a element into a B-tree of order m.
5- Insertion into
- B Tree Case 1
- A key is placed in a leaf that still has some
room - For example, as shown in the following example,
in a B-tree of order 5, a new key, 7, is placed
in a leaf, preserving the order of keys in the
leaf so that key 8 must be shifted to the right
by one position
12
13
8
5
15
12
7
8
13
5
15
6- Insertion into
- B Tree Case 2
- The leaf in which a key should be placed is full.
- In this case the leaf is split, creating a new
leaf, and half of the keys are moved from the
leaf full node to the new leaf. - The last key of the old leaf is moved to the
parent and a pointer to the new leaf is placed in
the parent as well.
12
Want to insert 6
13
5
2
15
7
8
12
move
5
8
6
15
2
7
13
6
12
5
8
2
7
15
7- Insertion into B Tree Case 3
- Suppose you want to insert a key into a full node
- Because the node is full, the node is split and
the middle key is moved to the parent node as we
explained in case 2. - What if the parent has no more room?
- If parent has no more room, we need to split the
parent node, create two nodes and move the middle
key up the tree into the parent of the parent. - If parent of the parent does not exist, we create
one - If parent of the parent has room , we insert the
middle key there - If parent of the parent is also full, we repeat
the same process again
86
12
20
30
Insert 13
Move
19
18
30
20
19
18
9Algorithm for inserting into B-Tree BTreeInsert(
K) Find a leaf node to insert While
(true) Find a proper position in the leaf for
K If there is space in that node Insert K in
proper position Return Else split in node in
node1 and node2 Distribute keys and pointers
evenly between node1 and node2 K the last key
of node1 If node was the root Create a new root
as parent of node1 and node2 Put K and pointers
to node1 and node2 in the root Return Else
node its parent // now process the parent
node if it is full
10- Step by step of insertion into a B-Tree
- Insert 8, 14, 2, 15, 3, 1, 16, 6, 5, 27, 37, 18,
25, 7, 13, 20, 22, 23, 24 into a tree of order m
5
After inserting 8, 14, 2, and 15
After inserting 3
8
14
15
11After inserting 1, 16, 6
After inserting 5
12After inserting 27
27
14
5
6
15
16
27
After inserting 37
16
5
6
14
15
27
13After inserting 18, 25, 7, 13
16
15
5
6
14
7
13
27
27
37
25
18
After inserting 20
16
25
15
14
13
5
6
7
27
37
20
18
27
14After inserting 22, 23
25
16
15
13
14
5
6
7
27
20
37
22
23
18
27
After inserting 24
16
22
25
7
37
15
24
23
27
20
18
15- Another example
- This time we want to insert a set of numbers into
a tree of order m 4. - For order 4, the number of keys in each node is
at least 1 and at most 3. - Insert 8, 14, 2, 15, 3, 1, 16, 6, 5, 27, 37, 18,
25, 7, 13, 20, 22, 23, 24
After inserting 8, 14, and 2
2
14
8
After inserting 15
8
14
15
2
16After inserting 3, 1, and 16
8
14
15
16
2
3
1
After inserting 6
2
8
14
15
16
6
3
1
17After inserting 5
2
8
14
15
16
6
5
3
1
After inserting 27
15
2
8
16
14
27
6
5
3
1
18After inserting 37, 13, 12
15
2
8
16
12
27
37
13
6
5
3
14
1
After inserting 20
8
15
20
2
12
1
13
5
3
6
14
16
37
27
19- Another example
- This time we present the B-tree with more detail
that shows how the index keys are connected to
specific records in the disk. - Suppose we want to create indexing using B-tree
of order m 3 for the following employee records
in the disk - Assuming that the EmpId is unique in the employee
table, the best index key can be the EmpId - This example shows step by step of creating
B-tree of order m3 and illustrates how the
pointers are linked to the records in the disk
20Before
After
21Before
After
22Before
After
23Before
After
24Before
After
8
71
2
15
80
25Before
8
71
2
15
80
After
8
71
2
15
80
63
26Before
8
71
2
15
80
63
After
8
71
2
15
80
63
90
27Before
8
71
2
15
80
63
90
55
After
8
71
80
15
2
90
63
2855
Before
8
71
80
15
2
90
63
55
After
8
71
80
15
2
90
35
63
2955
Before
8
71
80
15
2
90
35
63
After
3090
80
71
63
51
55
35
15
8
2
31- Deleting from a B-tree
- For the delete operation, there are two general
cases - Deleting a key from the leaf
- Deleting a key from a non-leaf
- Case 1 Deleting from a leaf node
- If after deleting a key K, the leaf is at least
half full, simply delete the element
32- If after deleting, the number of keys in the leaf
is less than - (m/2) -1, causing an underflow
- If there is a left or right sibling with the
number of keys exceeding the minimal (m/2) -1,
then all keys from this leaf and this sibling are
redistributed between the two nodes
Before deleting 7
15
13
14
5
7
27
After deleting 7
3
13
8
5
14
27
15
33- If after deleting, the number of keys in the leaf
is less than - (m/2) -1, causing an underflow
- If neither left no right sibling have more than
minimal f - (m/2) -1, then merge the node with one of the
siblings and place proper index in the parent node
Before deleting 8
3
13
merge
8
5
14
27
15
After deleting 8
3
14
15
5
27
13
34- A particular case results in merging a leaf or
nonleaf with its sibling when its parent is the
root with only one key. - In this case, the keys from the node and its
sibling, along with the only key of the root, are
put in the node which becomes a new root, and
both the sibling and the old root nodes are
discarded. - This is the only case when two nodes disappear at
the same time. - Also the height of the tree is decreased by one
- See the next example.
35Before Deleting 8
16
22
25
merge
37
14
15
24
23
27
20
18
- is deleted but process continues
16
merge
22
25
3
15
37
14
13
24
23
27
20
5
18
After deleting 8
3
16
22
25
15
27
37
14
13
24
23
20
5
18
36- Case 2
- Deleting from a non-leaf node
- This can lead to problems with reorganization.
- Therefore, deleting from a nonleaf node should be
reduced to deleting a key from a leaf to make the
task simple - The key to be deleted is replaced by its
immediate predecessor (the successor could also
be used) which can only be found in a leaf. - This predecessor key is deleted from the leaf
based on the algorithm we discussed in case 1
37Before deleting 16
3
16
22
25
15
27
37
14
13
24
23
20
5
18
Swap 16 and 15 and delete 16
3
15
22
25
16
27
37
14
13
24
23
20
5
18
After deleting 16
3
15
22
25
27
37
14
13
24
23
20
5
18
38B-Tree delete algorithm Node Search for the
node that contains key K to be deleted If (node
is not a leaf) Find a leaf with the closest
successor/predecessor S of K Copy S over K in
node Node the leaf containing S Delete S from
node Else delete K from node While
(1) If node does not underflow Return else
if there is a sibling of node with enough keys
(i. e. more than (m/2)-1) Redistribute keys
between node and its sibling Return else if
nodes parent is the root If the parent has only
one key Merge node, its sibling, and parent to
form a new root else merge node and its
sibling Return else merge node and its
sibling node its parent
39- B Tree
- A B-Tree is a variant of the B-Tree .
- All the nodes except the root are required to be
at least two-third full. - More precisely, the number of keys in all nodes
except the root in a B-tree of order m is k
where - (2m-1/3) ltk lt m-1
- In this type of tree, the frequency of node
splitting is decreased by delaying a split and
when the time comes we split two nodes into three
(not one node into two as done in B-tree) - Lets see some examples of inserting into a B
tree
40Before inserting 6
16
2
12
18
30
27
28
0
10
25
1
7
5
9
After inserting 6
41- B Tree Cont.
- As shown in the previous slide, the key 6 is to
be inserted into the left node which is already
full - Instead of splitting the node, all keys from this
node and its sibling are evenly divided and the
median key, key 10, is put into the parent - Notice that this not only evenly divides the
keys, but also it frees some space in the nodes
for more key - If the sibling is also full, a split occurs and
one new node is created, the keys from the node
and its sibling (along with the separating keys
from the parent) are evenly divided among three
nodes and two separating keys are put into the
parent - See the next slide for an example.
42Before inserting 4
10
2
9
12
27
28
30
18
25
0
8
16
29
1
6
5
7
After inserting 4
6
16
6
18
29
30
27
28
25
0
7
12
9
10
1
8
4
2
5
43- B Tree
- Basically, a node in a B-tree structure
represents one secondary page or a disk block - The passing from one node to another node can be
a time consuming operation in case we need to do
something like in-order traversal or print of the
B-tree - B tree is enhanced form of B-tree that allow us
to access data sequentially in a faster manner
than using in order traversal - In a B-tree, references to data are made from any
node of the tree but in a B tree, these
references are made only from the leaves - The internal nodes of a B-tree are indexes to the
leaves for fast access to the data
44- B Tree Cont.
- In a B tree, the leaves have a different
structure than other nodes of the B tree and
usually they are linked sequentially to form a
sequence set so that scanning this list of leaves
results in data given in ascending order - The reason this is called B tree is that
- The internal nodes (not the leaves) all have the
same structure as the B-tree) plus - The leaves make a linked list of the keys
- Thus we can say that B tree is a combination of
indexes plus a linked list of keys - The internal node of B tree stores keys, and
pointers to the next level nodes - The leaves store keys, references to the records
in a file, and pointer to the next leaf.
45- Algorithm for inserting into a B tree
- During the insert, when a leaf node is full and a
new entry is inserted there, the node overflows
and must split - Given that the order of the B-tree is p, the
split of a leaf node causes the first p/2
entries (index keys) to remain in the original
node and the rest move to the new node - A copy of the middle key is placed into the
parent node - If the parent (non-leaf node) is full and we try
to insert a new key there, the parent splits.
Half of the nodes stay in the original node, the
other half move to the new node, and the middle
key is moved (not copied) to the parent node
(just like B-tree). - This process can be propagated all the way up to
the root. - In the next example, we go through step by step
(with pointer details) of inserting into a B
tree
46- Example of a B-Tree of order 3
- Example of inserting the index for the following
records into a B-tree
47Before
After
48Before
After
49Before
After
5
1
5
8
50Before
5
1
5
8
After
5
1
5
7
8
515
Before
1
5
7
8
After
3
5
5
7
8
1
3
523
Before
5
5
7
8
1
3
5
After
3
8
5
7
8
1
3
12
535
Before
3
8
5
7
8
1
3
12
After
5
3
8
5
7
8
1
3
9
12
545
Before
3
8
5
7
1
3
9
12
8
5
After
3
7
8
5
6
1
3
9
12
8
7
5512
9
8
8
7
7
6
5
3
5
3
1
56- Deleting from B tree
- If the deleting of a key does not cause
underflow, we just have to make sure other keys
are properly sorted - Even if the index of the key to be deleted is in
the internal node, the index can still be there
because it is just a separator
3
Before Deleting 3
5
5
7
8
1
3
After Deleting 3
3
5
5
7
8
1
57- When delete of a node from a leaf causes an
underflow, then either the keys from this leaf
and the keys of a sibling are redistributed
between this leaf and its sibling or the leaf is
deleted and the remaining keys are included in
the sibling
Before Deleting 12
5
3
8
5
7
8
1
3
12
After Deleting 12
5
3
7
5
7
1
3
8
58- Trie
- In the previous examples we have used the entire
key (not just part of it) to do searching of an
index or an element - A tree that uses parts of the key to navigate the
search is called a trie (pronounced try) - Each key is a sequence of characters and a trie
is organized around these characters rather than
entire keys - Suppose that all keys are made of 5 letters A, E,
I, P and R - The next slide shows an example of a trie.
- For example, search for word ERIE, we first
check the first level of trie, the pointer
corresponding to the first letter of this word
E is checked - Since this pointer is not null, the second level
is checked. Again it is not null and we follow
the pointer from letter R - Again other levels are checked till you either
find the word or you reach NULL.
59IPA
IRE
EIre
Pier
Ara
Pear
Peer
Per
Rep
Erie
Era
Rear
Ere
Are
Are a