Skip to content

trees and their words

shapes of data

what it is

A tree here is numbers joined from the top down, each with at most two just below it. One shape of it fits in a plain row of numbers. This page names its parts and counts them.

Each spot in the tree is a place, holding one number. Places are numbered 1, 2, 3 by where they sit, whatever number they hold. The top place is the root. The places just below a place are its children, at most two, and it is their parent. A place with no children is a leaf.

depth012361154783352207429116111524738433552620774829911100123
A tree of 10 places, each holding a number, above the same numbers kept as a row, each with its place number over it. Each block in the row is one level, 1 place, then 2, then 4, then the last 3 of 8.

The depth of a place is how many steps down from the root it lies: the root's is 0, its children's 1. The places of one depth make a level. The height of the tree is the depth of its deepest place. So a tree with levels 0 to 3 has height 3: height counts steps, not levels.

A tree is full when its leaves all sit on the bottom level and every other place has two children. Then each level holds twice the places of the level above: 1, 2, 4, 8. Question 1 adds them up.

A row keeps numbers one after another, in places numbered from 1. A row makes a tree: place 1 is the root, and each place in turn takes the next two places as its children, until the places run out. So no level starts until the one above it is used up. The picture shows a tree above its row: place 1's children are 2 and 3, place 2's are 4 and 5. Every result below is for a tree of this shape. A chain of 3 places, each the only child of the one above, is a tree too, but not this shape. Question 2 finds every place's children and parent, and question 3 the height.

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 full tree of height hh. How many places does it hold?
  1. the move: add up the levels

    the line:
    P=1+2+4+⋯+2hP = 1 + 2 + 4 + \dots + 2^h
    depth01212345671234567012
    A full tree of height 2. Its levels hold 1, 2 and 4 places, 7 in all, and its row holds the same 7.

    why it is allowed: Let PP be the places. Level 0 holds the root alone. Every place off the bottom level has two children, so each level holds twice the one above. Level 1 holds 2, level 2 holds 2 times 2, so the last, level hh, holds hh twos multiplied, 2h2^h.

  2. the move: use the doubling sum

    the line:
    P=2h+1−1P = 2^{h + 1} - 1
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

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

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The page on doubling sums shows that line 1's sum is one less than its next term, 2h+12^{h + 1}.

  3. the move: read it

    the line:
    height 2: 7 placesheight 3: 15 places\begin{gathered} \text{height 2: 7 places} \\ \text{height 3: 15 places} \end{gathered}

    why it is allowed: At height 2, line 2 gives 2 to the power 3, less 1, which is 7, and the levels 1, 2 and 4 add to 7. At height 3 it gives 16 less 1, which is 15.

try it with a small hh: at h=2h = 2, line 1 gives P=7P = 7, which you can work out by hand

question 2From place ii of a tree kept in a row, which places lie one step down, and which one step up?
  1. the move: find where a level starts

    the line:
    level d startsat place 2d\begin{gathered} \text{level } d \text{ starts} \\ \text{at place } 2^d \end{gathered}
    depth012312345678910123456789100123
    The tree of 10 places. Its levels start at places 1, 2, 4 and 8, each block in the row starting where the one before ends.

    why it is allowed: Say place ii is on level dd. No level starts until the one above it is used up, so the levels above level dd are full. For level 3, levels 0 to 2 hold 1, 2 and 4, which is 7 places, so level 3 starts at place 8. In general the levels above form a full tree whose height is one less than dd. By question 1 it holds one place fewer than 2d2^d, so level dd starts at place 2d2^d. Level 0 is the root alone, at place 1, and 2 to the power 0 is 1.

  2. the move: count what comes before the first child

    the line:
    C=2d+1+2(i−2d)C = 2^{d + 1} + 2(i - 2^d)
    1234567891012345678910
    Places 2 and 3 take their children in turn, 4 and 5, then 6 and 7. So the first child of place 3 comes after the two of place 2.

    why it is allowed: Let CC be the place of the first child of ii. On level dd, i−2di - 2^d places come before ii. The row fills level d+1d + 1 from its left end, each place's children in turn, so each place before ii has its two children there before ii's first. That level starts at place 2d+12^{d + 1}. For place 3, one place comes before it on level 1, so its first child is 4 + 2, place 6.

  3. the move: multiply out

    the line:
    C=2d+1+2i−2⋅2dC = 2^{d + 1} + 2i - 2 \cdot 2^d

    why it is allowed: Twice i−2di - 2^d is twice ii less twice 2d2^d.

  4. the move: tidy the power

    the line:
    C=2d+1+2i−2d+1C = 2^{d + 1} + 2i - 2^{d + 1}

    why it is allowed: Twice 2d2^d is 2d+12^{d + 1}.

  5. the move: cancel

    the line:
    C=2iC = 2i

    why it is allowed: The first and last terms cancel. For place 3, twice 3 is 6, as line 2 found.

  6. the move: the second child is next

    the line:
    its second childis place 2i+1\begin{gathered} \text{its second child} \\ \text{is place } 2i + 1 \end{gathered}

    why it is allowed: The two children stand side by side. For place 3, places 6 and 7. A place whose first child would fall past the end of the row is a leaf.

  7. the move: halve a first child

    the line:
    ⌊2i/2⌋=i\lfloor 2i / 2 \rfloor = i

    why it is allowed: The floor brackets round down, as on the page on halving. Half of 2i2i is ii exactly, so rounding changes nothing.

  8. the move: halve a second child

    the line:
    ⌊(2i+1)/2⌋=i\lfloor (2i + 1) / 2 \rfloor = i

    why it is allowed: Half of 2i+12i + 1 is ii and a half, which rounds down to ii.

  9. the move: read it

    the line:
    past the root, the parentof place i is place ⌊i/2⌋\begin{gathered} \text{past the root, the parent} \\ \text{of place } i \text{ is place } \lfloor i / 2 \rfloor \end{gathered}

    why it is allowed: Every place but the root is the first or the second child of some place, twice it or one more, by lines 5 and 6. Halving it and rounding down gives that place back, by lines 7 and 8. For places 6 and 7, place 3.

try it with a small ii: at i=3i = 3, line 2 gives C=6C = 6, which you can work out by hand

question 3A tree of nn places kept in a row. What is its height?
  1. the move: the last place is the deepest

    the line:
    place n lies onthe last level\begin{gathered} \text{place } n \text{ lies on} \\ \text{the last level} \end{gathered}

    why it is allowed: The row fills the levels in order, so its last place, place nn, is on the last level. So the height is the depth of place nn.

  2. the move: each step up halves

    the line:
    a step up halves the place,rounding down\begin{gathered} \text{a step up halves the place,} \\ \text{rounding down} \end{gathered}
    13223451678910012345678910
    From place 10 up to the root, place 1, each step halving and rounding down, 10, 5, 2, 1, the steps counted beside them. Three steps, so place 10 has depth 3.

    why it is allowed: By question 2, the parent of place ii is place ⌊i/2⌋\lfloor i / 2 \rfloor. From place 10 the steps up go 5, 2, 1.

  3. the move: count the steps

    the line:
    H=halvings, rounding down,that take n to 1\begin{gathered} H = \text{halvings, rounding down,} \\ \text{that take } n \text{ to 1} \end{gathered}

    why it is allowed: Let HH be the height. By lines 1 and 2 it is the steps from place nn up to place 1, each one halving, rounding down.

  4. the move: use halving and logarithms

    the line:
    H=⌊log⁡2 n⌋H = \lfloor \log_2\, n \rfloor
    where does this come from?
    question 2 of the page halving and logarithmsHalve nn, rounding down each time. How many halvings reach 1?
    1. the move: name the log rounded down

      the line:
      K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
      numberlog× 2+ 14283164113.46352.322211100
      Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving.

      why it is allowed: Here nn is the number halved, from 2 up; for 11, KK is 3.

    2. the move: rounding down never adds

      the line:
      ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

      why it is allowed: For 11, 3 is at most 3.46.

    3. the move: rounding down takes off less than 1

      the line:
       log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

      why it is allowed: For 11, 4 is above 3.46.

    4. the move: raise 2 to each side of line 2

      the line:
      2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

      why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

    5. the move: 2 to the log of n is n

      the line:
      2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

      why it is allowed: By what a log is. For 11, 8 is at most 11.

    6. the move: raise 2 to each side of line 3

      the line:
      2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

      why it is allowed: As in line 4. For 11, about 11 is below 16.

    7. the move: 2 to the log of n is n

      the line:
      n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

      why it is allowed: As in line 5. For 11, 11 is below 16.

    8. the move: halve both sides of line 5

      the line:
      2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

      why it is allowed: For 11, 4 is at most 5.5, which is not whole.

    9. the move: write the half as a power

      the line:
      2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

      why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

    10. the move: round the half down

      the line:
      2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

      why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as KK is at least 1 for nn from 2 up.

    11. the move: halve both sides of line 7

      the line:
      n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

      why it is allowed: For 11, 5.5 is below 8.

    12. the move: write the half as a power

      the line:
      n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

      why it is allowed: As in line 9. Half of 16 is 8.

    13. the move: round the half down

      the line:
      ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

      why it is allowed: Rounding down never adds. For 11, 5 is below 8.

    14. the move: take the log of line 10

      the line:
       log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

      why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

    15. the move: the log of a power of 2 is the power

      the line:
      ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

      why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

    16. the move: take the log of line 13

      the line:
       log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

      why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

    17. the move: the log of a power of 2 is the power

      the line:
       log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

      why it is allowed: For 11, about 2.32 is below 3.

    18. the move: round the log down

      the line:
      ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
      the numberat leastbelowits log rounded down
      118163
      5482
      2241
      1120
      Each halving takes 1 off the log rounded down.

      why it is allowed: By lines 15 and 17, the log of the half is at least one less than KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

    19. the move: use line 18 again and again

      the line:
      after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

      why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after KK. For 11, 3, 2, 1, 0.

    20. the move: a log rounded down to 0 belongs to 1

      the line:
      K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

      why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.

    the whole page: halving and logarithms

    why it is allowed: The page on halving shows that halving nn, rounding down, reaches 1 after ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor halvings. For 10, the biggest power of 2 not past it is 8, three 2s multiplied, so 3 halvings, as in the picture. A tree of 1 place needs none, and the log of 1 is 0.

  5. the move: read it

    the line:
    10 places: height 31,000 places: height 9\begin{gathered} 10 \text{ places: height 3} \\ 1{,}000 \text{ places: height 9} \end{gathered}

    why it is allowed: 10 lies between 8 and 16, so its log rounded down is 3. 1,000 lies between 512 and 1,024, so 9. Doubling the places adds 1 to the height. 20 places end at place 20, the first child of place 10 by question 2, one level below where 10 places end.

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

a worked example

Read a tree of 6 places kept in a row.

  1. Place 1 is the root. Its children are places 2 and 3, and place 2's are 4 and 5.
  2. Place 3 has one child, 6: its second, 7, would be past the end.
  3. Places 4, 5 and 6 are the leaves: twice 4 is 8, past the end.
  4. The parent of 6 is 3, its half. The parent of 5 is 2, its half rounded down.
  5. Place 6 is two steps below the root: 6, 3, 1. So the height is 2. Check: 6 lies between 4 and 8, and its log rounded down is 2.

the trap

check yourself

  1. How many places does a full tree of height 4 hold, and how many are leaves?

    the answer

    By question 1, one less than 2 to the power 5, which is 31. The leaves fill the last level, 2h2^h of them, 16, more than half.

  2. A row holds 13 places. Which places hang below place 6, and which place does it hang below?

    the answer

    Below it, places 12 and 13, twice 6 and one more. Above it, place 3, half of 6.

  3. What is the height of a tree of 40 places kept in a row?

    the answer

    40 lies between 32 and 64, so its log rounded down is 5, and the height is 5. By hand, from place 40 up, 20, 10, 5, 2, 1, five steps.