Upgrade to Pro — share decks privately, control downloads, hide ads and more …

これでわかるB-treeアルゴリズム / B-tree algorithm

これでわかるB-treeアルゴリズム / B-tree algorithm

・二分探索木 (binary search tree)
・AVL tree
・B-tree
・B+ tree

について順を追いながら説明。
流れを細かく書いたので、わかりやすいと思います。

ハトネコエ

December 06, 2018
Tweet

More Decks by ハトネコエ

Other Decks in Technology

Transcript

  1. • MySQLͷσʔλϕʔεΤϯδϯ InnoDB Ͱ͸ɺ
 ॱʑʹΞΫηε͢ΔੑೳΛ্͛ͨ B+ tree ͕࢖ΘΕ͍ͯΔ • Oracle

    Database ΍
 MacͷϑΝΠϧγεςϜͩͬͨHFS+ Ͱ͸
 B* tree ͕࢖ΘΕ͍ͯΔʢࠓճ͸ѻ͍·ͤΜʣ B-tree ѥछ
  2. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5
  3. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5 ਌ϊʔυ ࢠϊʔυ
  4. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5 3 2
  5. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5 3 2 8
  6. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5 3 2 8 4
  7. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5 3 2 1 8 4
  8. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ ೋ෼୳ࡧ໦ɿྫ 10 5 3 2 1 8 4 6
  9. • ໦ͷόϥϯε͕ۉ౳ʹ੔͍ͬͯΕ͹ɺ
 ཁૉ਺ n ʹରͯ͠୳ࡧʹ͔͔Δ࠷ѱܭࢉྔ͸ O (log n) ʹͰ͖Δ •

    ͭ·Γɺσʔλ͕૿͑ͯ΋ݕࡧͷ͕࣌ؒ૿͑ͳ͍ ೋ෼୳ࡧ໦ͷར఺ 10 5 2 8 16 12 20 ཁૉ਺ n ܭࢉྔ
  10. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5
  11. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 ʮ10ʯ͔Βݟͯ
 ࠨ͸ߴ͞ 2
 ӈ͸ߴ͞ 0
 ʹͳͬͯ͠·͏ ͦ͜Ͱɺ໦ͷճసʂ
  12. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 த৺ʹ͍ͨ
 ʮ5ʯΛ਌ʹͯ͠
 ଞΛࢠʹ͢Δ
  13. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2
  14. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8
  15. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8 4
  16. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8 4 1
  17. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8 4 1 ͭͳ͛ΒΕΔՕॴͷߴ͞ͷ͕ࠩ2
 ͜Ε͸໦ͷճస͕ى͜Δʁ ? ?
  18. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8 4 1 ʮ5ʯ͔Βݟͯ
 ࠨͷߴ͞ 3
 ӈͷߴ͞ 2 ʮ3ʯ͔Βݟͯ
 ࠨͷߴ͞ 2
 ӈͷߴ͞ 1 ࠨ෦෼໦ͱӈ෦෼໦ͷߴ͞ͷࠩΛ
 ݟΔͷͰɺ·ͩ଱͑·͢
  19. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8 4 1 6 ʮ10ʯ͔Βݟͯ
 ࠨͷߴ͞ 2
 ӈͷߴ͞ 0 ໦ͷճసνϟϯεʂ
  20. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 10 5 3 2 8 4 1 6 த৺ʹ͍Δ
 ʮ8ʯΛ਌ʹ͠ɺ
 ଞ2ͭ͸ࢠʹ
  21. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ AVL໦ɿྫ 8 5 3 2 6 4 1 10 ׬੒ʂ
  22. • ֤ϊʔυʹ࣋ͨͤΔ஋Λ1ͭͰͳ͘ෳ਺ʹ͠ɺ໦ͷߴ͞Λ཈͑Δ • ࠓ·Ͱͷϊʔυ͸஋XΑΓখ͍͔ͦ͞ΕҎ্͔ɺͷ
 2ຊͷࢬ͔͍࣋ͬͯ͠ͳ͔͕ͬͨɺB-treeͰ͸ΑΓଟ͘ͷࢬΛ࣋ͭ • ྫ͑͹ X ͱ Y

    ͷ 2 ͭͷ஋Λϊʔυʹ΋ͭͱܾΊͨ৔߹ɺ
 ஋Xະຬ / ஋XҎ্ͰYະຬ / ஋YҎ্
 ͷ3ຊͷࢬΛϊʔυ͸࠷େͰ࣋ͭʢΦʔμʔ3ʣ ͦ͜Ͱ B-tree
  23. • ϊʔυ͕࠷େͰ࣋ͭࢬͷ਺ʹԠͯ͡ɺ
 ʮΦʔμʔ m ͷB-treeʯͱ͍͏ݴ͍ํΛ͢Δ • Φʔμʔ͸ 3 ʙ 5

    ͕Α͋͘ΔΒ͍͠ • ࠓճ͸ɺΦʔμʔ 3 ͷ B-tree ʢͭ·Γϊʔυʹ2ͭͷ஋Λ࣋ͭʣ
 ʹ͍ͭͯྫΛݟ͍ͯ͘ɻ
 ࢠϊʔυΛ2ʙ3࣋ͭ͜ͱ͔Βɺ2-3 tree ͱ΋ݺ͹ΕΔ Φʔμʔ
  24. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 ϊʔυʹ2ͭͷ
 ஋Λ΋ͭ
  25. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 ϊʔυʹ
 3ͭ໨ͷ஋͕…… 3 த৺ͷ஋͸਌ϊʔυʹҠಈʂ
  26. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 3
  27. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 10 2 3
  28. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10
  29. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10 4
  30. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10 4 த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ
  31. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 5 8 2 3 10 4 Զ͕਌ϊʔυͩʂ த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ
  32. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 8 2 10 4 5
  33. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 8 1 10 4 5 2
  34. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ
  35. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 ͋Ε……͓अຐʁ
  36. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ
  37. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B-treeɿྫ 3 6 1 8 4 5 2 10 ߴ͞Ξοϓʂ ˍ׬੒ʂ
  38. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 10
  39. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 10 ϊʔυʹ
 3ͭ໨ͷ஋͕…… 3 த৺ͷ஋͸਌ϊʔυʹҠಈʂ
  40. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 3 10 ਌ϊʔυ΁Ҡಈͭͭ͠… ΋ͱͷ஋΋࢒͢ʂ
  41. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 3 10 ࠷Լ૚ಉ࢜Λ
 ϙΠϯλͰͭͳ͙ʂ ͜Ε͕ B+ tree ͷ Ұ൪ͷಛ௃ʂ
  42. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 3 10
  43. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 10 3
  44. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ
  45. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 8 ΋ͱͷ஋΋࢒͢
  46. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 8 4 த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ
  47. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 2 8 3 10 8 4 3 ΋ͱͷ஋΋࢒͢
  48. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 3ͭʹͳͬͯ͠·ͬͨ…… 2
  49. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 த৺ͷ஋͸
 ਌ϊʔυʹҠಈʂ 3ͭʹͳͬͯ͠·ͬͨ……
  50. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 3 8 5 Θ͔Γ΍͍͢Α͏
 ্෦෼͚ͩߟ͑Δ ৽͍͠ύύͩΑ ΋ͱͷ஋΋࢒͢
  51. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 3 8 5 Θ͔Γ΍͍͢Α͏
 ্෦෼͚ͩߟ͑Δ 5ະຬ 5Ҏ্
  52. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 ͳͷͰಉ༷ʹ……
  53. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 5 ͜͏ͳΔʂ
  54. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 5 ʢ஫ҙʣ ͜͏͸ͳΒͳ͍ Φʔμʔ 3 ͷ
 B໦Ͱ͸
 ࠷௿ 2 ͭͷ
 ࢠϊʔυΛ࣋ͭ
  55. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 2 5 ࿩Λ໭ͯ࣍͠΁…
  56. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2
  57. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2 6
  58. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2 6 ׬੒ʂ
  59. • 10 → 5 → 3 → 2 → 8

    → 4 → 1 → 6 ͷॱʹೖΕ͍͖ͯ·͢ B+ treeɿྫ 5 5 8 3 10 8 4 3 1 5 2 6 Ϧʔϑϊʔυ