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.
step 23 of 27
5 is bigger than 2, so go right.
numbers compared: 1
step 24 of 27
5 is smaller than 6, so go left.
numbers compared: 2
step 25 of 27
5 is bigger than 4, so go right.
numbers compared: 3
step 26 of 27
The way runs out, so hang 5 to the right of 4.
numbers compared: 3
Chaining works out a number's bucket, its remainder after dividing by 5, then looks at each number already in that bucket.
step 13 of 18
1 leaves 1 when divided by 5, so it belongs in bucket 1.
compared: 0
step 14 of 18
6 is not 1.
compared: 1
step 15 of 18
1 is not in bucket 1 yet, so put it in.
compared: 1
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.
step 4 of 7
8 leaves 1 when divided by 7, so its home is slot 1.
slots looked at: 0
step 5 of 7
Slot 1 holds 1, not 8.
slots looked at: 1
step 6 of 7
Slot 2 is free, so put 8 there.
slots looked at: 2
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.
step 8 of 46
7 is bigger than 2, so go right.
numbers compared: 1
step 9 of 46
7 is bigger than 6, so go right.
numbers compared: 2
step 10 of 46
The way runs out, so hang 7 to the right of 6.
numbers compared: 2
step 13 of 46
Turn left at 2: 6 moves up into its place, and 2 hangs to its left.
numbers compared: 2
or make one
race these, up to four
Choose two to four racers.
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.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
binary search tree
done in 12 lookshash table with chaining
done in 2 lookshash table with open addressing
done in 7 looksavl tree
done in 14 lookswhat 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
- 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 tree | hash table with chaining | hash table with open addressing | avl tree | |
|---|---|---|---|---|
| the starting row2, 6, 7, 4, 3, 1, 5 | 12 | 2 | 7 | 14 |
| 1,000 numbers | 11,190 | 99,906 | 6,966 | 8,723 |
| 1,000,000 numbers | 24,785,482 | 99,999,900,000 | 6,999,966 | not 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.
step 35 of 36
The way runs out, so hang 7 to the right of 6.
numbers compared: 6
step 41 of 45
The way runs out, so hang 7 to the right of 6.
numbers compared: 3
step 44 of 45
Turn left at 5: 6 moves up into its place, and 5 hangs to its left.
numbers compared: 3
Their looks on the row the race makes in each shape.
| binary search tree | hash table with chaining | hash table with open addressing | avl tree | |
|---|---|---|---|---|
| shuffled2, 6, 7, 4, 3, 1, 5 | 12 | 2 | 7 | 14 |
| in order1, 2, 3, 4, 5, 6, 7 | 21 | 2 | 7 | 14 |
| back to front7, 6, 5, 4, 3, 2, 1 | 21 | 2 | 7 | 14 |
| nearly in order1, 2, 3, 4, 6, 5, 7 | 20 | 2 | 7 | 13 |
| many repeats2, 1, 2, 3, 3, 1, 2 | 8 | 4 | 7 | 8 |