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 77, 69, 63, 56 and 42, in that order. How many comparisons do the five puts make in all?

the answer, and why

the answer is D

77 is the top, 0. 69 meets 77, 1. 63 meets 77 and 69, 2, then turn right at 77: 69 moves up. 56 meets 69 and 63, 2. 42 meets 69, 63 and 56, 3, then turn right at 63. 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 63 meets 77 and 69 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 1, 56 and 42. How many turns does it make?

the answer, and why

the answer is C

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

if you chose another

  • One turn, left at 1, would put 56 on top with 1 below it and 42 below 1, still two levels apart.
  • 1'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 C

Putting in 68, 62, 8, 60, 41, 30, 70 makes 14 comparisons, the most of the four.

if you chose another

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

new numbersthe test page's numbers