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…

38 13 51 10 25 40 84 12 37 66 89 95


Following one pointer costs one disk read.


So the number of disk reads is

____________.

B-tree of order \(m\)

12 18 27 52 58 63 77 89

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

k1 k2 … ki

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
1 2 6 7 12 14 16 3 8 25 26 29 45 52 53 55 68 28 48 17

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?

AVL trees B-trees Hash tables

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:

class dict array k hash function ⋮

A perfect hash function

(Ann, black cat) (Ben, Sasha Velour) (Cory, spy) (David, bball player) (Ellen, butterfly) (Finn, cereal killer) (Gus, ghost) (Harmony, bee) 0 1 2 3 4 5 6 7

A __________ example:

A perfect hash function?

Roll 5 dice: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19




How do you feel about this hash function?

A bit of perspective

General purpose hash functions

KeySpace 0 1 … m-1 Easy, if |KeySpace| ~ m KeySpace 0 1 … m-1

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