1 / 55

Data Structures B-tree

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

brac
Download Presentation

Data Structures B-tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresB-tree Jibrael Jos : Sep 2009

  2. Agenda Introduction Multiway Trees B Tree Application Structure Algo : Insert / Delete Avoid Taking Printout : Use RTF Outline in case needed

  3. B Tree Please Do Not Take Printout : Use RTF Outline in case needed

  4. 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

  5. Multiway Trees >= K1 <K2 >= K2 < K1 Please Do Not Take Printout : Use RTF Outline in case needed

  6. 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 ........

  7. m-way trees • But you have to search through the m keys in each node! • Reduces your gain from having fewer levels!

  8. m-way trees

  9. 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

  10. BTREE

  11. 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

  12. 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

  13. 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

  14. 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

  15. Insert Node 63

  16. After Insert 63

  17. Insert Node 99

  18. After Insert 99

  19. Split Node 0 4 node

  20. Structure of Btree Avoid Taking Printout : Use RTF Outline in case needed

  21. Split Node : Final 0 3 4 median entry fromNdx 3 node 2 toNdx 2 rightPtr

  22. Split Node : Final 4 3 4 median entry fromNdx 3 node 1 toNdx 2 rightPtr

  23. Traversal

  24. Agenda Delete Delete Walk Through Reflow Borrow Left Borrow Right Combine Delete Mid Avoid Taking Printout : Use RTF Outline in case needed

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. Borrow Left 2 3 1 Node >= 74 < 78 Node >= 78 < 85 Please Do Not Take Printout : Use RTF Outline in case needed

  40. Combine 3 2 1 2 2 Please Do Not Take Printout : Use RTF Outline in case needed

  41. Combine 3 3 1 2 2 Please Do Not Take Printout : Use RTF Outline in case needed

  42. Combine 3 4 2 2 Please Do Not Take Printout : Use RTF Outline in case needed

  43. Combine 2 4 2 2 Please Do Not Take Printout : Use RTF Outline in case needed

  44. 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

  45. 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

  46. 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

  47. order Please Do Not Take Printout : Use RTF Outline in case needed

  48. 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

  49. 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

  50. 2 -3 Tree 1 2 2 2 2 2 Please Do Not Take Printout : Use RTF Outline in case needed

More Related