Unit 5 Indexing 2024
Unit 5 Indexing 2024
• Address of data from index file is used to retrieve data from main file.
• Index files are typically much smaller than the original file.
Advantages:
1. Indexing provide better flexibility. Key are ordered.
2. Requires less storage compared to main file. Only Keys are stored and pointers.
3. Inserting new records doesn’t affect main file. Need to update index structure.
Indexing Techniques
Key Pointer ID InstructorName Department Salary
10101 22222 Einstein Physics 60000
12121 10101 Srinivasan Computer 90000
22222 76543 Singh Mechanical 70000
33456 33456 Raj Computer 80000
76543 12121 Vijay Civil 75000
…. … …. …. …
1. B+ Tree Indexing
2. B- Tree Indexing
Indexing Techniques
• Hashed Indexes
• Tree Indexing (B-Tree).
• Trie indexing – (Compact
Trie).
Indexing Techniques
• Hashed Indexes:
• Hash indexes are same as hash tables.
• Record of file are divided among buckets. A bucket is either one block or cluster of
block.
• Hashing function maps a key into a bucket number. Buckets are numbered from 0 to b –
1.
0 • 230 480
Translation of bucket number to disk block is done with the help of bucket directory.
1 460 790 460
. 580 810 580
.
.
. 321 382
. 531 392
. 651 662
. . .
b–1
Bucket Directory
Indexing Techniques
• Tree Indexing:
• AVL tree is example of Tree Indexing.
• Indexes can be stored in tree nodes.
• Search, insertion and deletion can be easy.
• An m- way search tree reduces the height of a search tree and thus decreases no. of
disk access.
Indexing Techniques
• Tree Indexing:
• AVL tree is example of Tree Indexing.
• Indexes can be stored in tree nodes.
• Search, insertion and deletion can be easy.
• An m- way search tree reduces the height of a search tree and thus decreases no. of
disk access.
Indexing Techniques B G
• Trie Indexing:
i o
• Trie is an efficient information reTrieval
structure. g o
• data
It is useful when key values are of varying size.
$ g d g
• A trie is a tree of degree m>=2.
$ l
• Useful for storing words as sequence of characters. e
B-tree
B-Tree
• Construct a B-Tree of order 5 for following data: 78, 21, 14, 11, 97, 85,
74,
63, 45, 42, 57, 20, 16, 19, 32, 30, 31.
B-Tree
• Construct a B-Tree of order 5 for following data: 78, 21, 14, 11, 97, 85,
74,
63, 45, 42, 57, 20, 16, 19, 32, 30, 31.
32
16 21 57 78
11 14 19 20 30 31 42 45 63 74 85 97
B–
Tree.
B-Tree
• Construct a B-Tree of order 3 for following data: 1, 2, 3, 4, 5, 6, 7, 8, 9,
10.
B-Tree 30 80
• Deletion Operation:
50 61 73 89 95
9 18
2 5 8 11 13 17 51 56 58 68 70 75 76 78 84 86 87 90 92 97 99 100
25
of � − 𝟏
• A node (except root node) should contain a minimum• Both Left & Right sibling have min. no. of keys.
�
• If key to be deleted is in internal
𝟐
• Facts of B-tree
keys. of degree m.
• Inorder Predecessor, Inorder Sucessor node.
• A node can have a maximum of m
children.
B-Tree 30 80
9 17 89 95
61
75
2 5 8 11 13 18 29 43 45 50 58 70 73 76 78 84 86 87 90 92 97 99 100
58
9 30 78 89
2 5 8 11 13 17 29 43 45 50 70 73 76 84 86 87 90 92 99 100
B+-Tree
• Insertion Operation:
1. Each node except root can have a maximum of M children and at least
ceil(M/2) children.
2. Each node can contain a maximum of M – 1 keys and a minimum of ceil(M/2) – 1 keys.
3. The root has at least two children and atleast one search key.
4. While insertion overflow of the node occurs when it contains more than M – 1 search
key values.
5. Here M is the order of B+ tree.
B+-Tree
• Insertion Operation:
Steps for insertion in B+ Tree
Every element is inserted into a leaf node. So, go to the appropriate leaf node.
Insert the key into the leaf node in increasing order only if there is no overflow. If there is an overflow go ahead with the
following steps mentioned below to deal with overflow while maintaining the B+ Tree properties.
1 4 17 19 45
22 23 30 31 32 Overflow, so
24 30 27
split node
3. 28 30
26 27 28 29
15 22 23 31 32
30
24
22 23 28 30 31 32
B-Tree
• Construct a B+-Tree of order 5 for following data: 30, 31, 23, 32, 22, 28,
24,
29, 15, 26, 27, 34, 39, 36.
Overflow, so
24 30
split node 27
26 27 28 29
15 22 31 32
30
23 24 28 30
24
15 22 23 24 26 27 28 29 30 31 32
24 28 30
34, 39
15 22 23 24 26 27 28 29 30 31 32 34 39
B+Tree
• Construct a B+-Tree of order 5 for following data: 30, 31, 23, 32, 22, 28,
24,
29, 15, 26, 27, 34, 39, 36.
24 28 30
Overflow, so
36 split node
26 27 28 31 32 34 36
15 22 23 24 29 30
39
24 28 30 34
15 22 23 24 26 27 28 29 30 31 32 34 36 39
B+ - Tree
B+Tree
• Construct a B+-Tree of order 3 for following data: 6, 19, 17, 11, 3, 12, 8,
20,
22, 23, 13, 18.
B+ - Tree
B+Tree
• Problem: Insert the following key values 6, 16, 26, 36, 46 on a B+ tree
with order = 3.
B+ - Tree
B-Tree vs B+ Tree
•.
Binary Heap
• A Heap is a complete binary tree.
• In complete binary tree elements are filled
level by level from left to right.
• Bottom level may not be completely filled.
• A Binary Heap is either Min or Max Heap.
Complete Binary
• Min Heap: Value of each node is greater Tree
than or equal to the value of its parent,
with the minimum-value element at the
root.
• Max Heap: Value of each node is less than
or equal to the value of its parent, with
the maximum-value element at the root.
Note: Min-Heap or Max-Heap tree is complete Binary tree. So elements are added at leaf from left to
right.
Binary Heap
•Create Max Heap with following elements : 6, 2, 10, 3, 12, 60, 7, 200,
8.
• Insert 6 : 6 Insert 2 : 6 Insert 10:
2
• Insert 3: Insert 12:
• Insert 8:
Binary Heap
•Create Min Heap with following elements : 6, 2, 10, 3, 12, 60, 7, 200,
8.
Binary Heap
• Deletion of node in Min-Heap or Max-Heap tree.
• Replace the value of root with the value of rightmost leaf and remove leaf from tree.
• Now check whether the tree follows the Heap property.
• If not, swap the element with its parent and return to previous step. For Max-Heap if
parent is less than both the children then replace the root with larger child.
Heap Sort
• From the unsorted list create a Heap tree (Min or Max).
• Once Heap is created, now select the first element(root) and add it to
array.
• Remove first element and rearrange the Heap and repeat above step.
• Heap sort algorithm:
1. Create binary tree from the given elements.
2. Convert binary tree to Min Heap or Max Heap.
3. Delete the root element from the Heap.
4. Add deleted element to array.
5. Rearrange the Heap and repeat the step 3 to 5 till Heap is empty.
6. If heap is empty , display sorted list from array and stop.
Heap Sort
• Sort numbers in
ascending
order using
heapsort :
50, 2 , 1, 84, 12,
4, 102.
• Create Max-Heap:
• Sort
Heap Sort
numbers
ascending
in order
using heapsort :
50, 2 , 1, 84, 12, 4,
102.
• Sorting Data:
Sort the numbers using heapsort: 12, 45, 21, 76, 83, 97, 82, 54.
Sort the numbers using heapsort: 25, 4, 70, 1, 60, 10, 85, 11.
Questions
• Insert 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.
• Construct B+ tree of order 3 : 1, 42, 28, 21, 31, 10, 17, 7, 31, 25, 20, 18.
• Explain Heap tree, Max-Heap & Min-Heap.
• Write Algorithm & Pseudo code for Inserting Element in Min Heap and Max Heap.
• Create Min Heap & Max Heap for given data: 10, 20, 15, 12, 25, 30, 14, 2, 5, 4.
• Delete root node of above Heap tree and Re-arrange Heap tree.
• Write algorithm to sort the numbers in ascending order using heapsort.
• Sort the numbers using heapsort: 18, 13, 12, 22, 15, 24, 10, 16, 19, 14, 30.