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.
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 , 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: add up the levels
the line: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 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 , holds twos multiplied, .
the move: use the doubling sum
the line:where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total? the move: name the sum
the line:why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
the move: double both sides
the line:why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
the move: take line 1 away
the line:why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
the move: tidy the left side
the line:why it is allowed: Two lots of less one lot is one lot.
the move: cancel what both brackets share
the line:when n is 3 1 2 4 8 16 first bracket 2 4 8 16 second bracket 1 2 4 8 left over take away 1 16 Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15. why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
the move: the terms before the last
the line:why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
the move: tidy the power
the line:why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
the move: read it
the line: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 , twice the last.
why it is allowed: The page on doubling sums shows that line 1's sum is one less than its next term, .
guess it, thenthe move: read it
the line: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.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: find where a level starts
the line: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 is on level . No level starts until the one above it is used up, so the levels above level 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 . By question 1 it holds one place fewer than , so level starts at place . Level 0 is the root alone, at place 1, and 2 to the power 0 is 1.
the move: count what comes before the first child
the line: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 be the place of the first child of . On level , places come before . The row fills level from its left end, each place's children in turn, so each place before has its two children there before 's first. That level starts at place . For place 3, one place comes before it on level 1, so its first child is 4 + 2, place 6.
guess it, thenthe move: multiply out
the line:why it is allowed: Twice is twice less twice .
guess it, thenthe move: tidy the power
the line:why it is allowed: Twice is .
guess it, thenthe move: cancel
the line:why it is allowed: The first and last terms cancel. For place 3, twice 3 is 6, as line 2 found.
guess it, thenthe move: the second child is next
the line: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.
guess it, thenthe move: halve a first child
the line:why it is allowed: The floor brackets round down, as on the page on halving. Half of is exactly, so rounding changes nothing.
guess it, thenthe move: halve a second child
the line:why it is allowed: Half of is and a half, which rounds down to .
guess it, thenthe move: read it
the line: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.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: the last place is the deepest
the line:why it is allowed: The row fills the levels in order, so its last place, place , is on the last level. So the height is the depth of place .
the move: each step up halves
the line: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 is place . From place 10 the steps up go 5, 2, 1.
guess it, thenthe move: count the steps
the line:why it is allowed: Let be the height. By lines 1 and 2 it is the steps from place up to place 1, each one halving, rounding down.
guess it, thenthe move: use halving and logarithms
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line: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 is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: 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 is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 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 and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line: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 . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line: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.
why it is allowed: The page on halving shows that halving , rounding down, reaches 1 after 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.
guess it, thenthe move: read it
the line: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.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
a worked example
Read a tree of 6 places kept in a row.
- Place 1 is the root. Its children are places 2 and 3, and place 2's are 4 and 5.
- Place 3 has one child, 6: its second, 7, would be past the end.
- Places 4, 5 and 6 are the leaves: twice 4 is 8, past the end.
- The parent of 6 is 3, its half. The parent of 5 is 2, its half rounded down.
- 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
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, of them, 16, more than half.
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.
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.