what a structure promises
shapes of data
what it is
A structure holds what you put in and gives it back when you ask. Its promise says what you may ask and what you get back, never how the numbers are kept. Each way of keeping them has its own cost, counted in steps such as comparisons.
Four promises come up again and again. A stack gives back the newest number first, like a pile of plates. A queue gives back the oldest first, like a line at a counter. A priority queue gives back the most urgent first. On this page that is the smallest number: think of jobs numbered by urgency, job 1 first. A dictionary keeps each key with a value, such as a word with its meaning, and finds the value from its key.
| operation | stack | queue | priority queue |
|---|---|---|---|
| put 6 | 6 | 6 | 6 |
| put 2 | 6, 2 | 6, 2 | 6, 2 |
| put 9 | 6, 2, 9 | 6, 2, 9 | 6, 2, 9 |
| take | gives 9 | gives 6 | gives 2 |
| put 5 | 6, 2, 5 | 2, 9, 5 | 6, 9, 5 |
| take | gives 5 | gives 2 | gives 5 |
| take | gives 2 | gives 9 | gives 6 |
| came out | 9, 5, 2 | 6, 2, 9 | 2, 5, 6 |
A promise is kept by a build: the numbers laid out somehow, and a recipe for each operation. The obvious build is a row. A stack keeps its top at the end of the row, so a put or a take touches only the end. A queue keeps a mark on its front number; a take moves the mark on, and no number moves. Each put and each take then costs the same few steps however many are held, while the row has room, and no comparison. A full row must first grow, which copies every number. The stack and queue pages count each put, and each take that gives a number back, as 1 step. The queue page draws its line stepping forward, as people at a counter do, but counts what the marked queue pays. A priority queue or a dictionary kept in a row costs more, and questions 1 to 3 count how much. Here is how many numbers or keys the structure holds.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: one take
the line:row before the take smallest comparisons 5, 2, 8, 3 2 3 5, 8, 3 3 2 5, 8 5 1 8 8 0 in all 6 Taking 5, 2, 8 and 3 out of an unsorted row, smallest first. Each take makes one comparison fewer than the numbers left; 6 in all. why it is allowed: Here is how many numbers are left. To find the smallest, hold the first number and compare it with each of the others, keeping the smaller. On 5, 2, 8, 3: 5 against 2 keeps 2, then 2 against 8 and 2 against 3 keep 2. That is 3 comparisons, the table's first line.
the move: add the takes, the last first
the line:why it is allowed: Let be the comparisons. Puts make none: each number goes at the end. By line 1 the first take, from all numbers, makes , and each take after it one fewer, down to 0. Any order of adding gives the same total, so they are added from the last take.
guess it, thenthe move: leave out the 0
the line:why it is allowed: Adding 0 changes nothing.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The page on adding up 1 to shows that a sum up to a last number is that number times one more, halved. So 1 + 2 + 3 is 3 times 4, halved, which is 6. Here the last number is .
guess it, thenthe move: tidy the bracket
the line:numbers stack steps unsorted row comparisons 10 20 45 100 200 4,950 1,000 2,000 499,500 Putting in that many numbers, then taking them all out. Ten times the numbers gives ten times the stack's steps, and about a hundred times the row's comparisons. why it is allowed: is . The table sets it beside a stack, which compares nothing; its work is 1 step a put and 1 a take.
guess it, thenthe move: read it
the line:why it is allowed: By line 1, each take compares the first number left with every other one, so the order the numbers arrive in changes nothing. 1,000 numbers cost 499,500, as line 5's table counts.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: one put
the line:put row after comparisons 8 8 0 5 5, 8 1 3 3, 5, 8 2 2 2, 3, 5, 8 3 in all 6 Putting 8, 5, 3 and 2 into a row kept in order. Each new number passes every number already there; 6 comparisons in all, and the takes then cost none. why it is allowed: Only comparisons count here; a take makes none, since the smallest is at the front. A put starts at the back and compares with each number, stepping past it while it is bigger. It stops at the first smaller number, and that comparison counts too. So a put compares at most once with each number there, and a number smaller than all before it compares with every one. No order costs more: this is a dearest order.
the move: add the puts
the line:why it is allowed: Let be the comparisons. By line 1 the first put finds the row empty, and each later put passes one more number than the put before.
guess it, thenthe move: the same sum as question 1
the line:put row after comparisons 5 5 0 2 2, 5 1 8 2, 5, 8 1 3 2, 3, 5, 8 3 in all 5 Putting 5, 2, 8 and 3 into a row kept in order. 8 stops at 5, and 3 passes 8 and 5, then stops at 2; 5 comparisons in all, less than a dearest order's 6. why it is allowed: This is line 2 of question 1, which lines 3 to 5 there work out. The table puts in another order.
guess it, thenthe move: read it
the line:why it is allowed: An unsorted row pays this on the takes, whatever the order. A row in order pays it only in a dearest order, and less in the rest: line 3's 5, 2, 8, 3 costs 5.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: one look up
the line:look up place looks 17 1 1 42 2 2 8 3 3 22 4 4 all four 10 35, not there 4 Looking up each key of the row 17, 42, 8, 22, kept in the order they came. The four cost 10 looks, 2.5 each on average. why it is allowed: A look compares the key you want with one key of the row, from the front, and the walk stops at the key. Here is the place of the key you want, so places 1 to cost looks. A key that is not there costs a look at every key: looks.
the move: each place times its chance
the line:why it is allowed: Let be the average. means add up: the term after it, once for each from the 1 under it to the over it. With 4 keys each place has chance 1 over 4. So adds 1, 2, 3 and 4 looks, each times 1 over 4: 10 over 4, which is 2.5, the table's average. With keys each chance is 1 over , and by line 1 the key at place costs looks.
guess it, thenthe move: use the page on expected cost
the line:where does this come from?
question 1 of the page expected costPick a whole number from 1 to , each as likely. What is its expected value? the move: each value times its chance
the line:times its chance 1 1 over 5 2 2 over 5 3 3 over 5 4 4 over 5 5 5 over 5 in all 15 over 5, which is 3 For 5, each number times its chance, 1 over 5. why it is allowed: The expected value adds each number times its chance, 1 over . , read the sum of, adds the term after it for each from the value under it to the value over it.
the move: take the chance out in front
the line:why it is allowed: Each term is multiplied by 1 over , so multiply once, after adding. For 5, 1 over 5 times 15.
the move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The whole numbers from 1 to add up to times one more than , halved, as the page on adding up 1 to works out. For 5, 5 times 6, halved, is 15.
the move: cancel the n
the line:why it is allowed: on top and underneath cancel, as is not zero. For 5, 6 over 2 is 3.
the move: read it
the line:why it is allowed: By line 4, halfway between the smallest number, 1, and the biggest, : 3 for 5.
why it is allowed: That page works out this same sum, for a number picked from 1 to , as over 2.
guess it, thenthe move: read it
the line:why it is allowed: 1,000 keys cost 500.5 looks on average.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
a worked example
Put 5, 2, 8 and 3 into each structure, then take until it is empty.
- Stack: they come out 3, 8, 2, 5, newest first. Each put and each take is 1 step, with no comparison: 8 in all.
- Queue: they come out 5, 2, 8, 3, oldest first. Again 8 steps and no comparison.
- Priority queue as an unsorted row: they come out 2, 3, 5, 8, after 6 comparisons, as question 1's first table counts.
- Priority queue as a row in order: the puts make 5 comparisons, as question 2's last table counts, and the takes none.
- Dictionary as an unsorted row of the keys 5, 2, 8 and 3: looking up 8 costs 3 looks, and 7, which is not there, costs 4.
the trap
check yourself
A structure takes in 6, 1 and 9, then gives back 9, 1 and 6. Which promise does it keep?
the answer
A stack's, newest first. A queue would give 6, 1, 9, and a priority queue that gives the smallest first 1, 6, 9.
A priority queue kept as an unsorted row takes in 10 numbers, then gives them all back. How many comparisons?
the answer
By question 1, 9 times 10, halved, which is 45.
A dictionary kept as an unsorted row holds 9 keys. On average, how many looks find a key that is there? And one that is not?
the answer
By question 3, 10 over 2, which is 5. A key that is not there costs a look at all 9.