Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22...
Transcript of Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22...
![Page 1: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/1.jpg)
Graduate AlgorithmsCS673-2016F-11
B-Trees
David Galles
Department of Computer Science
University of San Francisco
![Page 2: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/2.jpg)
11-0: Binary Search Trees
Binary Tree data structure
All values in left subtree < value stored in root
All values in the right subtree > value stored in root
![Page 3: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/3.jpg)
11-1: Generalizing BSTs
Generalized Binary Search Trees
Each node can store several keys, instead ofjust one
Values in subtrees between values insurrounding keys
For non leaves, # of children = # of keys + 1
2 6
1 3 4 7
![Page 4: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/4.jpg)
11-2: 2-3 Trees
Generalized Binary Search Tree
Each node has 1 or 2 keys
Each (non-leaf) node has 2-3 childrenhence the name, 2-3 Trees
All leaves are at the same depth
![Page 5: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/5.jpg)
11-3: Example 2-3 Tree
6 16
3 8 13 18
7 11 12 14 17 202 5
![Page 6: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/6.jpg)
11-4: Finding in 2-3 Trees
How can we find an element in a 2-3 tree?
![Page 7: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/7.jpg)
11-5: Finding in 2-3 Trees
How can we find an element in a 2-3 tree?
If the tree is empty, return false
If the element is stored at the root, return true
Otherwise, recursively find in the appropriatesubtree
![Page 8: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/8.jpg)
11-6: Inserting into 2-3 Trees
Always insert at the leaves
To insert an element:
Find the leaf where the element would live, if itwas in the tree
Add the element to that leaf
![Page 9: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/9.jpg)
11-7: Inserting into 2-3 Trees
Always insert at the leaves
To insert an element:
Find the leaf where the element would live, if itwas in the tree
Add the element to that leafWhat if the leaf already has 2 elements?
![Page 10: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/10.jpg)
11-8: Inserting into 2-3 Trees
Always insert at the leaves
To insert an element:
Find the leaf where the element would live, if itwas in the tree
Add the element to that leafWhat if the leaf already has 2 elements?Split!
![Page 11: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/11.jpg)
11-9: Splitting Nodes
5 6 7
75
6
![Page 12: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/12.jpg)
11-10: Splitting Nodes
4
1 2 5 6 7Too manyelements
![Page 13: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/13.jpg)
11-11: Splitting Nodes
4
1 2 5 6 7
Promote to parent
Left childof 6
Right childof 6
![Page 14: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/14.jpg)
11-12: Splitting Nodes
4 6
1 2 5 7
![Page 15: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/15.jpg)
11-13: Splitting Root
When we split the root:
Create a new root
Tree grows in height by 1
![Page 16: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/16.jpg)
11-14: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
![Page 17: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/17.jpg)
11-15: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1 2
![Page 18: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/18.jpg)
11-16: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1 2 3
Too many keys,need to split
![Page 19: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/19.jpg)
11-17: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2
3
![Page 20: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/20.jpg)
11-18: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2
3 4
![Page 21: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/21.jpg)
11-19: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2
3 4 5
Too many keys,need to split
![Page 22: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/22.jpg)
11-20: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2 4
3 5
![Page 23: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/23.jpg)
11-21: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2 4
3 5 6
![Page 24: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/24.jpg)
11-22: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2 4
3 5 6 7
Too many keysneed to split
![Page 25: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/25.jpg)
11-23: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1
2 4 6
3 5 7
Too many keysneed to split
![Page 26: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/26.jpg)
11-24: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1 3 5 7
4
2 6
![Page 27: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/27.jpg)
11-25: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1 3 5 7 8
4
2 6
![Page 28: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/28.jpg)
11-26: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1 3 5 7 8 9
4
2 6
Too many keysneed to split
![Page 29: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/29.jpg)
11-27: 2-3 Tree Example
Inserting elements 1-9 (in order) into a 2-3 tree
1 3 5
4
2 6 8
7 9
![Page 30: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/30.jpg)
11-28: Deleting from 2-3 Tree
As with BSTs, we will have 2 cases:
Deleting a key from a leaf
Deleting a key from an internal node
![Page 31: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/31.jpg)
11-29: Deleting Leaves
If leaf contains 2 keys
Can safely remove a key
![Page 32: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/32.jpg)
11-30: Deleting Leaves
4 8
3 5 7 11
Deleting 7
![Page 33: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/33.jpg)
11-31: Deleting Leaves
4 8
3 5 11
Deleting 7
![Page 34: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/34.jpg)
11-32: Deleting Leaves
If leaf contains 1 key
Cannot remove key without making leaf empty
Try to steal extra key from sibling
![Page 35: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/35.jpg)
11-33: Deleting Leaves
4 8
3 5 7 11
Deleting 3 – we can steal the 5
![Page 36: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/36.jpg)
11-34: Deleting Leaves
4 8
5 7 11
Not a 2-3 tree. What can we do?
![Page 37: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/37.jpg)
11-35: Deleting Leaves
4 8
5 7 11
Steal key from sibling through parent
![Page 38: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/38.jpg)
11-36: Deleting Leaves
5 8
7 114
Steal key from sibling through parent
![Page 39: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/39.jpg)
11-37: Deleting Leaves
If leaf contains 1 key, and no sibling contains extrakeys
Cannot remove key without making leaf empty
Cannot steal a key from a sibling
Merge with siblingsplit in reverse
![Page 40: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/40.jpg)
11-38: Merging Nodes
5 8
7 114
Removing the 4
![Page 41: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/41.jpg)
11-39: Merging Nodes
5 8
7 11
Removing the 4
Combine 5, 7 into one node
![Page 42: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/42.jpg)
11-40: Merging Nodes
8
5 7 11
![Page 43: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/43.jpg)
11-41: Merging Nodes
Merge decreases the number of keys in the parent
May cause parent to have too few keys
Parent can steal a key, or merge again
![Page 44: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/44.jpg)
11-42: Merging Nodes
1 3 5
4
2 6 8
7 9
Deleting the 3 – cause a merge
![Page 45: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/45.jpg)
11-43: Merging Nodes
1 2 5
4
6 8
7 9
Deleting the 3 – cause a merge
Not enough keys in parent
![Page 46: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/46.jpg)
11-44: Merging Nodes
1 2 5
4
6 8
7 9
Steal key from sibling
![Page 47: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/47.jpg)
11-45: Merging Nodes
1 2 5
6
8
7 9
4
Steal key from sibling
![Page 48: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/48.jpg)
11-46: Merging Nodes
1 2 5
6
8
7 9
4
When we steal a key from an internal node, stealnearest subtree as well
![Page 49: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/49.jpg)
11-47: Merging Nodes
1 2 5
6
8
7 9
4
When we steal a key from an internal node, stealnearest subtree as well
![Page 50: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/50.jpg)
11-48: Merging Nodes
1 2 5
6
8
7 9
4
Deleting the 7 – cause a merge
![Page 51: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/51.jpg)
11-49: Merging Nodes
1 2 5
6
8 9
4
Parent has too few keys – merge again
![Page 52: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/52.jpg)
11-50: Merging Nodes
1 2 5 8 9
4 6
Root has no keys – delete
![Page 53: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/53.jpg)
11-51: Merging Nodes
1 2 5 8 9
4 6
![Page 54: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/54.jpg)
11-52: Deleting Interior Keys
How can we delete keys from non-leaf nodes?
HINT: How did we delete non-leaf nodes instandard BSTs?
![Page 55: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/55.jpg)
11-53: Deleting Interior Keys
How can we delete keys from non-leaf nodes?
Replace key with smallest element subtree toright of key
Recursivly delete smallest element fromsubtree to right of key
(can also use largest element in subtree to left ofkey)
![Page 56: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/56.jpg)
11-54: Deleting Interior Keys
1 3 5 6 8 9
4
2 7
Deleting the 4
![Page 57: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/57.jpg)
11-55: Deleting Interior Keys
1 3 5 6 8 9
4
2 7
Deleting the 4
Replace 4 with smallest element in tree to right of 4
![Page 58: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/58.jpg)
11-56: Deleting Interior Keys
1 3 6 8 9
5
2 7
![Page 59: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/59.jpg)
11-57: Deleting Interior Keys
1 3 6 8 9
5
2 7
Deleting the 5
![Page 60: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/60.jpg)
11-58: Deleting Interior Keys
1 3 6 8 9
5
2 7
Deleting the 5
Replace the 5 with the smallest element in tree toright of 5
![Page 61: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/61.jpg)
11-59: Deleting Interior Keys
1 3 8 9
6
2 7
Deleting the 5
Replace the 5 with the smallest element in tree toright of 5
Node with two few keys
![Page 62: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/62.jpg)
11-60: Deleting Interior Keys
1 3 8 9
6
2 7
Node with two few keys
Steal a key from a sibling
![Page 63: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/63.jpg)
11-61: Deleting Interior Keys
1 3 9
6
2 8
7
![Page 64: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/64.jpg)
11-62: Deleting Interior Keys
1 3 9
6 10
2 8
7 13
11
12
Removing the 6
![Page 65: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/65.jpg)
11-63: Deleting Interior Keys
1 3 9
6 10
2 8
7 13
11
12
Removing the 6
Replace the 6 with the smallest element in the treeto the right of the 6
![Page 66: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/66.jpg)
11-64: Deleting Interior Keys
1 3 9
7 10
2 8
13
11
12
Node with too few keys
Can’t steal key from sibling
Merge with sibling
![Page 67: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/67.jpg)
11-65: Deleting Interior Keys
1 3 8 9
7 10
2
13
11
12
Node with too few keys
Can’t steal key from sibling
Merge with sibling
(arbitrarily pick right sibling to merge with)
![Page 68: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/68.jpg)
11-66: Deleting Interior Keys
1 3 8 9
7
2
13
10 11
12
![Page 69: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/69.jpg)
11-67: Generalizing 2-3 Trees
In 2-3 Trees:
Each node has 1 or 2 keys
Each interior node has 2 or 3 children
We can generalize 2-3 trees to allow more keys /node
![Page 70: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/70.jpg)
11-68: B-Trees
A B-Tree of maximum degree k:
All interior nodes have ⌈k/2⌉ . . . k children
All nodes have ⌈k/2⌉ − 1 . . . k − 1 keys
2-3 Tree is a B-Tree of maximum degree 3
![Page 71: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/71.jpg)
11-69: B-Trees
5 11 16 19
1 3 7 8 9 12 15 17 18 22 23
B-Tree with maximum degree 5
Interior nodes have 3 – 5 children
All nodes have 2-4 keys
![Page 72: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/72.jpg)
11-70: B-Trees
Inserting into a B-Tree
Find the leaf where the element would go
If the leaf is not full, insert the element into theleaf
Otherwise, split the leaf (which may causefurther splits up the tree), and insert theelement
![Page 73: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/73.jpg)
11-71: B-Trees
5 11 16 19
1 3 7 8 9 12 15 17 18 22 23
Inserting a 6 ..
![Page 74: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/74.jpg)
11-72: B-Trees
5 11 16 19
1 3 6 7 8 9 12 15 17 18 22 23
![Page 75: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/75.jpg)
11-73: B-Trees
5 11 16 19
1 3 6 7 8 9 12 15 17 18 22 23
Inserting a 10 ..
![Page 76: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/76.jpg)
11-74: B-Trees
5 11 16 19
1 3 6 7 8 9 10 12 15 17 18 22 23
Too many keysneed to split
Promote 8 to parent (between 5 and 11)
Make nodes out of (6, 7) and (9, 10)
![Page 77: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/77.jpg)
11-75: B-Trees
5 8 11 16 19
1 3 9 10 12 15 17 18 22 236 7
Too many keysneed to split
Promote 11 to parent (new root)
Make nodes out of (5, 8) and (6, 19)
![Page 78: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/78.jpg)
11-76: B-Trees
16 19
1 3 9 10 12 15 17 18 22 236 7
5 8
11
Note that the root only has 1 key, 2 children
All nodes in B-Trees with maximum degree 5should have at least 2 keys
The root is an exception – it may have as few asone key and two children for any maximum degree
![Page 79: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/79.jpg)
11-77: B-Trees
B-Tree of maximum degree k
Generalized BST
All leaves are at the same depth
All nodes (other than the root) have⌈k/2⌉ − 1 . . . k − 1 keys
All interior nodes (other than the root) have⌈k/2⌉ . . . k children
![Page 80: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/80.jpg)
11-78: B-Trees
B-Tree of maximum degree k
Generalized BST
All leaves are at the same depth
All nodes (other than the root) have⌈k/2⌉ − 1 . . . k − 1 keys
All interior nodes (other than the root) have⌈k/2⌉ . . . k children
Why do we need to make exceptions for the root?
![Page 81: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/81.jpg)
11-79: B-Trees
Why do we need to make exceptions for the root?
Consider a B-Tree of maximum degree 5 withonly one element
![Page 82: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/82.jpg)
11-80: B-Trees
Why do we need to make exceptions for the root?
Consider a B-Tree of maximum degree 5 withonly one element
Consider a B-Tree of maximum degree 5 with 5elements
![Page 83: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/83.jpg)
11-81: B-Trees
Why do we need to make exceptions for the root?
Consider a B-Tree of maximum degree 5 withonly one element
Consider a B-Tree of maximum degree 5 with 5elements
Even when a B-Tree could be created for aspecific number of elements, creating anexception for the root allows our split/mergealgorithm to work correctly.
![Page 84: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/84.jpg)
11-82: B-Trees
Deleting from a B-Tree (Key is in a leaf)
Remove key from leaf
Steal / Split as necessary
May need to split up tree as far as root
![Page 85: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/85.jpg)
11-83: B-Trees
5 11 16 19
1 3 7 8 9 12 15 17 18 22 23
Deleting the 15
![Page 86: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/86.jpg)
11-84: B-Trees
5 11 16 19
1 3 7 8 9 12 17 18 22 23
Too few keys
![Page 87: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/87.jpg)
11-85: B-Trees
5 11 16 19
1 3 7 8 9 12 17 18 22 23
Steal a key from sibling
![Page 88: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/88.jpg)
11-86: B-Trees
5 9 16 19
1 3 7 8 11 12 17 18 22 23
![Page 89: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/89.jpg)
11-87: B-Trees
5 9 16 19
1 3 7 8 11 12 17 18 22 23
Delete the 11
![Page 90: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/90.jpg)
11-88: B-Trees
5 9 16 19
1 3 7 8 12 17 18 22 23
Too few keys
![Page 91: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/91.jpg)
11-89: B-Trees
5 9 16 19
1 3 7 8 12 17 18 22 23
Combine into 1 node
Merge with a sibling (pick the left sibling arbitrarily)
![Page 92: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/92.jpg)
11-90: B-Trees
5 16 19
1 3 7 8 9 12 17 18 22 23
![Page 93: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/93.jpg)
11-91: B-Trees
Deleting from a B-Tree (Key in internal node)
Replace key with largest key in right subtree
Remove largest key from right subtree
(May force steal / merge)
![Page 94: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/94.jpg)
11-92: B-Trees
5 16 19
1 3 7 8 9 12 17 18 22 23
Remove the 5
![Page 95: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/95.jpg)
11-93: B-Trees
5 16 19
1 3 7 8 9 12 17 18 22 23
Remove the 5
![Page 96: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/96.jpg)
11-94: B-Trees
7 16 19
1 3 8 9 12 17 18 22 23
![Page 97: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/97.jpg)
11-95: B-Trees
7 16 19
1 3 8 9 12 17 18 22 23
Remove the 19
![Page 98: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/98.jpg)
11-96: B-Trees
7 16 19
1 3 8 9 12 17 18 22 23
Remove the 19
![Page 99: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/99.jpg)
11-97: B-Trees
7 16 22
1 3 8 9 12 17 18 23
Too few keys
![Page 100: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/100.jpg)
11-98: B-Trees
7 16 22
1 3 8 9 12 17 18 23
Merge with left sibling
![Page 101: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/101.jpg)
11-99: B-Trees
7 16
1 3 8 9 12 17 18 22 23
![Page 102: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/102.jpg)
11-100: B-Trees
Almost all databases that are large enough torequire storage on disk use B-Trees
Disk accesses are very slow
Accessing a byte from disk is 10,000 – 100,000times as slow as accessing from main memory
Recently, this gap has been getting even bigger
Compared to disk accesses, all other operationsare essentially free
Most efficient algorithm minimizes disk accessesas much as possible
![Page 103: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/103.jpg)
11-101: B-Trees
Disk accesses are slow – want to minimize them
Single disk read will read an entire sector of thedisk
Pick a maximum degree k such that a node of theB-Tree takes up exactly one disk block
Typically on the order of 100 children / node
![Page 104: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/104.jpg)
11-102: B-Trees
With a maximum degree around 100, B-Trees arevery shallow
Very few disk reads are required to access anypiece of data
Can improve matters even more by keeping thefirst few levels of the tree in main memory
For large databases, we can’t store the entiretree in main memory – but we can limit thenumber of disk accesses for each operation tobe very small
![Page 105: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/105.jpg)
11-103: B-Trees
If the maximum degree of a B-Tree is odd (2-3 tree,3-4-5 tree), then we can only split a node when itgets “over-full”
Examples for 2-3 trees on board
If the maximum degree of a B-Tree is even (2-3-4tree, 3-4-5-6, etc.):
We can split a node before it is “over-full”
We can merge nodes before they are“under-full”
![Page 106: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/106.jpg)
11-104: B-Trees
Preemptive Splitting
If the maximum degree is even, we canimplement an insert with a single pass downthe tree (instead of a pass down, and then apass up to clean up)
When inserting into any subtree tree, if the rootof that tree is full, split the root before inserting
Every time we want to do a split, we knowour parent is not full.
(examples, use visualization)
![Page 107: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/107.jpg)
11-105: B-Trees
Preemptive Combining – Deleting from Leaves
If the maximum degree is even, we canimplement a delete with a single pass down thetree (instead of a pass down, and then a passup to clean up)
When deleting from any node (other than theroot), combine / steal as necessary so that thenode has more then the minimum # of keys
When you get to a leaf, you are guaranteed thatthere will be an extra key in the leaf
(examples, deleting from leaves)
![Page 108: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/108.jpg)
11-106: B-Trees
Preemptive Combining
Deleting k from a non-leaf:If the subtree left of k has > minimumnumber of elements, replace k with largestelement in the left subtree, splitting as you godownIf the subtree right of k has > minimumnumber of elements, replace k with smallestelement in the right subtree, splitting as yougo down
(examples)
![Page 109: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/109.jpg)
11-107: B-Trees
Deleting 5:
5 8
7 113 4
![Page 110: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/110.jpg)
11-108: B-Trees
Deleting 5:
5 8
7 113 4
Replace 5 withlargest elememntin left subtree
![Page 111: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/111.jpg)
11-109: B-Trees
Deleting 5:
5 20
8 15 303
![Page 112: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/112.jpg)
11-110: B-Trees
Deleting 5:
5 20
8 15 303
Replace 5 withsmallest elememntin right subtree
![Page 113: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/113.jpg)
11-111: B-Trees
Preemptive Combining
Deleting k from a non-leaf:If the subtrees to the left & right of k subtreesboth have the minimum # of elements,combine around kRecursively remove k from this new node
![Page 114: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/114.jpg)
11-112: B-Trees
Deleting 5:
5 8
7 114
![Page 115: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/115.jpg)
11-113: B-Trees
Merge around 5:
5 8
7 114
![Page 116: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/116.jpg)
11-114: B-Trees
Delete 5 from new node:
8
114 5 7
![Page 117: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/117.jpg)
11-115: B-Trees
Preemptive Combining
Deleting k from a non-leaf:If the subtrees to the left & right of k subtreesboth have the minimum # of elements,combine around kRecursively remove k from this new node
Why do we need this case? Why can’t we justreplace key with largest value in left subtree, orsmallest value in right subtree?
![Page 118: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/118.jpg)
11-116: B-Trees
Preemptive Combining
Deleting k from a non-leaf:If the subtrees to the left & right of k subtreesboth have the minimum # of elements,combine around kRecursively remove k from this new node
Why do we need this case? Why can’t we justreplace key with largest value in left subtree?
Immediately cause a merge, anyway
Harder to determine which location to copylargest element into
![Page 119: Graduate Algorithmsgalles/cs673/lecture/lecture11.pdf11-76: B-Trees 16 19 1 3 9 10 12 15 17 186 7 22 23 5 8 11 Note that the root only has 1 key, 2 children All nodes in B-Trees with](https://reader034.fdocuments.us/reader034/viewer/2022042106/5e85d9f6502f504e10208b65/html5/thumbnails/119.jpg)
11-117: B-Trees
Preemptive split/merge vs. “standard” split/merge
Advantages of the “standard” method?
Advantages of the “preemptive” method?
Textbook uses “preemptive” method
Defines “minimum degree k” (with maximumdegree = 2k) instead of “maximum degree k”
(with minimum degree =⌈
k
2
⌉
)