Skip to content

AVL tree

storing

Level 3 of 3

Also called Self-balancing binary search tree and Adelson-Velsky and Landis tree.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

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.

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.

question 3

Which is true of putting nn different numbers into an empty AVL tree?

the answer, and why

the answer is A

Section 4 holds every order between FF, at least n log⁡2 n8\frac{n \,\log_2\, n}{8} from 4 numbers on, and UU, at most 2n log⁡2 n2n \,\log_2\, n.

this tests where the formula comes from, on the learn page

if you chose another

  • The turns keep them short. Seven in order cost 14 here, against 21 in the plain tree.
  • The thinnest tree of section 4 puts 7 numbers on 4 levels, where 3 would do.
  • A turn moves places and weighs no numbers, so it adds no comparison.

question 4

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.

pen and paper

question 5about 10 minutes

Put 10, 20, 30, 40, 50, 60 and 70 into an empty AVL tree, in that order.

  1. Draw the tree after each turn.
  2. How many comparisons does each put make?
  3. How many does the plain search tree make on the same order?
a hint

A turn comes after 30, 50, 60 and 70 go in.

a worked answer

The puts make 0, 1, 2, 2, 3, 3 and 3: 14. The turns are left at 10, at 30, at 20 and at 50. The last tree is middle first's, 40 on top. The plain tree makes a path, 0 + 1 + 2 + 3 + 4 + 5 + 6, which is 21.

putmeetscomparisonsthen turns
10nothing0
20101
3010, 202left at 10
4020, 302
5020, 30, 403left at 30
6020, 40, 503left at 20
7040, 50, 603left at 50
in all144 turns
Each put meets one number a level. After each turn, the next put starts from a shorter tree.

this tests best, worst and average, on the learn page

what a full answer includes
  • The four trees after the turns, ending with 40 on top.
  • Comparisons 0, 1, 2, 2, 3, 3 and 3: 14.
  • The plain tree: 21, a path.

question 6about 8 minutes

A tree keeps each place's two sides within one level.

  1. Work out the fewest numbers it can hold on 1 to 6 levels.
  2. Can 12 numbers stand on 6 levels? Why?
  3. Check line 6 of the question on the ceiling at 12 numbers.
a hint

The top has one side of h−1h - 1 levels, and the other of at least h−2h - 2.

a worked answer

1 and 2. Then 2 + 1 + 1, which is 4, and 4 + 2 + 1, which is 7. Then 7 + 4 + 1, which is 12, and 12 + 7 + 1, which is 20. 6 levels need at least 20 numbers, so 12 stand on at most 5. Line 6 allows twice the log of 13, about 7.4: true, but looser.

levelsfewest numbers
11
22
34
47
512
620
Each is the two above it added, plus 1.
Why does no order of nn numbers cost more than 2n log⁡2 n2n \,\log_2\, n comparisons?
  1. the move: price a put by the levels

    the line:
    a put meets at most onenumber on each level\begin{gathered} \text{a put meets at most one} \\ \text{number on each level} \end{gathered}
    1. step 29 of 37

      203040506070

      To put 10 in, start at the top.

      numbers compared: 0

    2. step 30 of 37

      203040506070

      10 is smaller than 50, so go left.

      numbers compared: 1

    3. step 31 of 37

      203040506070

      10 is smaller than 30, so go left.

      numbers compared: 2

    4. step 32 of 37

      203040506070

      10 is smaller than 20, so go left.

      numbers compared: 3

    5. step 33 of 37

      10203040506070

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

      numbers compared: 3

    50, 30, 60, 20, 40 and 70 stand on 3 levels. 10 meets 50, 30 and 20, one a level, 3 comparisons, the most 3 levels allow.

    why it is allowed: Its walk goes down one level at each number it meets, and hangs below the last. So a tree of hh levels costs a put at most hh comparisons.

  2. the move: ask the fewest numbers, not the most levels

    the line:
    N(h), the fewest in h levels, isN(h−1)+N(h−2)+1\begin{gathered} N(h) \text{, the fewest in } h \text{ levels, is} \\ N(h - 1) + N(h - 2) + 1 \end{gathered}
    1. step 33 of 37

      10203040506070

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

      numbers compared: 3

    2. step 34 of 37

      10203040506070

      At 20, the left side is one level high and the right side empty, so it is balanced.

      numbers compared: 3

    3. step 35 of 37

      10203040506070

      At 30, the left side is 2 levels high and the right side one level high, so it is balanced.

      numbers compared: 3

    4. step 36 of 37

      10203040506070

      At 50, the left side is 3 levels high and the right side 2 levels high, so it is balanced.

      numbers compared: 3

    The thinnest tree of 4 levels holds 7 numbers. Climbing back from 10, at 20, 30 and 50 the left side is one level higher than the right.
    levelsfewest numbersone more2h/22^{h/2}
    1121.41
    2232
    3452.83
    4784
    512135.66
    620218
    Each fewest is the two above it added, plus 1. One more than each stays ahead of the last column, as line 5 shows.

    why it is allowed: The top has one side of h−1h - 1 levels, or the tree would be shorter. The other side is within one level, so at least h−2h - 2. Each side holds fewest when it too is a thinnest tree, and the 1 is the top. So 1, 2, then 2 + 1 + 1, which is 4, then 7, then 12.

  3. the move: add 1 to both

    the line:
    N(h)+1 is (N(h−1)+1)plus (N(h−2)+1)\begin{gathered} N(h) + 1 \text{ is } \bigl(N(h - 1) + 1\bigr) \\ \text{plus } \bigl(N(h - 2) + 1\bigr) \end{gathered}

    why it is allowed: Add 1 to line 2 and share the 2 out, one to each bracket. In the table, 13 is 8 plus 5.

  4. the move: at least double, two levels down

    the line:
    N(h)+1 is at least2(N(h−2)+1)\begin{gathered} N(h) + 1 \text{ is at least} \\ 2\bigl(N(h - 2) + 1\bigr) \end{gathered}

    why it is allowed: By line 2, a thinnest tree of h−1h - 1 levels holds a thinnest tree of h−2h - 2 and more, so line 3's first bracket is at least its second. In the table, 13 is at least twice 5.

  5. the move: climb from the start

    the line:
    N(h)+1 is at least 2h/2\begin{gathered} N(h) + 1 \text{ is at least } 2^{h/2} \end{gathered}

    why it is allowed: It holds at 1 and 2 levels, as the table's first rows show. By line 4, two levels more at least double the left side and exactly double the right. So it holds at every hh: the climb of the page on proof by induction.

  6. the move: turn it round

    the line:
    m numbers stand on atmost 2 log⁡2(m+1) levels\begin{gathered} m \text{ numbers stand on at} \\ \text{most } 2\,\log_2(m + 1) \text{ levels} \end{gathered}

    why it is allowed: A tree of mm numbers on hh levels holds at least N(h)N(h), so m+1m + 1 is at least 2h/22^{h/2} by line 5. So half of hh is at most log⁡2(m+1)\log_2(m + 1), by the page on halving and logarithms, and hh at most twice that. For 12 numbers, at most about 7.4 levels; the table's thinnest has 5.

  7. the move: price put k

    the line:
    put k costs at most2 log⁡2 k comparisons\begin{gathered} \text{put } k \text{ costs at most} \\ 2\,\log_2\, k \text{ comparisons} \end{gathered}

    why it is allowed: Put kk goes into a tree holding one number fewer than kk. So by line 6, with that number as mm, m+1m + 1 is kk, and it has at most 2 log⁡2 k2\,\log_2\, k levels. By line 1, at most that many comparisons. Put 7 goes into 6 numbers: at most about 5.6 levels.

  8. the move: add the puts

    the line:
    U=2 log⁡2 1+2 log⁡2 2+⋯+2 log⁡2 n\begin{gathered} U = 2\,\log_2\, 1 + 2\,\log_2\, 2 \\ + \dots + 2\,\log_2\, n \end{gathered}

    why it is allowed: UU adds line 7's ceiling over the nn puts. Put 1 costs 0, which is 2 log⁡2 12\,\log_2\, 1. For 7, about 24.6.

  9. the move: read it

    the line:
    no order goes past U\begin{gathered} \text{no order goes past } U \end{gathered}
    numbersorders trieddearestceiling UU
    5120813.81
    67201118.98
    75,0401424.60
    840,3201830.60
    Every order of 1 to n tried. The dearest always lies under the ceiling.

    why it is allowed: Lines 1 to 8 hold for every order. The table tries every order of short rows, and the dearest stays well under UU.

  10. the move: a simpler ceiling

    the line:
    U≤2n log⁡2 nU \le 2n\,\log_2\, n

    why it is allowed: Each of the nn terms of line 8 is at most the last, 2 log⁡2 n2\,\log_2\, n.

  11. the move: read it as a bound

    the line:
    U∈O(n log⁡2 n)U \in O(n \,\log_2\, n)

    why it is allowed: By line 10, UU stays under twice n log⁡2 nn \,\log_2\, n.

try it with a small nn: at n=7n = 7, line 8 gives UU, about 24.6

this tests where the formula comes from, on the learn page

what a full answer includes
  • 1, 2, 4, 7, 12 and 20.
  • No: 6 levels need at least 20 numbers.
  • Line 6 gives about 7.4, a looser ceiling than 5.