An empty AVL tree puts in 50, 40, 30, 20 and 10, in that order. How many comparisons do the five puts make in all?
the answer, and why
the answer is A
50 is the top, 0. 40 meets 50, 1. 30 meets 50 and 40, 2, then turn right at 50: 40 moves up. 20 meets 40 and 30, 2. 10 meets 40, 30 and 20, 3, then turn right at 30. That is 8.
step 21 of 28
10 is smaller than 40, so go left.
numbers compared: 1
step 22 of 28
10 is smaller than 30, so go left.
numbers compared: 2
step 23 of 28
10 is smaller than 20, so go left.
numbers compared: 3
step 24 of 28
The way runs out, so hang 10 to the left of 20.
numbers compared: 3
step 26 of 28
At 30, the left side is 2 levels high and the right side empty: two levels apart, so the tree must turn.
numbers compared: 3
step 27 of 28
Turn right at 30: 20 moves up into its place, and 30 hangs to its right.
numbers compared: 3
Operation 5 puts 10 in. It meets 40, 30 and 20, 3 comparisons. Climbing back, 30's left side is 2 levels high and its right empty, so turn right at 30.
if you chose another
BThat is the plain tree's count. With no turns, each number meets every number before it.
CThat is middle first's, the cheapest order of five; here 30 meets 50 and 40 before the tree turns.
DThat counts one comparison a put, but later puts go deeper.
2question 2
An empty AVL tree puts in 10, 30 and 20. How many turns does it make?
the answer, and why
the answer is D
20 hangs left of 30, below 10. 10's right side is 2 levels high and leans inwards. So it turns right at 30, then left at 10: 2 turns, and 20 ends on top.
step 10 of 15
The way runs out, so hang 20 to the left of 30.
numbers compared: 2
step 12 of 15
At 10, the left side is empty and the right side 2 levels high: two levels apart, so the tree must turn.
numbers compared: 2
step 13 of 15
Turn right at 30: 20 moves up into its place, and 30 hangs to its right.
numbers compared: 2
step 14 of 15
Turn left at 10: 20 moves up into its place, and 10 hangs to its left.
numbers compared: 2
20 hangs left of 30. 10's high side leans inwards, so turn right at 30, then left at 10.
if you chose another
AOne turn, left at 10, would put 30 on top with 10 below it and 20 below 10, still two levels apart.
B10's two sides are 2 levels apart, so it must turn.
CA put mends at most once, with a single turn or a double, so 2 turns at most.
3question 3
Which is true of putting different numbers into an empty AVL tree?
the answer, and why
the answer is A
Section 4 holds every order between , at least from 4 numbers on, and , at most .
BThe turns keep them short. Seven in order cost 14 here, against 21 in the plain tree.
CThe thinnest tree of section 4 puts 7 numbers on 4 levels, where 3 would do.
DA turn moves places and weighs no numbers, so it adds no comparison.
4question 4
Which order of these seven numbers costs the most comparisons, put into an empty AVL tree?
the answer, and why
the answer is B
In order, the puts make 0, 1, 2, 2, 3, 3 and 3, which is 14. The others cost 10, 11 and 13.
if you chose another
AThat is middle first, level by level, the cheapest, 10.
CThat shuffle never turns and costs 11.
D40 first, then the rest in order, costs 13. Starting from the middle saves one comparison.
pen and paper
5question 5about 10 minutes
Put 10, 20, 30, 40, 50, 60 and 70 into an empty AVL tree, in that order.
Draw the tree after each turn.
How many comparisons does each put make?
How many does the plain search tree make on the same order?
a hint
A turn comes after 30, 50, 60 and 70 go in.
a worked answer
The puts make 0, 1, 2, 2, 3, 3 and 3: 14. The turns are left at 10, at 30, at 20 and at 50. The last tree is middle first's, 40 on top. The plain tree makes a path, 0 + 1 + 2 + 3 + 4 + 5 + 6, which is 21.
put
meets
comparisons
then turns
10
nothing
0
20
10
1
30
10, 20
2
left at 10
40
20, 30
2
50
20, 30, 40
3
left at 30
60
20, 40, 50
3
left at 20
70
40, 50, 60
3
left at 50
in all
14
4 turns
Each put meets one number a level. After each turn, the next put starts from a shorter tree.
The four trees after the turns, ending with 40 on top.
Comparisons 0, 1, 2, 2, 3, 3 and 3: 14.
The plain tree: 21, a path.
6question 6about 8 minutes
A tree keeps each place's two sides within one level.
Work out the fewest numbers it can hold on 1 to 6 levels.
Can 12 numbers stand on 6 levels? Why?
Check line 6 of the question on the ceiling at 12 numbers.
a hint
The top has one side of levels, and the other of at least .
a worked answer
1 and 2. Then 2 + 1 + 1, which is 4, and 4 + 2 + 1, which is 7. Then 7 + 4 + 1, which is 12, and 12 + 7 + 1, which is 20. 6 levels need at least 20 numbers, so 12 stand on at most 5. Line 6 allows twice the log of 13, about 7.4: true, but looser.
levels
fewest numbers
1
1
2
2
3
4
4
7
5
12
6
20
Each is the two above it added, plus 1.
Why does no order of numbers cost more than comparisons?11 of 11 shown
1the move: price a put by the levels
the line:
step 29 of 37
To put 10 in, start at the top.
numbers compared: 0
step 30 of 37
10 is smaller than 50, so go left.
numbers compared: 1
step 31 of 37
10 is smaller than 30, so go left.
numbers compared: 2
step 32 of 37
10 is smaller than 20, so go left.
numbers compared: 3
step 33 of 37
The way runs out, so hang 10 to the left of 20.
numbers compared: 3
50, 30, 60, 20, 40 and 70 stand on 3 levels. 10 meets 50, 30 and 20, one a level, 3 comparisons, the most 3 levels allow.
why it is allowed: Its walk goes down one level at each number it meets, and hangs below the last. So a tree of levels costs a put at most comparisons.
2the move: ask the fewest numbers, not the most levels
the line:
step 33 of 37
The way runs out, so hang 10 to the left of 20.
numbers compared: 3
step 34 of 37
At 20, the left side is one level high and the right side empty, so it is balanced.
numbers compared: 3
step 35 of 37
At 30, the left side is 2 levels high and the right side one level high, so it is balanced.
numbers compared: 3
step 36 of 37
At 50, the left side is 3 levels high and the right side 2 levels high, so it is balanced.
numbers compared: 3
The thinnest tree of 4 levels holds 7 numbers. Climbing back from 10, at 20, 30 and 50 the left side is one level higher than the right.
levels
fewest numbers
one more
1
1
2
1.41
2
2
3
2
3
4
5
2.83
4
7
8
4
5
12
13
5.66
6
20
21
8
Each fewest is the two above it added, plus 1. One more than each stays ahead of the last column, as line 5 shows.
why it is allowed: The top has one side of levels, or the tree would be shorter. The other side is within one level, so at least . Each side holds fewest when it too is a thinnest tree, and the 1 is the top. So 1, 2, then 2 + 1 + 1, which is 4, then 7, then 12.
guess it, then
3the move: add 1 to both
the line:
why it is allowed: Add 1 to line 2 and share the 2 out, one to each bracket. In the table, 13 is 8 plus 5.
guess it, then
4the move: at least double, two levels down
the line:
why it is allowed: By line 2, a thinnest tree of levels holds a thinnest tree of and more, so line 3's first bracket is at least its second. In the table, 13 is at least twice 5.
guess it, then
5the move: climb from the start
the line:
why it is allowed: It holds at 1 and 2 levels, as the table's first rows show. By line 4, two levels more at least double the left side and exactly double the right. So it holds at every : the climb of the page on proof by induction.
guess it, then
6the move: turn it round
the line:
why it is allowed: A tree of numbers on levels holds at least , so is at least by line 5. So half of is at most , by the page on halving and logarithms, and at most twice that. For 12 numbers, at most about 7.4 levels; the table's thinnest has 5.
guess it, then
7the move: price put k
the line:
why it is allowed: Put goes into a tree holding one number fewer than . So by line 6, with that number as , is , and it has at most levels. By line 1, at most that many comparisons. Put 7 goes into 6 numbers: at most about 5.6 levels.
guess it, then
8the move: add the puts
the line:
why it is allowed: adds line 7's ceiling over the puts. Put 1 costs 0, which is . For 7, about 24.6.
guess it, then
9the move: read it
the line:
numbers
orders tried
dearest
ceiling
5
120
8
13.81
6
720
11
18.98
7
5,040
14
24.60
8
40,320
18
30.60
Every order of 1 to n tried. The dearest always lies under the ceiling.
why it is allowed: Lines 1 to 8 hold for every order. The table tries every order of short rows, and the dearest stays well under .
guess it, then
10the move: a simpler ceiling
the line:
why it is allowed: Each of the terms of line 8 is at most the last, .
guess it, then
11the move: read it as a bound
the line:
why it is allowed: By line 10, stays under twice .
guess it, then
try it with a small : at , line 8 gives , about 24.6