90 likes | 193 Views
B – Tree Bhargav Vadher (208) San Jose State University Computer Science Department Feb 18 th , 2008. Agenda. Insertion of key in node. Deletion of key from node. Insertion of key in node. 1. Find specific location at leaf level for insertion, if there is room.
E N D
B – Tree Bhargav Vadher (208) San Jose State University Computer Science Department Feb 18th , 2008
Agenda • Insertion of key in node. • Deletion of key from node.
Insertion of key in node 1. Find specific location at leaf level for insertion, if there is room. 2. If there is no room then divide the leaf into two leaf node. 2.1 Divide key-ptr pair such that ptr ≥ n/2. 3. recursion may be required if key-ptr pair need to insert at higher level. 3.1 If there is room at higher level, insert there or go ahead. 4. If there is insertion at root node. 4.1 Divide root two internal node. 4.2 Make new root at upper level. Root must have only one key and two ptrs.
2 possibility for splitting node • Splitting at leaf node. suppose we have leaf node N keys = n = 3 ptrs = (n+1) = 4 and now we want to add (n+1)st key = K4. we have split the node N into two node N and new node M. Condition : (n+1)/2 ptrs would go with node N, and remaining with M. N N M
Splitting at internal node. suppose we have internal node = N keys = n = 3 ptrs = (n+1) = 4 and now we want to add (n+1)st key = K4. we have split the node N into two node N and new node M. Condition : (n+2)/2 ptrs would go with node N, and remaining with M. N N M Example of Insertion node.
Deletion of key from node • Find a leaf node N for deletion of key from a tree. • Delete the requested (key-ptr) pair. • After deletion • If node N satisfy the minimum ptr condition ( (n+1) / 2 ) We are done. Else (i). Borrow (key-ptr) from the sibling M. OR (ii). Combine the sibling M with N.
Case 1: • Borrow (key-ptr) from sibling M. • Adjust the parent of M and N. N M N M But what if, even sibling of N too, don’t have extra (key-ptr) pair. Move to case 2.
Case 2: • Combine N with sibling M. Because M has lack of ptr & N has minimum ptr. • Adjust the key at parent of M and N. • Delete extra ptr from parent. • after deletion if parent has minimum ptr We are done with deletion Else Recursion in higher level. ……. …. N M N
Q & A Thank you.