BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’...

35
BTrees

Transcript of BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’...

Page 1: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

B-­‐Trees  

Page 2: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

Problem:    Data  base  too  big  to  fit  memory                                        Disk  reads  are  slow  

Example:    1,000,000  records  on  disk  

                                     Binary  search  might  take  

                                     20  disk  reads  

Page 3: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

Disk  reads  are  done  in  blocks  

Example:  One  block  read  can  retrieve  

                                   100  records  

Page 4: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

 1,000,000  Records  

Page 5: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

 1,000,000  Records  

….………….  Block  0        Block  1            Block  2          .............      Block  99………………...………...………….        Block  9999  

Page 6: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

 1,000,000  Records  

10,000  Records  

….………….  Block  0        Block  1            Block  2          .............      Block  99………………...………...………….        Block  9999  

Page 7: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

 1,000,000  Records  

10,000  Records  

….………….  Block  0        Block  1            Block  2          .............      Block  99………………...………...………….        Block  9999  

Block  0        Block  1                ...................................                Block  99  

Page 8: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

 1,000,000  Records  

10,000  Records  

Block  0        Block  1                ...................................                Block  99  

….………….  Block  0        Block  1            Block  2          .............      Block  99………………...………...………….        Block  9999  

Block  0  

100  Records  

Page 9: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DEF:  A  B-­‐Tree  of  order  m  is  an  m-­‐way  tree  such  that  

1.  All  leaf  nodes  are  at  the  same  level.  2.  All  non-­‐leaf  nodes  (except  the  root)  have  at  most  m  

and  at  least  m/2  children.  

3.  The  number  of  keys  is  one  less  than  the  number  of  children  for  non-­‐leaf  nodes  and  at  most  m-­‐1  and  at  least  m/2  for  leaf  nodes.  

4.  The  root  may  have  as  few  as  2  children  unless  the  tree  is  the  root  alone.    

Page 10: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

Example  for  m  =  5  

DEF:  A  B-­‐Tree  of  order  5  is  an  5-­‐way  tree  such  that  

1.  All  leaf  nodes  are  at  the  same  level.  2.  All  non-­‐leaf  nodes  (except  the  root)  have  at  most  5  

and  at  least  2  children.  

3.  The  number  of  keys  is  one  less  than  the  number  of  children  for  non-­‐leaf  nodes  and  at  most  4  and  at  least  2  for  leaf  nodes.  

4.  The  root  may  have  as  few  as  2  children  unless  the  tree  is  the  root  alone.    

Page 11: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

Page 12: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

A                  B                    F                  G  

Page 13: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

A                  B                    F                  G              K  

Page 14: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                    

A                  B                                                                          G                  K  

Page 15: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                    

A                  B                  D                                                    G                  H                  K                  M  

Page 16: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                    

A                  B                  D                                                    G                  H                  J                    K                  M  

Page 17: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                J                    

A                  B                  D                                                    G                  H                                        K                  M  

Page 18: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                J                    

A                  B                      D                    E                            G                  H                  I                                            K                    M                R                  S  

Page 19: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                J                    

A                  B                      D                    E                            G                  H                    I                                          K                  M                  R                  S                    X  

Page 20: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                  J                  R                    

A                  B                      D                    E                            G                  H                    I                                          K                  M                                S                    X  

Page 21: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

F                  J                  R                    

A                  B                    C                D                    E                            G                  H                    I                                          K                  M                                S                    X  

Page 22: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

C                F                  J                  R                    

A                  B                                          D                    E                            G                  H                    I                                          K                  M                                S                    X  

Page 23: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

C                F                  J                  R                    

   A        B                              D          E                        G          H        I                        K          L        M        N                                S            T          U          X  

Page 24: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

C                F                  J                  R                    

   A        B                              D          E                        G          H        I                        K          L        M        N        P                      S            T          U          X  

Page 25: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

C                F                  J                  M                R                    

   A        B                              D          E                        G          H        I                        K          L                        N        P                      S            T          U          X  

Page 26: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

CreaTng  a  B-­‐tree  of  order  5  

                   A  G  F  B  K  D  H  M  J  E  S  I  R  X  C  L  N  T  U  P  

C                F                                      M                R                    

   A        B                              D          E                        G          H        I                        K          L                        N        P                      S            T          U          X  

J  

Page 27: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

•  Delete  E  from  leaf  node  

C                F                                      M                R                    

J  

   A        B                              D          E                        G          H        I                        K          L                        N        P                      S            T          U          X  

Page 28: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

•  Delete  E  

C                F                                      M                R                    

J  

   A        B                              D                                    G          H        I                        K          L                        N        P                      S            T          U          X  

Page 29: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

•  Borrow  from  a  neighbor  

C                G                                      M                R                    

J  

   A        B                              D            F                                    H        I                        K          L                        N        P                      S            T          U          X  

Page 30: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

•  Delete  F    -­‐-­‐-­‐    but  can’t  borrow  from  a  neighbor  

C                G                                      M                R                    

J  

   A        B                              D                                                  H        I                        K          L                        N        P                      S            T          U          X  

Page 31: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

Combine  and  push  the  problem  up  one  level  

C                                                        M                R                    

J  

   A        B                              D            G      H        I                                                      K          L                        N        P                      S            T          U          X  

Page 32: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

Can’t  borrow  so  combine  

C                    J                  M                R                    

   A        B                              D            G      H        I                                                      K          L                        N        P                      S            T          U          X  

Page 33: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

Delete  M  from  non-­‐leaf  node  Note:  immediate  predecessor  in  non-­‐leaf  

Is  always  in  a  leaf.  

C                    J                  M                R                    

   A        B                              D            G      H        I                                                      K          L                        N        P                      S            T          U          X  

Page 34: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

Delete  M  from  non-­‐leaf  node  

Overwrite  M  with  immediate  predecessor  

C                    J                  L                R                    

   A        B                              D            G      H        I                                                                  K                        N        P                      S            T          U          X  

Page 35: BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’ DEF:’A’B"Tree’of’order’5’is’an’5"way’tree’such’that 1. All’leaf’nodes’are’atthe’same’level.’

DeleTng  Nodes  

Borrow  from  a  neighbor  

C                    I                  L                R                    

   A        B                              D            G      H                                                              J          K                        N        P                      S            T          U          X