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 74, 63, 41, 31 and 12, in that order. How many comparisons do the five puts make in all?

the answer, and why

the answer is A

74 is the top, 0. 63 meets 74, 1. 41 meets 74 and 63, 2, then turn right at 74: 63 moves up. 31 meets 63 and 41, 2. 12 meets 63, 41 and 31, 3, then turn right at 41. That is 8.

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 41 meets 74 and 63 before the tree turns.
  • That counts one comparison a put, but later puts go deeper.

new numbersthe test page's numbers

question 2

An empty AVL tree puts in 18, 78 and 77. How many turns does it make?

the answer, and why

the answer is C

77 hangs left of 78, below 18. 18's right side is 2 levels high and leans inwards. So it turns right at 78, then left at 18: 2 turns, and 77 ends on top.

if you chose another

  • One turn, left at 18, would put 78 on top with 18 below it and 77 below 18, still two levels apart.
  • 18'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 numbersthe test page's 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

Putting in 66, 9, 37, 59, 58, 60, 44 makes 14 comparisons, the most of the four.

if you chose another

  • That order makes 13 comparisons, fewer than 14.
  • That order makes 13 comparisons, fewer than 14.
  • That order makes 12 comparisons, fewer than 14.

new numbersthe test page's numbers