CSCI C343 Data Structures Spring 2025

Review: AVL Invariant

Recall the definition of the AVL invariant

Definition The AVL Invariant: the height of two child subtrees may only differ by 1, for every node in the tree.

Tree Rotation

                y                         x
               / \    right_rotate(y)    / \
              x   C  --------------->   A   y
             / \     <-------------        / \
            A   B     left_rotate(x)      B   C

Rotations preserve the BST property and the in-order ordering.

A x B y C  =  A x B y C

Algorithm for fixing AVL property

Starting from the lowest changed node, repeat the following up to the root of the tree (because there can be several AVL violations).

Time Complexity of Insert and Remove for AVL Tree

  1. Add or remove using the BST algorithm: O(log n)

  2. Number of iterations of the fixing AVL: O(log n) How much time per fix: O(1) O(log n) * O(1) = O(log n)

Taotal for add/remove and rebalance: O(log n) + O(log n) = O(log n)

Using AVL Trees for sorting

  1. Insert n items: O(n log(n))

  2. In-order traversal: O(n)

Overall time complexity: O(n log(n))

ADT’s that can be implemented by AVL Trees

Does the AVL invariant ensure that the tree is balanced?

Let N(h) represent the minimum number of nodes in an AVL tree of height h. (The least-balanced possible scenario.)

N(h) = N(h-1) + N(h-2) + 1

We want to show that

h ≲ log₂(N(h))

Ok, let’s use the equation for N(h) and see if we can relate it to h.

N(h) = N(h-1) + N(h-2) + 1
     > N(h-2) + N(h-2) + 1
     = 2·N(h-2) + 1
     > 2·N(h-2)
     > 2·2·N(h-4)       because ∀ h. N(h) > 2·N(h-2)
     > 2·2·2·N(h-6)
       ...
     > 2^(h/2)

so

N(h) > 2^(h/2)

Take the log of both sides

      log₂(N(h)) > log₂(2^(h/2))
                              (log₂(Aᴮ) = B log₂(A))
                 = h/2 · log₂(2)
                              (log₂(2) = 1, because 2¹ = 2)
                 = h/2 · 1
                              (multiply both side by 2) 
2 · log₂(N(h)) > h

so we have

h ≲ log₂ N(h)