BTrees - Carleton · 2011. 5. 9. · Example’for’m’=5’...
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/1.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/2.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/3.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/4.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/5.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/6.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/7.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/8.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/9.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/10.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/11.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/12.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/13.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/14.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/15.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/16.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/17.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/18.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/19.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/20.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/21.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/22.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/23.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/24.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/25.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/26.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/27.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/28.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/29.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/30.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/31.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/32.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/33.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/34.jpg)
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.’](https://reader035.fdocuments.in/reader035/viewer/2022081411/60aeac6ac261b752462bceaf/html5/thumbnails/35.jpg)
DeleTng Nodes
Borrow from a neighbor
C I L R
A B D G H J K N P S T U X