Skip to content

Trie

storing

Level 2 of 3

Also called Prefix tree and Digital tree.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

An empty trie puts in tree, trek and trim. How many places does it make?

the answer, and why

the answer is D

tree makes 4 places. trek follows t, r and e and makes k, 1 more. trim follows t and r and makes i and m, 2 more. That is 7.

if you chose another

  • That is the visits, every letter followed or made.
  • That is the letters followed, not the places made.
  • That is one word's places; the later words make some too.

question 2

An empty trie puts in car, cat, cart and to. How many letters does it follow?

the answer, and why

the answer is C

car follows none. cat follows c and a, 2. cart follows c, a and r, 3. to follows none. That is 5.

if you chose another

  • That is the places made.
  • That is every letter, followed or made, the visits.
  • That counts the words, not the letters.

question 3

Which is true of putting nn different words of five letters into an empty trie?

the answer, and why

the answer is A

Section 4's first question counts 5 visits a word, followed or made, whatever the words share.

this tests where the formula comes from, on the learn page

if you chose another

  • A shared letter is followed, and following is a visit too.
  • Words that share their starts make fewer, 14 places for 10 words in counting order.
  • A put walks down its own letters only, one place a letter.

question 4

Which list makes the most places, put into an empty trie?

the answer, and why

the answer is C

tree, trek and trim make 4, then 1, then 2, which is 7. The others make 5, 6 and 4.

if you chose another

  • trek shares tre with tree, so the two make 5.
  • true shares tr with tree, so the two make 6.
  • The second tree follows every letter and makes nothing, so 4.

pen and paper

question 5about 8 minutes

Put tree, trek, trim and tree again into an empty trie, in that order.

  1. Draw the trie after each put.
  2. How many letters does each put follow, and how many places does it make?
  3. What does the last put change, and how many visits does it cost?
a hint

The last put follows every letter of a word already in.

a worked answer

tree makes 4. trek follows 3 and makes 1. trim follows 2 and makes 2. The second tree follows 4 and makes none: the trie already marks it, so nothing changes. 16 visits, 4 a put, and 7 places.

  • tree

    after tree: 4 places

  • treek

    after trek: 5 places

  • treekim

    after trim, and after tree again: 7 places

The trie after each put. The second tree changes nothing, so the last drawing stands for both.

this tests best, worst and average, on the learn page

what a full answer includes
  • The trie with t and r shared, e and i below r, then e and k below the first e, and m below i.
  • Followed 0, 3, 2 and 4; made 4, 1, 2 and 0.
  • The last put changes nothing and still costs 4 visits.

question 6about 6 minutes

Words of five letters go into an empty trie, all different.

  1. Why does every put cost exactly 5 visits?
  2. Why can nn words make no more than 5n5n places?
  3. Why must they make at least nn?
a hint

A place stands for the letters on the way down to it, and every word ends on level 5.

a worked answer

Each letter is followed or made, one visit, so 5 a word and 5n5n in all. Each letter makes at most one place, so at most 5n5n. Every word ends on level 5, at a place of its own, so at least nn.

Why must nn different words of five letters make at least nn places?
  1. the move: each word ends at its own place

    the line:
    two different wordsend at two places\begin{gathered} \text{two different words} \\ \text{end at two places} \end{gathered}
    1. step 14 of 16

      traint

      No t below, so make a place for it.

      visits: 5

    2. step 15 of 16

      traint

      Mark this place as the end of trait.

      visits: 5

    trait's last letter, t, is made below i, then marked as its end. train's end stays at n.

    why it is allowed: A place stands for the letters on the way down to it. Two words ending at one place would have the same letters, so they would be one word.

  2. the move: no earlier word reaches the new end

    the line:
    no earlier word passesthrough a new word’s end\begin{gathered} \text{no earlier word passes} \\ \text{through a new word's end} \end{gathered}

    why it is allowed: Every word has 5 letters, so a put stops on level 5. In the picture, train's path stops at n on level 5 and never reaches trait's t, so trait must make that t. By line 1, no earlier word ends at a new word's end, and none can pass it.

  3. the move: the last letter is made

    the line:
    a new word makesat least 1 place\begin{gathered} \text{a new word makes} \\ \text{at least 1 place} \end{gathered}

    why it is allowed: By lines 1 and 2, its end is a place no earlier word reached, so its last letter makes it.

  4. the move: add the puts

    the line:
    F=1+1+⋯+1F = 1 + 1 + \dots + 1

    why it is allowed: FF adds line 3's floor, 1 for each put. The first put makes all 5 of its places, so a real list makes more, as aaaaa, aaaab and aaaac make 5, 1 and 1.

  5. the move: n lots of 1

    the line:
    F=nF = n

    why it is allowed: Line 4 adds nn ones. Counting order comes close, 14 places for 10 words, the 4 places of aaaa shared and 10 last letters.

  6. the move: a floor

    the line:
    F≥nF \ge n

    why it is allowed: By line 5.

  7. the move: read it as a bound

    the line:
    F∈Ω(n)F \in \Omega(n)

    why it is allowed: By line 6, with the fixed multiple 1.

  8. the move: so for the places

    the line:
    the places made are atleast F, so they areΩ(n) too\begin{gathered} \text{the places made are at} \\ \text{least } F \text{, so they are} \\ \Omega(n) \text{ too} \end{gathered}

    why it is allowed: By line 4, every list of nn different words makes at least FF. At 3 words, counting order makes 7, at least 3.

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

this tests where the formula comes from, on the learn page

what a full answer includes
  • One visit a letter, 5 a word.
  • At most one place a letter: 5n5n.
  • One end place a word, on level 5: nn.