CPSC 221
B-Trees, and Hashing
Today
Announcements
- PA3 is out, due Monday 11/23
- Examlet 6 is Thursday to Sunday: binary search trees
- Lab AVL is due Sunday 11/08
- Next week is the quiet week: just Friday’s video lesson
Today:
- B-trees: finish the story
- Hashing: a dictionary faster than \(O(\log n)\)
Recap
Last time: the story on disks.
A 2 GHz machine does about 2 million instructions per millisecond.
A disk seek takes about 5 ms: about 10 million instructions’ worth of waiting.
An AVL tree of 300 million driving records is 28 to 41 levels deep.
One lookup can take about 41 disk seeks.
The story on disks, continued
Suppose we weren’t careful…
Following one pointer costs one disk read.
So the number of disk reads is
____________.
B-tree of order \(m\)
Goal: minimize the number of reads from disk.
- Build a tree that uses one disk block per node
- A disk block is the fundamental unit of transfer
- Nodes have more than one key
- The tree should be balanced and shallow
- In practice, branching factors over 1000 are common
A B-tree
B-tree of order \(m\): a node
Every node has room for \(m - 1\) keys, but __________________.
If a node has \(i\) keys, then it has ________ child pointers.
The keys in a node are in order.
Definition of a B-tree
A B-tree of order \(m\) is an \(m\)-way search tree in which:
- Every node holds at most \(m - 1\) keys, in order
- An internal node with \(i\) keys has \(i + 1\) children
- Every non-root node holds at least \(\lceil m/2 \rceil - 1\) keys
- The root holds at least 1 key
- All leaves are on the same level
Your Turn
Place the keys into this B-tree of order 5. Not every slot will be used.
15 22 31 38 44 50 57 63 71 79 86 94
Back to the driving records
How many keys can a B-tree of order \(m\) and height \(h\) hold?
With \(m = 1000\): how tall is the tree for 300 million records?
How many disk reads to find a record?
Where are we?
Intro to hashing: a speedier(?) alternative to AVL trees.
Hashing
Using “hash tables” to implement ____________.
Functionality of a dictionary:
Structure of a dictionary:
Key → Value
Locker # → student
Course number → schedule info
Vertex → set of incident edges
URL → html page
Dice roll → payoff amount
Associative array
A dictionary with a particular interface:
it overloads operator[] for insert and find.
unordered_map<string, int> myDict;
myDict["Miguel"] = 22; // insert
int d = myDict["Miguel"]; // find
Overview
client code declares an object of ADT dictionary:
dict<ktype, vtype> d;
d[k] = v; // insert
A hash table consists of:
A perfect hash function
A __________ example:
A perfect hash function?
How do you feel about this hash function?
A bit of perspective
General purpose hash functions
Suppose we have a general hash function, and it does a good job avoiding collisions…
Collision definition:
What characterizes a good hash function?
Universal hash functions
A set \(H\) of hash functions is said to be universal if
\[P\big(h(x) = h(y)\big) \le 1/{\color{orange}m}\]
when \(h\) is chosen randomly from \(H\).
Example (multiplicative hashing): let \({\color{seagreen}p}\) be a prime larger than any key.
Choose \({\color{crimson}a}\) at random from \(\{1, 2, \ldots, p - 1\}\).
Choose \({\color{royalblue}b}\) at random from \(\{0, 1, \ldots, p - 1\}\). Then
\[h(x) = \big(({\color{crimson}a}x + {\color{royalblue}b}) \bmod {\color{seagreen}p}\big) \bmod {\color{orange}m}\]
So we don’t have to worry about collisions, right?…
Hash function summary
We can only avoid collisions if \(|\text{KeySpace}| \le m\) and the hash is perfect.
We can thwart poor performance by choosing the hash function at random.
Universal hashing to the rescue!
We have to deal with collisions: with only ______ keys, we expect one.
We need a collision resolution strategy (next time!)
- Separate chaining
- Open addressing
- Other strategies we won’t talk about