Unit V
Unit V
Herbert A. Evans
Unit V : Indexing and Multiway
Trees
Indexing and Multiway Trees- Indexing, indexing
techniques-primary, secondary, dense, sparse, Multiway
search trees, B-Tree- insertion, deletion, B+Tree -
insertion, deletion, use of B+ tree inIndexing, Trie Tree.
Purposes of Data Indexing
• Why it is important?
Concept of File Systems
42 51 62
1 2 4 7 8 13 15 18 25
27 29 45 46 48 53 55 60 64 70 90
20 10 50 30 40 70 60 80 90
Solution
20 10 50 30 40 70 60 80 90
20
• 1
• 2.
10,20
• 3.
10,20
20 ,50 20
10 50
Solution
• 4.
20
10 30,50
• 5. 20 20,40
10 30,40
40 ,50 10 30 50
Solution
20,40
• 6.
10 30 50,70
• 7.
20,40 40
20, 40, 60
10 30 60 70
50,60, 10 30 50 70
Solution
40
20 60
10 30 50 70
Solution
• 8.
40
20 60
10 30 50 70,80
Solution
• 9.
40
20 60
10 30 50 70, 80
80, 90
Solution
40
20 60,80
10 30 50 70 90
Constructing a B-tree
• Suppose we start with an empty B-tree and keys
arrive in the following order:1 12 8 2 25 6 14
28 17 7 52 16 48 68 3 26 29 53 55 45
• We want to construct a B-tree of order 5
• The first four items go into the root:
1 2 8 12
1 2 12 25
1 2 6 12 14 25 28
B-Trees
Constructing a B-tree (contd.)
Adding 17 to the right leaf node would over-fill it, so we take the
middle key, promote it (to the root) and split the leaf
8 17
1 2 6 12 14 25 28
1 2 6 7 12 14 16 25 28 48 52
B-Trees
Constructing a B-tree (contd.)
Adding 68 causes us to split the right most leaf, promoting 48 to the
root, and adding 3 causes us to split the left most leaf, promoting 3
to the root; 26, 29, 53, 55 then go into the leaves
3 8 17 48
1 2 6 7 12 14 16 25 26 28 29 52 53 55 68
B-Trees
Constructing a B-tree (contd.)
17
3 8 28 48
1 2 6 7 12 14 16 25 26 29 45 52 53 55 68
B-Trees
Inserting into a B-Tree
• Attempt to insert the new key into a leaf
• If this would result in that leaf becoming too big, split the
leaf into two, promoting the middle key to the leaf’s
parent
• If this would result in the parent becoming too big, split
the parent into two, promoting the middle key
• This strategy might have to be repeated all the way to
the top
• If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher
B-Trees
Exercise in Inserting a B-Tree
• Insert the following keys to a 5-way B-tree:
• 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8,
19, 4, 31, 35, 56
B-Trees
Removal from a B-tree
• During insertion, the key always goes into a leaf. For
deletion we wish to remove from a leaf. There are three
possible ways we can do this:
• 1 - If the key is already in a leaf node, and removing it
doesn’t cause that leaf node to have too few keys, then
simply remove the key to be deleted.
• 2 - If the key is not in a leaf then it is guaranteed (by the
nature of a B-tree) that its predecessor or successor will
be in a leaf -- in this case we can delete the key and
promote the predecessor or successor key to the non-
leaf deleted key’s position.
B-Trees
Removal from a B-tree (2)
• If (1) or (2) lead to a leaf node containing less than the minimum
number of keys then we have to look at the siblings immediately
adjacent to the leaf in question:
– 3: if one of them has more than the min. number of keys then we
can promote one of its keys to the parent and take the parent
key into our lacking leaf
– 4: if neither of them has more than the min. number of keys then
the lacking leaf and one of its neighbours can be combined with
their shared parent (the opposite of promoting a key) and the
new leaf will have the correct number of keys; if this step leave
the parent with too few keys then we repeat the process up to
the root itself, if required
B-Trees
Type #1: Simple leaf deletion
Assuming a 5-way
B-Tree, as before... 12 29 52
2 7 9 15 22 31 43 56 69 72
B-Trees
Type #2: Simple non-leaf
deletion
Delete 52
12 29 56
52
7 9 15 22 31 43 56 69 72
B-Trees
Type #4: Too few keys in node
and its siblings
12 29 56
7 9 15 22 31 43 69 72
Too few keys!
Delete 72
B-Trees
Type #4: Too few keys in node
and its siblings
12 29
7 9 15 22 31 43 56 69
B-Trees
Type #3: Enough siblings
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
B-Trees
Type #3: Enough siblings
12 31
7 9 15 29 43 56 69
B-Trees
B+ Tree
• B+ Tree is an extension of B Tree which allows efficient
insertion, deletion and search operations.
• In B Tree, Keys and records both can be stored in the
internal as well as leaf nodes. Whereas, in B+ tree,
records (data) can only be stored on the leaf nodes while
internal nodes can only store the key values.
• The leaf nodes of a B+ tree are linked together in the
form of a singly linked lists to make the search queries
more efficient.
B+Tree Advantages
Comparison of B Tree and B+Tree
B+Tree Example
Trie Tree
• What is Trie-
• The word "Trie" is an excerpt from the word
"retrieval". Trie is a sorted tree-based data-structure
that stores the set of strings.
• A trie (derived from retrieval) is a multiway tree data
structure used for storing strings over an alphabet. It
is used to store a large amount of strings. The
pattern matching can be done efficiently using tries.
• The trie shows words like allot, alone, ant, and, are,
bat, bad. The idea is that all strings sharing common
prefix should come from a common node. The tries
are used in spell checking programs.
Trie Tree
Properties of the Trie for a set of the string: