620 likes | 921 Views
Data Structures B-tree. Jibrael Jos : Sep 2009. Agenda. Introduction Multiway Trees B Tree Application Structure Algo : Insert / Delete. B Tree. Binary Search Tree. What happens if data is loaded in a binary search tree in this order 23, 32, 45, 11, 43 , 41 1,2,3,4,5,6,7,8
E N D
Data StructuresB-tree Jibrael Jos : Sep 2009
Agenda Introduction Multiway Trees B Tree Application Structure Algo : Insert / Delete Avoid Taking Printout : Use RTF Outline in case needed
B Tree Please Do Not Take Printout : Use RTF Outline in case needed
Binary Search Tree • What happens if data is loaded in a binary search tree in this order • 23, 32, 45, 11, 43 , 41 • 1,2,3,4,5,6,7,8 • What is AVL tree Please Do Not Take Printout : Use RTF Outline in case needed
Multiway Trees >= K1 <K2 >= K2 < K1 Please Do Not Take Printout : Use RTF Outline in case needed
m-way trees • Reduce the depth of the tree to O(logmn)with m-way trees • mchildren, m-1 keys per node • m = 10 : 106 keys in 6 levels vs 20 for a binary tree • but ........
m-way trees • But you have to search through the m keys in each node! • Reduces your gain from having fewer levels!
B-trees • All leaves are on the same level • All nodes except for the root and the leaveshave • at least m/2 children • at most m children Each node is at least half full of keys
Multiway Tree • M – ary tree • 3 levels : • Cylinder , Track , Record : Index Seq (RDBMS) • Tables with less change Please Do Not Take Printout : Use RTF Outline in case needed
BTree • If level is 3, m =199 then what is N • How many split per insertion ? Please Do Not Take Printout : Use RTF Outline in case needed
Multiway Trees : Application • NDPL , Delhi: Electricity Billing • 3 lakh consumers • Table indexed as BTREE • UCO Bank, Jaipur • One DD takes 10 minutes to print • Saviour : BTREE Please Do Not Take Printout : Use RTF Outline in case needed
B-trees - Insertion • Insertion • B-tree property : block is at least half-full of keys • Insertion into block with m keys • block overflows • split block • promote one key • split parent if necessary • if root is split, tree becomes one level deeper
Insert Node 63
Insert Node 99
Split Node 0 4 node
Structure of Btree Avoid Taking Printout : Use RTF Outline in case needed
Split Node : Final 0 3 4 median entry fromNdx 3 node 2 toNdx 2 rightPtr
Split Node : Final 4 3 4 median entry fromNdx 3 node 1 toNdx 2 rightPtr
Agenda Delete Delete Walk Through Reflow Borrow Left Borrow Right Combine Delete Mid Avoid Taking Printout : Use RTF Outline in case needed
Delete : For 78 • Btree Delete • Delete() • Delete() • Delete Mid() • Reflow() • Reflow() • If shorter delete root 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
B Btree Delete Target = 78 • If (root null) • print (“Attempt to delete from null tree”) • Else • shorter = delete (root, target) • if Shorter • delete root • Return root 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete(root , deleteKey) D Target = 78 • If (root null) • data does not exist • Else • entryNdx= searchNode(root, deleteKey) • if found entry to be deleted • if leaf node • underflow=deleteEntry() • else • underflow=deleteMid (left) • if underflow • underflow=reflow() 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete Else Part D Target = 78 • Else • if deleteKey less than first entry • subtree=firstPtr • else • subtree=rightPtr • underflow= delete (subtree,deleteKey) • if underflow • underflow= reflow() • Return underflow 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete(root , deleteKey) D D Target = 78 • If (root null) • data does not exist • Else • entryNdx= searchNode(root, deleteKey) • if found entry to be deleted • if leaf node • underflow=deleteEntry() • else • underflow=deleteMid (root,entryIndx,left) • if underflow • underflow=reflow(root,entryIndx) DM 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete(root , deleteKey) D D 74 replaces 78 • If (root null) • data does not exist • Else • entryNdx= searchNode(root, deleteKey) • if found entry to be deleted • if leaf node • underflow=deleteEntry() • else • underflow=deleteMid (root,entryIndx,left) • if underflow • underflow=reflow(root,entryIndx) 1 2 2 2 1 2 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete(root , deleteKey) D D After Reflow • If (root null) • data does not exist • Else • entryNdx= searchNode(root, deleteKey) • if found entry to be deleted • if leaf node • underflow=deleteEntry() • else • underflow=deleteMid (root,entryIndx,left) • if underflow • underflow=reflow(root,entryIndx) 1 1 2 2 4 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete Else Part D Before Reflow • Else • if deleteKey less than first entry • subtree=firstPtr • else • subtree=rightPtr • underflow= delete (subtree,deleteKey) • if underflow • underflow= reflow(root,entryIndx) • Return underflow 1 1 2 2 4 Please Do Not Take Printout : Use RTF Outline in case needed
B Delete Else Part D After Reflow • Else • if deleteKey less than first entry • subtree=firstPtr • else • subtree=rightPtr • underflow= delete (subtree,deleteKey) • if underflow • underflow= reflow(root,entryIndx) • Return underflow 0 4 2 4 Please Do Not Take Printout : Use RTF Outline in case needed
B BTREE Delete • If (root null) • print (“Attempt to delete from null tree”) • Else • shorter = delete (root, target) • if Shorter • delete root • Return root 0 4 2 4 Please Do Not Take Printout : Use RTF Outline in case needed
B BTREE Delete • If (root null) • print (“Attempt to delete from null tree”) • Else • shorter = delete (root, target) • if Shorter • delete root • Return root 4 2 4 Please Do Not Take Printout : Use RTF Outline in case needed
Delete : For 78 • Btree Delete • Delete() • Delete() • Delete Mid() • Reflow() • Reflow() • If shorter delete root 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
Delete : Reflow • 1: Try to borrow right. • 2: If 1 failed try to borrow from left • 3: Cannot Borrow (1,2 failed) Combine Please Do Not Take Printout : Use RTF Outline in case needed
Delete Reflow • Underflow=false • If RT->no > min Entries • BorrowRight (root,entryNdx,LT,RT) • Else • If LT->no > min Entries • BorrowLeft (root,entryNdx,LT,RT) • Else • combine (root,entryNdx,LT,RT) • if root->no < min entries • underflow=True • Return underflow Please Do Not Take Printout : Use RTF Outline in case needed
Borrow Left 2 3 1 Node >= 74 < 78 Node >= 78 < 85 Please Do Not Take Printout : Use RTF Outline in case needed
Combine 3 2 1 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
Combine 3 3 1 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
Combine 3 4 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
Combine 2 4 2 2 Please Do Not Take Printout : Use RTF Outline in case needed
Delete Mid • If leaf • exchange data and delete leaf entry • Else • traverse right to locate predecessor • deleteMid(right) • if underflow • reflow Please Do Not Take Printout : Use RTF Outline in case needed
Delete Mid 1 2 2 2 2 2 Case 1: To Delete 78 we replace with 74 Please Do Not Take Printout : Use RTF Outline in case needed
Delete Mid 1 2 2 2 2 2 Case 2: To Delete 78 we replace with 76 Hence recursive call of Delete Mid to locate predecessor 2 Please Do Not Take Printout : Use RTF Outline in case needed
order Please Do Not Take Printout : Use RTF Outline in case needed
Get the Order Right • Keys are 4 • Subtrees Max is 5 = Order is 5 • Minimum = 3 (which is subtrees) • Min Keys is 2 4 2 4 Please Do Not Take Printout : Use RTF Outline in case needed
2-3 Tree • Order 3 ….. So how many keys in a node • This rule is valid for non root leaf • Root can have 0, 2, 3 subtrees Please Do Not Take Printout : Use RTF Outline in case needed
2 -3 Tree 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed