cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time...
Transcript of cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time...
![Page 1: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/1.jpg)
CS 225Data Structures
March 11 – BTreesWade Fagen-Ulmschneider, Craig Zilles
![Page 2: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/2.jpg)
B-Tree MotivationBig-O assumes uniform time for all operations, but this isn’t always true.
However, seeking data from disk may take 40ms+.…an O(lg(n)) AVL tree no longer looks great:
5
3 6
4
2
8
10
9 12
111 7
![Page 3: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/3.jpg)
BTree (of order m)
Goal: Minimize the number of reads!Build a tree that uses ______________________ / node
[1 network packet][1 disk block]
-3 8 23 25 31 42 43 55m=9
![Page 4: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/4.jpg)
BTree InsertionA BTrees of order m is an m-way tree:- All keys within a node are ordered- All nodes hold no more than m-1 keys.
m=5
![Page 5: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/5.jpg)
BTree InsertionWhen a BTree node reaches m keys:
m=5
![Page 6: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/6.jpg)
BTree Recursive Insert
-3 8
23
25 31
42
43 55
m=3
![Page 7: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/7.jpg)
BTree Recursive Insert
-3 8
23
25 31
42
43 55
m=3
![Page 8: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/8.jpg)
BTree Visualization/Toolhttps://www.cs.usfca.edu/~galles/visualization/BTree.html
![Page 9: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/9.jpg)
Btree PropertiesA BTrees of order m is an m-way tree:- All keys within a node are ordered- All leaves contain hold no more than m-1 keys.
- All internal nodes have exactly one more child than key- Root nodes can be a leaf or have [2, m] children.- All non-root, internal nodes have [ceil(m/2), m] children.
- All leaves are on the same level
![Page 10: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/10.jpg)
BTree
3
17
16
28 488
1 2 6 7 25 26 29 4512 14 52 53 55 68
![Page 11: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/11.jpg)
BTree Search
-3
8
23
25 31
42
43
55
-11 60
![Page 12: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/12.jpg)
BTree Searchbool Btree::_exists(BTreeNode & node, const K & key) {
unsigned i;for ( i = 0; i < node.keys_ct_ && key < node.keys_[i]; i++) { }
if ( i < node.keys_ct_ && key == node.keys_[i] ) {return true;
}
if ( node.isLeaf() ) {return false;
} else {BTreeNode nextChild = node._fetchChild(i);return _exists(nextChild, key);
} }
123456789
10111213141516
-3
8
23
25 31
42
43
55
-11 60
![Page 13: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/13.jpg)
BTree AnalysisThe height of the BTree determines maximum number of ____________ possible in search data.
…and the height of the structure is: ______________.
Therefore: The number of seeks is no more than __________.
…suppose we want to prove this!
![Page 14: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/14.jpg)
BTree AnalysisIn our AVL Analysis, we saw finding an upper bound on the height (given n) is the same as finding a lower bound on the nodes (given h).
We want to find a relationship for BTrees between the number of keys (n) and the height (h).
![Page 15: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/15.jpg)
BTree AnalysisStrategy:We will first count the number of nodes, level by level.
Then, we will add the minimum number of keys per node (n).
The minimum number of nodes will tell us the largest possible height (h), allowing us to find an upper-bound on height.
![Page 16: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/16.jpg)
BTree AnalysisThe minimum number of nodes for a BTree of order m at each level:
root:
level 1:
level 2:
level 3:…level h:
![Page 17: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/17.jpg)
BTree AnalysisThe total number of nodes is the sum of all of the levels:
![Page 18: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/18.jpg)
BTree AnalysisThe total number of keys:
![Page 19: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/19.jpg)
BTree AnalysisThe smallest total number of keys is:
So an inequality about n, the total number of keys:
Solving for h, since h is the number of seek operations:
![Page 20: cs225sp19-23-BTree-slides - University Of Illinois...B-Tree Motivation Big-O assumes uniform time for all operations, but this isn’t always true. However, seeking data from diskmay](https://reader030.fdocuments.us/reader030/viewer/2022040911/5e85d953bed4ec3ae663f4af/html5/thumbnails/20.jpg)
BTree AnalysisGiven m=101, a tree of height h=4 has:
Minimum Keys:
Maximum Keys: