AVL Trees – CMPT 225

Recall: A BST is:

How unbalanced can an AVL Tree be?

Ex. A “Maximally unbalanced” height - 5 AVL Tree

How tall can an AVL Tree be?

Let N(h) = min # of nodes in an AVL tree of height h.

Observe:

Claim: N(h)>2h/2N(h) > 2^{h/2}

Proposition: N(h)>2h/2N(h) > 2^{h/2}

Max AVL Tree Height vs BST height

(Worst case # of nodes visited by AVL-tree vs BST search)

nn 2log2n2 \log_{2} n
1010 77
100100 1414
10001000 2020
10410^{4} 2727
10510^{5} 3333
10610^{6} 4040
10710^{7} 4747
10810^{8} 5353
10910^{9} 6060
101010^{10} 6666

Unbalanced subtrees are “repaired” using rotations

Can be converted to the following using a right rotation at node with 5. Can be converted from the following using a left rotation at the node with 3.

AVL Tree insertion:

  1. Do BST insertion.
  2. If there is an unbalanced node,
    • let v be the unbalanced node of greatest depth*
    • repair the imbalance at v.

Consider 4 cases (w is new node, v is unbalanced, k is height of tree, h is height of any given subtree):

2 outside cases:

Case 1:

Case 2:

Case 3:

Case 4:

* It msut be on the path from the new leaf to the root.

To fix the “ouside” cases:

Do 1 rotation at the unbalanced node.

After rotation:

* The final height of u is k, so the tree is now balanced.

Excersizes:

  1. Draw the right-right case in detail.
  2. Draw them with minimal sized T1,T2,T3

The “inside cases” are not fixed by this rotation:

Would become, with a right rotation:

(Transcriber’s note: w is now used as a variable by the slides. w will no longer represent the asdded node. This will be written down in full as “new node”.)

To fix the “inside” cases, we use two rotations:

Left rotation at b:

After 1 rotation, this is too tall (like outisde case

Right rotation at a:

The entire operation is:

  1. left(c) <- b
  2. right(c) <- a
  3. left(a) <- T3
  4. right(b) <- T2
  5. Change parent(a) to be parent(c)

(#1 rotation = 3 assignments; 2 rotations = 6 assignments; double rot = 5 assignments.)

AVL Tree Removal

  1. Do BST Removal.
  2. Rebalance.

Define “the parent of the unbalanced node” (*) by cases:

  1. The deleted key was at a leaf.
  2. The deleted key was at a node with one child.
  3. The deleted key is at a node with 2 children.

Case 1 graphs (X is removed node; p is parent of removed node):

Becomes:

Graphs for case 2:

Becomes:

Graphs for case 3:

Or:


Fact: After doing a BST removal in an AVL tree, there is at most 1 unbalanced node, and it is on the path from the parent of the deleted node to the root.

(If the deleted node was the root, the “parent of the deleted node” does not exist–but also there can be no unbalanced node)

Consider:

Becomes:

Terms:

An AVL tree removal that illustrates:

  1. Need to re-balance after removal
  2. Re-balancing node u may reduce the height of a subtree, resulting in an ancestor of u being unbalanced.

remove(14):

left at 4:

right at 10:

Abstract version:

Rebalance (for deletion):

* either a single or a double rotation, based on case diagrams similar to that used for insertion.

Correctness of the algorithm involves two properties:

  1. There is at most 1 unbalanced node after deletion.
  2. Rebalancing w may make an ancestor of w unbalanced.

Complexity of AVL tree operations

All three major operations in O(logn)O(\log n) time.

End