Skip to content

AVL tree

storing

Level 3 of 3

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

question 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.

  1. step 21 of 28

    20304050

    10 is smaller than 40, so go left.

    numbers compared: 1

  2. step 22 of 28

    20304050

    10 is smaller than 30, so go left.

    numbers compared: 2

  3. step 23 of 28

    20304050

    10 is smaller than 20, so go left.

    numbers compared: 3

  4. step 24 of 28

    1020304050

    The way runs out, so hang 10 to the left of 20.

    numbers compared: 3

  5. step 26 of 28

    1020304050

    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

  6. step 27 of 28

    1020304050

    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

  • That is the plain tree's count. With no turns, each number meets every number before it.
  • That is middle first's, the cheapest order of five; here 30 meets 50 and 40 before the tree turns.
  • That counts one comparison a put, but later puts go deeper.

new numbers

question 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.

  1. step 10 of 15

    102030

    The way runs out, so hang 20 to the left of 30.

    numbers compared: 2

  2. step 12 of 15

    102030

    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

  3. step 13 of 15

    102030

    Turn right at 30: 20 moves up into its place, and 30 hangs to its right.

    numbers compared: 2

  4. step 14 of 15

    102030

    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

  • One turn, left at 10, would put 30 on top with 10 below it and 20 below 10, still two levels apart.
  • 10's two sides are 2 levels apart, so it must turn.
  • A put mends at most once, with a single turn or a double, so 2 turns at most.

new numbers

question 3

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

  • That is middle first, level by level, the cheapest, 10.
  • That shuffle never turns and costs 11.
  • 40 first, then the rest in order, costs 13. Starting from the middle saves one comparison.

new numbers