Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

BTree, Data Structures

13,409 views

Published on

Published in: Education
  • Login to see the comments

BTree, Data Structures

  1. 1. Data StructuresB-tree<br />Jibrael Jos : Sep 2009<br />
  2. 2. Introduction<br />Multiway Trees<br />B Tree<br />Application<br />Structure<br />Algo : Insert / Delete<br />Avoid Taking Printout : Use RTF Outline in case needed<br />2<br />Agenda<br />
  3. 3. Data Structures<br />AVL Trees<br />Red Black<br />B-tree<br />Hashing / Indexing Techniques<br />Graphs <br />Please Do Not Take Printout : Use RTF Outline in case needed<br />3<br />
  4. 4. Path Has to be enjoyed<br />Walking<br />Walking in Rain !!<br />Certification<br />Effort ~ Satisfaction<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />4<br />
  5. 5. Research<br />Shoulders of Giants<br />Research on an area to reach a level of expertise<br />Mindmap and Research Path<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />5<br />
  6. 6. B Tree<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />6<br />
  7. 7. Methodology<br />One Book to Another<br />One Link to Another<br />Avoid Taking Printout : Use RTF Outline in case needed<br />7<br />
  8. 8. Binary Search Tree<br />What happens if data is loaded in a binary search tree in this order<br />23, 32, 45, 11, 43 , 41<br />1,2,3,4,5,6,7,8<br />What is AVL tree<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />8<br />
  9. 9. Multiway Trees<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />9<br />&gt;= K2<br />&gt;= K1 &lt;K2<br />&lt; K1<br />
  10. 10. m-way trees<br />Reduce the depth of the tree to O(logmn)with m-way trees<br />mchildren, m-1 keys per node<br />m = 10 : 106 keys in 6 levels vs 20 for a binary tree<br />but ........<br />
  11. 11. m-way trees<br />But you have to search through the m keys in each node!<br />Reduces your gain from having fewer levels!<br />
  12. 12. m-way trees<br />
  13. 13. Anand B<br />B-trees<br />All leaves are on the same level<br />All nodes except for the root and the leaveshave<br />at least m/2 children<br />at most m children<br />Each node is at least<br />half full of keys<br />
  14. 14. BTREE<br />
  15. 15. Disk<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />15<br />1 track = 5000 Chars<br />1 Cylinder = 20 tracks<br />1 disk unit = 200 cylinders<br />
  16. 16. Time Taken<br />Seek Time<br />Latency Time<br />Transmission Time<br />Overcoming Latency Time ??<br />72.5 + o.o5n millisec to read n chars<br />
  17. 17. 3 level<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />17<br />
  18. 18. Multiway Tree<br />M – ary tree<br />3 levels : <br />Cylinder , Track , Record : Index Seq (RDBMS)<br />Tables with less change<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />18<br />
  19. 19. BTree<br />If level is 3, m =199 then what is N<br />How many split per insertion ?<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />19<br />
  20. 20. Multiway Trees : Application<br />NDPL , Delhi: Electricity Billing<br />3 lakh consumers <br />Table indexed as BTREE<br />UCO Bank, Jaipur<br />One DD takes 10 minutes to print<br />Saviour : BTREE<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />20<br />
  21. 21. B-trees - Insertion<br />Insertion<br />B-tree property : block is at least half-full of keys<br />Insertion into block with m keys<br />block overflows<br />split block<br />promote one key<br />split parent if necessary<br />if root is split, tree becomes one level deeper<br />
  22. 22. Insert Node<br />63<br />
  23. 23. After Insert 63<br />
  24. 24. Insert Node<br />99<br />
  25. 25. After Insert 99<br />
  26. 26. Split Node<br />0<br />4<br />node<br />
  27. 27. Structure of Btree<br />node<br />firstPtr<br />numEntries<br /> Entries[1.. M-1]<br />End <br />Entry <br /> key<br />rightPtr<br />End Entry<br />Avoid Taking Printout : Use RTF Outline in case needed<br />27<br />
  28. 28. Split Node : Final<br />0<br />4<br />3<br />median<br />entry<br />fromNdx<br />3<br />node<br />2<br />toNdx<br />2<br />rightPtr<br />
  29. 29. Split Node : Final<br />4<br />4<br />3<br />median<br />entry<br />fromNdx<br />3<br />node<br />1<br />toNdx<br />2<br />rightPtr<br />
  30. 30. Traversal<br />
  31. 31. Delete<br />Delete Walk Through<br />Reflow<br />Borrow Left<br />Borrow Right<br />Combine<br />Delete Mid<br />Avoid Taking Printout : Use RTF Outline in case needed<br />31<br />Agenda<br />
  32. 32. Delete : For 78<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />32<br />Btree Delete<br /> Delete()<br /> Delete()<br /> Delete Mid()<br /> Reflow()<br /> Reflow()<br /> If shorter delete root<br />1<br />2<br />2<br />2<br />2<br />2<br />
  33. 33. Btree Delete<br />If (root null)<br /> print (“Attempt to delete from null tree”)<br />Else<br /> shorter = delete (root, target)<br /> if Shorter<br /> delete root <br />Return root<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />33<br />B<br />Target = 78<br />1<br />2<br />2<br />2<br />2<br />2<br />
  34. 34. Delete(root , deleteKey)<br />If (root null)<br /> data does not exist<br />Else<br />entryNdx= searchNode(root, deleteKey)<br /> if found entry to be deleted <br /> if leaf node<br /> underflow=deleteEntry()<br /> else<br /> underflow=deleteMid (left)<br /> if underflow<br /> underflow=reflow()<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />34<br />B<br />D<br />Target = 78<br />1<br />2<br />2<br />2<br />2<br />2<br />
  35. 35. Delete Else Part<br />Else<br /> if deleteKey less than first entry<br />subtree=firstPtr<br /> else<br />subtree=rightPtr<br /> underflow= delete (subtree,deleteKey)<br /> if underflow<br /> underflow= reflow()<br />Return underflow<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />35<br />B<br />D<br />Target = 78<br />1<br />2<br />2<br />2<br />2<br />2<br />
  36. 36. Delete(root , deleteKey)<br />If (root null)<br /> data does not exist<br />Else<br />entryNdx= searchNode(root, deleteKey)<br /> if found entry to be deleted <br /> if leaf node<br /> underflow=deleteEntry()<br /> else<br /> underflow=deleteMid (root,entryIndx,left)<br /> if underflow<br /> underflow=reflow(root,entryIndx)<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />36<br />B<br />D<br />D<br />Target = 78<br />DM<br />1<br />2<br />2<br />2<br />2<br />2<br />
  37. 37. Delete(root , deleteKey)<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />37<br />B<br />If (root null)<br /> data does not exist<br />Else<br />entryNdx= searchNode(root, deleteKey)<br /> if found entry to be deleted <br /> if leaf node<br /> underflow=deleteEntry()<br /> else<br /> underflow=deleteMid (root,entryIndx,left)<br /> if underflow<br /> underflow=reflow(root,entryIndx)<br />D<br />D<br />74 replaces 78<br />1<br />2<br />2<br />2<br />1<br />2<br />
  38. 38. Delete(root , deleteKey)<br />If (root null)<br /> data does not exist<br />Else<br />entryNdx= searchNode(root, deleteKey)<br /> if found entry to be deleted <br /> if leaf node<br /> underflow=deleteEntry()<br /> else<br /> underflow=deleteMid (root,entryIndx,left)<br /> if underflow<br /> underflow=reflow(root,entryIndx)<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />38<br />B<br />D<br />D<br />After Reflow<br />1<br />1<br />2<br />2<br />4<br />
  39. 39. Delete Else Part<br />Else<br /> if deleteKey less than first entry<br />subtree=firstPtr<br /> else<br />subtree=rightPtr<br /> underflow= delete (subtree,deleteKey)<br /> if underflow<br /> underflow= reflow(root,entryIndx)<br />Return underflow<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />39<br />B<br />D<br />Before Reflow<br />1<br />1<br />2<br />2<br />4<br />
  40. 40. Delete Else Part<br />Else<br /> if deleteKey less than first entry<br />subtree=firstPtr<br /> else<br />subtree=rightPtr<br /> underflow= delete (subtree,deleteKey)<br /> if underflow<br /> underflow= reflow(root,entryIndx)<br />Return underflow<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />40<br />B<br />D<br />After Reflow<br />0<br />4<br />2<br />4<br />
  41. 41. BTREE Delete<br />If (root null)<br /> print (“Attempt to delete from null tree”)<br />Else<br /> shorter = delete (root, target)<br /> if Shorter<br /> delete root <br />Return root<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />41<br />B<br />0<br />4<br />2<br />4<br />
  42. 42. BTREE Delete<br />If (root null)<br /> print (“Attempt to delete from null tree”)<br />Else<br /> shorter = delete (root, target)<br /> if Shorter<br /> delete root <br />Return root<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />42<br />B<br />4<br />2<br />4<br />
  43. 43. Templates<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />43<br />3<br />4<br />2<br />1<br />
  44. 44. Delete<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />44<br />1<br />2<br />2<br />2<br />2<br />2<br />
  45. 45. Delete : For 78<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />45<br />Btree Delete<br /> Delete()<br /> Delete()<br /> Delete Mid()<br /> Reflow()<br /> Reflow()<br /> If shorter delete root<br />1<br />2<br />2<br />2<br />2<br />2<br />
  46. 46. Delete : Reflow<br />1: Try to borrow right. <br />2: If 1 failed try to borrow from left<br />3: Cannot Borrow (1,2 failed) Combine<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />46<br />
  47. 47. Delete Reflow<br />Underflow=false<br />If RT-&gt;no &gt; min Entries<br />BorrowRight (root,entryNdx,LT,RT)<br />Else<br /> If LT-&gt;no &gt; min Entries<br />BorrowLeft (root,entryNdx,LT,RT)<br />Else<br /> combine (root,entryNdx,LT,RT)<br /> if root-&gt;no &lt; min entries<br /> underflow=True<br />Return underflow<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />47<br />
  48. 48. Borrow Left<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />48<br />2<br />1<br />3<br />Node &gt;= 74 <br />&lt; 78<br />Node &gt;= 78 <br />&lt; 85<br />
  49. 49. Combine<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />49<br />3<br />1<br />2<br />2<br />2<br />
  50. 50. Combine<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />50<br />3<br />1<br />3<br />2<br />2<br />
  51. 51. Combine<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />51<br />3<br />4<br />2<br />2<br />
  52. 52. Combine<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />52<br />2<br />4<br />2<br />2<br />
  53. 53. Delete Mid<br />If leaf<br /> exchange data and delete leaf entry<br />Else<br /> traverse right to locate predecessor<br />deleteMid(right)<br /> if underflow<br /> reflow<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />53<br />
  54. 54. Delete Mid<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />54<br />1<br />2<br />2<br />2<br />2<br />2<br />Case 1: To Delete 78 we replace with 74<br />
  55. 55. Delete Mid<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />55<br />1<br />2<br />2<br />2<br />2<br />2<br />Case 2:<br />To Delete 78 we replace with 76<br />Hence recursive call of Delete Mid to locate predecessor<br />2<br />
  56. 56. order<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />56<br />
  57. 57. Get the Order Right <br />Keys are 4<br />Subtrees Max is 5 = Order is 5<br />Minimum = 3 (which is subtrees)<br />Min Keys is 2<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />57<br />4<br />2<br />4<br />
  58. 58. 2-3 Tree<br />Order 3 ….. So how many keys in a node<br />This rule is valid for non root leaf<br />Root can have 0, 2, 3 subtrees<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />58<br />
  59. 59. 2 -3 Tree<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />59<br />1<br />2<br />2<br />2<br />2<br />2<br />
  60. 60. 2-3-4 Tree<br />Order 4 ….. So how many keys in a node<br />This rule is valid for non root leaf<br />Root can have 0, 2, 3 subtrees<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />60<br />
  61. 61. Structure of B + tree<br />Non leaf node<br />firstPtr<br />numEntries<br /> Entries[1.. M-1]<br />End <br />Entry <br /> key<br />rightPtr<br />End Entry<br />Avoid Taking Printout : Use RTF Outline in case needed<br />61<br /><ul><li>Leaf node
  62. 62. firstPtr
  63. 63. numEntries
  64. 64. Entries[1.. M-1]
  65. 65. Next Leaf Node
  66. 66. End </li></li></ul><li>B + Tree<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />62<br />1<br />2<br />2<br />2<br />2<br />Implies there are more nodes<br />
  67. 67. B * Tree<br />Space Usage<br />BTREE nodes can be 50% Empty (1/2)<br />So rule modified to two third (2/3)<br />Also when node overflows instead of being split immed distributed with siblings<br />And even when split happens all siblings are equally distributed (pg 462)<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />63<br />
  68. 68. B+-trees<br />B+ trees<br />All the keys in the nodes are dummies<br />Only the keys in the leaves point to “real” data<br />Linking the leaves <br />Ability to scan the collection in orderwithout passing through the higher nodes<br />
  69. 69. Reference<br />My Course<br />Furzon<br />Chapter 10<br />Volume 3 Knuth : 5.4.9 (Disks ) 6.2.4 (Multiway)<br />Please Do Not Take Printout : Use RTF Outline in case needed<br />65<br />Action Item<br /><ul><li>Do research on BTREE , AVL , Red Black</li>

×