Download B-Trees - Data Structure and Algorithms - Lecture Notes | CS 245 and more Study notes Data Structures and Algorithms in PDF only on Docsity!
19-0: Indexing
- Operations:
- Add an element
- Remove an element
- Find an element, using a key
- Find all elements in a range of key values
19-1: Indexing
- Sorted List
- Find / Find in Range fast
- Add / Remove slow
- Unsorted List / Hash Table
- Add, Find, Remove fast (hash)
- Find in Range slow
- Binary Search Tree
- All operations are fast (O(lg n))
- if the tree is balanced
19-2: Indexing
- Generalized Binary Search Trees
- Each node can store several keys, instead of just one
- Values in subtrees between values in surrounding keys
- For non leaves, # of children = # of keys + 1
19-3: 2-3 Trees
- Generalized Binary Search Tree
- Each node has 1 or 2 keys
- Each (non-leaf) node has 2-3 children
- hence the name, 2-3 Trees
- All leaves are at the same depth
19-4: Example 2-3 Tree
6 16
19-5: Finding in 2-3 Trees
- How can we find an element in a 2-3 tree?
19-6: 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 appropriate subtree
19-7: Inserting into 2-3 Trees
- Always insert at the leaves
- To insert an element:
- Find the leaf where the element would live, if it was in the tree
- Add the element to that leaf
19-8: Inserting into 2-3 Trees
- Always insert at the leaves
- To insert an element:
- Find the leaf where the element would live, if it was in the tree
- Add the element to that leaf
- What if the leaf already has 2 elements?
19-9: Inserting into 2-3 Trees
- Always insert at the leaves
- To insert an element:
- Find the leaf where the element would live, if it was in the tree
- Add the element to that leaf
- What if the leaf already has 2 elements?
- Split!
19-14: Splitting Root
- When we split the root:
- Create a new root
- Tree grows in height by 1
19-15: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-16: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-17: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
Too many keys,
need to split
19-18: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-19: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-20: 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
19-21: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-22: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-23: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
1
2 4
3 5 6 7
Too many keys need to split
Too many keys
need to split
19-28: 2-3 Tree Example
- Inserting elements 1-9 (in order) into a 2-3 tree
19-29: 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
19-30: Deleting Leaves
19-31: Deleting Leaves
19-32: Deleting Leaves
19-33: Deleting Leaves
- If leaf contains 1 key
- Cannot remove key without making leaf empty
- Try to steal extra key from sibling
19-34: Deleting Leaves
- Deleting 3 – we can steal the 5
19-35: Deleting Leaves
- Not a 2-3 tree. What can we do?
19-36: Deleting Leaves
- Removing the 4
- Combine 5, 7 into one node
19-41: Merging Nodes
19-42: 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
19-43: Merging Nodes
- Deleting the 3 – cause a merge
19-44: Merging Nodes
- Deleting the 3 – cause a merge
- Not enough keys in parent
19-45: Merging Nodes
19-46: Merging Nodes
19-47: Merging Nodes
- When we steal a key from an internal node, steal nearest subtree as well
19-52: Merging Nodes
19-53: Deleting Interior Keys
- How can we delete keys from non-leaf nodes?
- HINT: How did we delete non-leaf nodes in standard BSTs?
19-54: Deleting Interior Keys
- How can we delete keys from non-leaf nodes?
- Replace key with smallest element subtree to right of key
- Recursivly delete smallest element from subtree to right of key
- (can also use largest element in subtree to left of key)
19-55: Deleting Interior Keys
19-56: Deleting Interior Keys
- Deleting the 4
- Replace 4 with smallest element in tree to right of 4
19-57: Deleting Interior Keys
19-58: Deleting Interior Keys
19-59: Deleting Interior Keys
- Deleting the 5
- Replace the 5 with the smallest element in tree to right of 5
19-60: Deleting Interior Keys
- Deleting the 5
- Replace the 5 with the smallest element in tree to right of 5
- Replace the 6 with the smallest element in the tree to the right of the 6
19-65: Deleting Interior Keys
- Node with too few keys
- Can’t steal key from sibling
- Merge with sibling
19-66: Deleting Interior Keys
- Node with too few keys
- Can’t steal key from sibling
- Merge with sibling
- (arbitrarily pick right sibling to merge with)
19-67: Deleting Interior Keys
19-68: 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
19-69: 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
19-70: 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
19-71: 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 the leaf
- Otherwise, split the leaf (which may cause further splits up the tree), and insert the element
19-72: B-Trees
5 11 16 19
1 3 7 8 9 12 15 17 18 22 23
19-73: B-Trees
5 11 16 19
1 3 6 7 8 9 12 15 17 18 22 23
19-74: B-Trees
19-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
19-79: 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?
19-80: B-Trees
- Why do we need to make exceptions for the root?
- Consider a B-Tree of maximum degree 5 with only one element
19-81: B-Trees
- Why do we need to make exceptions for the root?
- Consider a B-Tree of maximum degree 5 with only one element
- Consider a B-Tree of maximum degree 5 with 5 elements
19-82: B-Trees
- Why do we need to make exceptions for the root?
- Consider a B-Tree of maximum degree 5 with only one element
- Consider a B-Tree of maximum degree 5 with 5 elements
- Even when a B-Tree could be created for a specific number of elements, creating an exception for the root allows our split/merge algorithm to work correctly.
19-83: 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
19-84: B-Trees
5 11 16 19
1 3 7 8 9 12 15 17 18 22 23
19-85: B-Trees
5 11 16 19
1 3 7 8 9 12 17 18 22 23
Too few keys
19-86: B-Trees
5 11 16 19
1 3 7 8 9 12 17 18 22 23
19-87: B-Trees
5 9 16 19
1 3 7 8 11 12 17 18 22 23
19-88: B-Trees
5 9 16 19
1 3 7 8 11 12 17 18 22 23
19-89: B-Trees
5 9 16 19
1 3 7 8 12 17 18 22 23
Too few keys