340 likes | 569 Vues
Foundation of Computing Systems. Lecture 14 B Trees. Indexing Mechanism. m-way Search B-Tree Indexing Trie indexing. m-way Search Tree. Definition An m -way search tree T is a tree in which all nodes are of degree ≤ m Each node in the tree contains the following attributes : where
E N D
Foundation of Computing Systems Lecture 14 B Trees IT 60101: Lecture #14
Indexing Mechanism • m-way Search • B-Tree Indexing • Trie indexing IT 60101: Lecture #14
m-way Search Tree • Definition • An m-way search tree T is a tree in which all nodes are of degree ≤ m • Each node in the tree contains the following attributes: where 1 ≤ n < m Ki (1 ≤ i ≤ n) are key values in the node Pi (0 ≤ i ≤ n) are pointers to the sub-trees of T. Ki < Ki+1, 1 ≤ i < n • All the key values in the sub-tree pointed by Pi are less than the key values Ki+1, 0 ≤ i < n. • All the key values in the sub-tree pointed by Pn is greater than Kn. • All the sub-trees pointed by Pi (0 ≤ i ≤ n) are also m-way search trees. IT 60101: Lecture #14
m-way Search Tree: Example IT 60101: Lecture #14
B-Tree Indexing • A B tree T of order m is an m-way search tree that is either empty, or it satisfies the following properties: • The root node has at least 2 children • All nodes other than the root node have at least child • All failure nodes are at the same level. IT 60101: Lecture #14
Example: B-Tree of Order 3 IT 60101: Lecture #14
Operations on B-Trees • Searching • Insertion • Deletion IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 10 • Initially the B tree is empty. Get a node (note that it is the root node) and insert the key 10 into it IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 20 • A node in a B tree of order m can have at most (m – 1) key values. So, in this case, the root node can hold the key value 20 after 10 IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 30 • A key value is to be inserted into a node which already has the maximum number of key values (that is, m – 1 for a B tree of order m). • Insert the value, say X into the list of values in the node in ascending order • Split the list of values into three parts: P1, P2 and P3 • P1 contains first – 1 key values • P3 contains + 1, ..., m-th values • P2 values contain the -th value. • With this splitting, the -th value is to be inserted into the parent node of the current node • If the parent node is nil, then create a new node. • Note: In place of the current node, two nodes are to be allotted containing the key values in P1 and P3 respectively. IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 30 • A key value is to be inserted into a node which already has the maximum number of key values (that is, m – 1 for a B tree of order m). IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 40 • Search for the node where 40 should be placed IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 50 • 50 should go to node N3, but it is already full. So, it will be splitted followed by rearrangement IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 60 • Search the nodes for 60 (it is N5) and insert it there. IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 70 • The node for insertion of key value 70 is N5, which is already full • So, it requires to split N5 into N6 and N7 • This process in turns require to insert 60 into N1 • Requires another splitting of N1 into N8, N9, N10 IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 80 • Search the nodes for 60 (it is N5) and insert it there. IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 90 • The node N7 is the right place where 90 has to be accommodated but it is full. IT 60101: Lecture #14
Insertion:10, 20, 30, 40, 50, 60, 70, 80, 90 • Insertion of 90 • The node N7 is the right place where 90 has to be accommodated but it is full. • So, splitting of N7 (to N11 and N12) is necessary. • Requires the insertion of 80 into N10, the parent of N7 IT 60101: Lecture #14
Deletion in B-Trees • Case 1: Deletion of a key value from a leaf node • Case 1.a:Removal of a key value leads to the number of keys ≥ – 1. • Case 1.b:Removal of key value leads to the number of keys < – 1. • Case 2: Deletion of a key value from a non-leaf node. IT 60101: Lecture #14
Deletion in B-Trees: Case 1.a • Case 1: Deletion of a key value from a leaf node • Case 1.a:Removal of a key value leads to the number of keys ≥ – 1. • Removal of a key value from the leaf node does not violate the requirement of minimum number of key values in that node IT 60101: Lecture #14
Deletion in B-Trees: Case 1.b • Case 1: Deletion of a key value from a leaf node • Case 1.b:Removal of key value leads to the number of keys < – 1. • Three situations may be possible in this case: 1. The nearest right sibling contains more than – 1 key values. 2. The nearest left sibling contains more than – 1 key values. 3. Neither the nearest left sibling nor the right sibling contain more than – 1 key values. IT 60101: Lecture #14
Deletion in B-Trees: Case 1.b • Case 1: Deletion of a key value from a leaf node • Case 1.b:Removal of key value leads to the number of keys < – 1. The nearest right (or left) sibling contains more than – 1 key values. IT 60101: Lecture #14
Deletion in B-Trees: Case 1.b • Case 1: Deletion of a key value from a leaf node • Case 1.b:Removal of key value leads to the number of keys < – 1. Neither the nearest left sibling nor the right sibling contain more than – 1 key values. IT 60101: Lecture #14
Deletion in B-Trees: Case 1.b • Case 1: Deletion of a key value from a leaf node • Case 1.b:Removal of key value leads to the number of keys < – 1. Neither the nearest left sibling nor the right sibling contain more than – 1 key values. IT 60101: Lecture #14
Deletion in B-Trees: Case 1.b • Case 2: Deletion of a key value from a non-leaf node IT 60101: Lecture #14
Deletion in B-Trees: Case 1.b • Case 2: Deletion of a key value from a non-leaf node IT 60101: Lecture #14
Some Properties of B-Trees • A B-tree is always a height balanced tree • The degree of a B-tree of order m is m, that is, the maximum number of branches that can emanate from a node is m • In a B-tree of order m and height h, • The maximum number of nodes possible = • The maximum number of key values that a node in a B tree of order m can have is m – 1. • The maximum number of key values that is possible in a B tree of order m is IT 60101: Lecture #14
Some Properties of B-Trees • The root node contains at least 2 children • All nodes other than the root node can have at least children. • The minimum number of key values in the root node is 1 (if the B tree is not empty). • The minimum number of key values in any node other than the root node is – 1. • The minimum number of key values in a B-tree of order m is IT 60101: Lecture #14
TRIE Structure • A trie tree is an m-way search tree • Definition • A “trie” is a tree of order m either empty or consisting of an ordered sequence of exactly m tries each of order m. IT 60101: Lecture #14
TRIE Structure: Example IT 60101: Lecture #14
Structure of a node in TRIE • Physical representation • Trie indexing is suitable for maintaining variable sized key values. • Actual key value is never stored but key values are implied through links. • If English alphabets are used, then a trie of order 26 can maintain whole English dictionary. (This is specially termed as lexicographic trie). • It allows us multi-way branching based on the part of key value, not the entire key value. The branching on the i-th level is determined by the i-th component of the key value. IT 60101: Lecture #14
TRIE Structure: Operations • Operations • Searching • Insertion • Deletion • For operations on trie structure see the book • Classic Data Structures • Chapter 7 • PHI, 2nd Edn., 17th Reprint IT 60101: Lecture #14