Skip to content

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.

operationstackqueuepriority queue
put 6666
put 26, 26, 26, 2
put 96, 2, 96, 2, 96, 2, 9
takegives 9gives 6gives 2
put 56, 2, 52, 9, 56, 9, 5
takegives 5gives 2gives 5
takegives 2gives 9gives 6
came out9, 5, 26, 2, 92, 5, 6
Seven operations fed to three promises, one per line. After a put, each cell shows what is held, oldest first; after a take, what came back.

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 nn 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 nn, 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.

question 1A priority queue kept as an unsorted row. Put in nn numbers, then take them all out. How many comparisons?
  1. the move: one take

    the line:
    a take from m numbersmakes m−1 comparisons\begin{gathered} \text{a take from } m \text{ numbers} \\ \text{makes } m - 1 \text{ comparisons} \end{gathered}
    row before the takesmallestcomparisons
    5, 2, 8, 323
    5, 8, 332
    5, 851
    880
    in all6
    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 mm 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.

  2. the move: add the takes, the last first

    the line:
    C=0+1+⋯+(n−1)C = 0 + 1 + \dots + (n - 1)

    why it is allowed: Let CC be the comparisons. Puts make none: each number goes at the end. By line 1 the first take, from all nn numbers, makes n−1n - 1, 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.

  3. the move: leave out the 0

    the line:
    C=1+2+⋯+(n−1)C = 1 + 2 + \dots + (n - 1)

    why it is allowed: Adding 0 changes nothing.

  4. the move: use the page on adding up

    the line:
    C=(n−1)((n−1)+1)2C = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The page on adding up 1 to nn 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 n−1n - 1.

  5. the move: tidy the bracket

    the line:
    C=(n−1)n2C = \frac{(n - 1)n}{2}
    numbersstack stepsunsorted row comparisons
    102045
    1002004,950
    1,0002,000499,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: (n−1)+1(n - 1) + 1 is nn. The table sets it beside a stack, which compares nothing; its work is 1 step a put and 1 a take.

  6. the move: read it

    the line:
    every take compares its waythrough every number left\begin{gathered} \text{every take compares its way} \\ \text{through every number left} \end{gathered}

    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.

try it with a small nn: at n=4n = 4, line 2 gives C=6C = 6, which you can work out by hand

question 2A priority queue kept instead as a row in order, smallest first. Put in nn numbers, each smaller than all before it, such as 8, 5, 3, 2. How many comparisons?
  1. the move: one put

    the line:
    a put walks in from the backpast every bigger number\begin{gathered} \text{a put walks in from the back} \\ \text{past every bigger number} \end{gathered}
    putrow aftercomparisons
    880
    55, 81
    33, 5, 82
    22, 3, 5, 83
    in all6
    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.

  2. the move: add the puts

    the line:
    P=0+1+⋯+(n−1)P = 0 + 1 + \dots + (n - 1)

    why it is allowed: Let PP 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.

  3. the move: the same sum as question 1

    the line:
    P=(n−1)n2P = \frac{(n - 1)n}{2}
    putrow aftercomparisons
    550
    22, 51
    82, 5, 81
    32, 3, 5, 83
    in all5
    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.

  4. the move: read it

    the line:
    the total of question 1,paid on the puts\begin{gathered} \text{the total of question 1,} \\ \text{paid on the puts} \end{gathered}

    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.

try it with a small nn: at n=4n = 4, line 2 gives P=6P = 6, which you can work out by hand

question 3A dictionary kept as an unsorted row of nn keys. Look up a key that is there, equally likely to be at any place. How many looks, on average?
  1. the move: one look up

    the line:
    the key at place kcosts k looks\begin{gathered} \text{the key at place } k \\ \text{costs } k \text{ looks} \end{gathered}
    look upplacelooks
    1711
    4222
    833
    2244
    all four10
    35, not there4
    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 kk is the place of the key you want, so places 1 to kk cost kk looks. A key that is not there costs a look at every key: nn looks.

  2. the move: each place times its chance

    the line:
    L=∑k=1nk×1nL = \sum_{k=1}^{n} k \times \frac{1}{n}

    why it is allowed: Let LL be the average. Σ\Sigma means add up: the term after it, once for each kk from the 1 under it to the nn over it. With 4 keys each place has chance 1 over 4. So LL adds 1, 2, 3 and 4 looks, each times 1 over 4: 10 over 4, which is 2.5, the table's average. With nn keys each chance is 1 over nn, and by line 1 the key at place kk costs kk looks.

  3. the move: use the page on expected cost

    the line:
    L=n+12L = \frac{n + 1}{2}
    where does this come from?
    question 1 of the page expected costPick a whole number from 1 to nn, each as likely. What is its expected value?
    1. the move: each value times its chance

      the line:
      E=∑k=1nk×1nE = \sum_{k=1}^{n} k \times \frac{1}{n}
      kktimes its chance
      11 over 5
      22 over 5
      33 over 5
      44 over 5
      55 over 5
      in all15 over 5, which is 3
      For 5, each number times its chance, 1 over 5.

      why it is allowed: The expected value EE adds each number kk times its chance, 1 over nn. Σ\Sigma, read the sum of, adds the term after it for each kk from the value under it to the value over it.

    2. the move: take the chance out in front

      the line:
      E=1n∑k=1nkE = \frac{1}{n} \sum_{k=1}^{n} k

      why it is allowed: Each term is multiplied by 1 over nn, so multiply once, after adding. For 5, 1 over 5 times 15.

    3. the move: use the page on adding up

      the line:
      E=1n×n(n+1)2E = \frac{1}{n} \times \frac{n(n + 1)}{2}
      where does this come from?
      question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
      1. the move: draw the sum as a staircase

        the line:
        S=1+2+⋯+nS = 1 + 2 + \dots + n

        why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

      2. the move: take two staircases

        the line:
        2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

        why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

      3. the move: fit them into a rectangle

        the line:
        2S=n(n+1)2S = n(n + 1)
        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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

      4. the move: halve both sides

        the line:
        S=n(n+1)2S = \frac{n(n + 1)}{2}

        why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

      the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

      why it is allowed: The whole numbers from 1 to nn add up to nn times one more than nn, halved, as the page on adding up 1 to nn works out. For 5, 5 times 6, halved, is 15.

    4. the move: cancel the n

      the line:
      E=n+12E = \frac{n + 1}{2}

      why it is allowed: nn on top and nn underneath cancel, as nn is not zero. For 5, 6 over 2 is 3.

    5. the move: read it

      the line:
      the middle of1 and n\begin{gathered} \text{the middle of} \\ \text{1 and } n \end{gathered}

      why it is allowed: By line 4, halfway between the smallest number, 1, and the biggest, nn: 3 for 5.

    the whole page: expected cost

    why it is allowed: That page works out this same sum, for a number picked from 1 to nn, as n+1n + 1 over 2.

  4. the move: read it

    the line:
    about half the row,on average\begin{gathered} \text{about half the row,} \\ \text{on average} \end{gathered}

    why it is allowed: 1,000 keys cost 500.5 looks on average.

try it with a small nn: at n=4n = 4, line 2 gives L=2.5L = 2.5, 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.

  1. Stack: they come out 3, 8, 2, 5, newest first. Each put and each take is 1 step, with no comparison: 8 in all.
  2. Queue: they come out 5, 2, 8, 3, oldest first. Again 8 steps and no comparison.
  3. Priority queue as an unsorted row: they come out 2, 3, 5, 8, after 6 comparisons, as question 1's first table counts.
  4. Priority queue as a row in order: the puts make 5 comparisons, as question 2's last table counts, and the takes none.
  5. 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

  1. 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.

  2. 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.

  3. 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.