Also called Self-balancing binary search tree and Adelson-Velsky and Landis tree.
fresh numbers
The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from avl tree's own run on the numbers shown.
choose one
1question 1
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.
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.