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 96, 64, 62, 51 and 23, in that order. How many comparisons do the five puts make in all?

the answer, and why

the answer is D

96 is the top, 0. 64 meets 96, 1. 62 meets 96 and 64, 2, then turn right at 96: 64 moves up. 51 meets 64 and 62, 2. 23 meets 64, 62 and 51, 3, then turn right at 62. 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 62 meets 96 and 64 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 16, 82 and 69. How many turns does it make?

the answer, and why

the answer is D

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

if you chose another

  • One turn, left at 16, would put 82 on top with 16 below it and 69 below 16, still two levels apart.
  • 16'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 D

Putting in 9, 13, 62, 10, 16, 15, 20 makes 13 comparisons, the most of the four.

if you chose another

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

new numbersthe test page's numbers