Skip to content

store and look up

Keep numbers so each one can be found again. Race two search trees and two hash tables putting in the same numbers.

Each racer puts the numbers in, one at a time. Each put first looks for its number, and stops if it is already there.

A hash table keeps numbers in numbered boxes, and the remainder after dividing a number picks the box it tries first: chaining's boxes are buckets, open addressing's are slots. A look checks one stored number against the number being put. Chaining has nothing to check in an empty bucket. Open addressing counts every slot it checks, even the empty one a number lands in. At each tick of the clock, every lane still putting makes one more look.

The binary search tree walks down from the top, left for smaller and right for bigger, and hangs the number where the walk ends.

  1. step 23 of 27

    123467

    5 is bigger than 2, so go right.

    numbers compared: 1

  2. step 24 of 27

    123467

    5 is smaller than 6, so go left.

    numbers compared: 2

  3. step 25 of 27

    123467

    5 is bigger than 4, so go right.

    numbers compared: 3

  4. step 26 of 27

    1234567

    The way runs out, so hang 5 to the right of 4.

    numbers compared: 3

Putting 5 last, it meets 2, 6 and 4: 3 looks. It hangs right of 4.

Chaining works out a number's bucket, its remainder after dividing by 5, then looks at each number already in that bucket.

  1. step 13 of 18

    16273401234

    1 leaves 1 when divided by 5, so it belongs in bucket 1.

    compared: 0

  2. step 14 of 18

    16273401234

    6 is not 1.

    compared: 1

  3. step 15 of 18

    61273401234

    1 is not in bucket 1 yet, so put it in.

    compared: 1

1 divided by 5 leaves 1, so 1 goes to bucket 1, which already holds 6. One look at 6, then 1 joins the bucket.

Open addressing works out a number's home slot, its remainder after dividing by 7, and steps on from there until it finds the number or an empty slot.

  1. step 4 of 7

    01234561

    8 leaves 1 when divided by 7, so its home is slot 1.

    slots looked at: 0

  2. step 5 of 7

    01234561

    Slot 1 holds 1, not 8.

    slots looked at: 1

  3. step 6 of 7

    012345618

    Slot 2 is free, so put 8 there.

    slots looked at: 2

8 divided by 7 leaves 1, so 8's home is slot 1, which holds 1. It steps on to slot 2, which is empty. 2 looks, and 8 lands there.

The AVL tree walks down as the binary search tree does, then turns on the way back up wherever one side grew two levels taller. A level is one step down the tree.

  1. step 8 of 46

    26

    7 is bigger than 2, so go right.

    numbers compared: 1

  2. step 9 of 46

    26

    7 is bigger than 6, so go right.

    numbers compared: 2

  3. step 10 of 46

    267

    The way runs out, so hang 7 to the right of 6.

    numbers compared: 2

  4. step 13 of 46

    267

    Turn left at 2: 6 moves up into its place, and 2 hangs to its left.

    numbers compared: 2

Putting 7, it meets 2 and 6: 2 looks. It hangs right of 6. Now 2 has nothing on its left and two levels on its right, 6 then 7, so the tree turns, and 6 goes on top.

or make one

race these, up to four

one tick is

Without scripts the race cannot run, so each lane shows where its racer ends.

Tick 14: no lane has made more than 14 looks. Binary search tree, hash table with chaining, hash table with open addressing and avl tree have finished.

binary search tree

done in 12 looks
f1a2e3d4g5b6c7

hash table with chaining

done in 2 looks
561273401234

hash table with open addressing

done in 7 looks
01234567123456

avl tree

done in 14 looks
f1a2e3d4g5b6c7

what the marks mean

  • a stored number being compared
  • a number going in, or moved by a turn
  • a stored number met again, or found to take out
  • a number turned away, or taken out
  • a stored number
  • how much of its race a lane has run

which suits this row

On these 7 numbers, hash table with chaining made the fewest looks: 2, against 7 for hash table with open addressing, 12 for binary search tree and 14 for avl tree.

each racer's own page

The same racers on bigger rows: average looks up, how many numbers across, each labelled grid line ten times the last. A solid line is measured; a dashed one carries on the formula its own page derives.
102104106108101010121011021031041051061071011021031041051061071081091010101110121013101102103104105106107
  • binary search tree
  • hash table with chaining
  • hash table with open addressing
  • avl tree

the starting racers as the row grows

On these 7 numbers, the remainders after dividing by 5 are 2, 1, 2, 4, 3, 1, 0. Only 7 and 1 go to a bucket that already holds a number, 2 and 6, so chaining makes 2 looks. Dividing by 7 leaves 2, 6, 0, 4, 3, 1, 5: each number has its own empty home slot, 1 look each, 7 in all. This site's hash tables keep their size, 5 buckets and 7 slots, and add no boxes as they fill, so bigger rows tell another story. At 1,000 numbers, each bucket holds about 200, and each number looks at every one before it in its bucket. A bucket of 4 makes 0 + 1 + 2 + 3 = 6 looks: pair 0 with 3 and 1 with 2, 2 pairs of 3. A bucket of 200 makes 0 + 1 + ... + 199: pair 0 with 199, 1 with 198, and so on. That is 100 pairs of 199, so 19,900 looks. 5 buckets make 99,500, about 100,000. Open addressing makes the fewest, but it fails the job: its 7 slots are full by the 7th number. It turns every later number away after looking at all 7. 1,000 - 7 = 993 numbers turned away, 7 looks each, make 6,951 of its 6,966 looks. Of the two trees, the AVL tree makes fewer looks than the plain tree, about 8,723 against 11,190. On the 7 numbers here it makes more, 14 against 12, all of it in putting 1. Putting 3, it turned again, and 3 went above 2 and 4, under 6. So 1 meets 6, 3 and 2: 3 looks. In the plain tree, 2 is on top, so 1 meets only 2: 1 look. On longer rows, its turns keep it short, and that pays.

First, their looks on the starting row, run once. Then on bigger rows, a plain number is the mean of 20 rows, rounded. In italics, the plain tree's and chaining's million each come from that racer's own formula: follow its link to the racer's page. The fewest on each line is in bold. These averages come from each racer's own page. The trees' are of shuffles of 1 to 1,000, or to a million. The hash tables' are not: each number goes to a bucket or home slot picked at random, so some buckets get more numbers than others. Chaining's own page works out its true average, 99,900, a little over the 99,500 of an even share. Its 20 random rows here averaged 99,906. The AVL tree has no formula for its average, and its runs stop at 100,000 numbers, so its million is not measured.

binary search treehash table with chaininghash table with open addressingavl tree
the starting row2, 6, 7, 4, 3, 1, 5122714
1,000 numbers11,19099,9066,9668,723
1,000,000 numbers24,785,48299,999,900,0006,999,966not measured

the starting racers on 7 numbers of each shape

The row's shape, its order or its repeats, changes most racers' looks, not the winner here. In order or back to front, the plain tree grows as one long line. Each number meets every one before it: 0 + 1 + 2 + 3 + 4 + 5 + 6 = 21 looks. The AVL tree turns, and makes 14.

  1. step 35 of 36

    1234567

    The way runs out, so hang 7 to the right of 6.

    numbers compared: 6

The plain tree on 1 to 7 in order. Each number hangs right of the last: one long line, 7 levels deep. 7 meets all 6 before it.
  1. step 41 of 45

    1234567

    The way runs out, so hang 7 to the right of 6.

    numbers compared: 3

  2. step 44 of 45

    1234567

    Turn left at 5: 6 moves up into its place, and 5 hangs to its left.

    numbers compared: 3

The AVL tree on the same row. Putting 7, it meets 4, 5 and 6: 3 looks. It hangs right of 6, then turns at 5, and 6 goes above 5 and 7. The tree is 3 levels deep.

Their looks on the row the race makes in each shape.

binary search treehash table with chaininghash table with open addressingavl tree
shuffled2, 6, 7, 4, 3, 1, 5122714
in order1, 2, 3, 4, 5, 6, 7212714
back to front7, 6, 5, 4, 3, 2, 1212714
nearly in order1, 2, 3, 4, 6, 5, 7202713
many repeats2, 1, 2, 3, 3, 1, 28478