AVL Trees


Properties

An AVL tree is a BST where the height of every node's left and right subtrees differ by at most 1. Another way to define AVL trees uses recursion: an AVL tree is a BST where the height of the root's left and right subtrees differ by at most 1, and the left and right subtrees are AVL trees.

In an AVL tree each node contains balance and height info. Both height and balance info are calculated from the leaves up. THe height is calculated as:

     height(leaf) = 1
     height(internal node) = 1 + max(height(left child), height(right child))

The balance of a node is calculated as:

     balance(leaf) = 0
     balance(internal node) = height(left child) - height(right child))

Here is a sample AVL tree with height and balance information:

Rebalancing

Nodes are inserted and deleted using the same algorithm as a standard BST. After inserting or deleting, the tree may be out of balance. In this case the balance at some node will be 2 or -2, because the height of the subtrees differs by 2. After inserting or deleting, every node from the point of insertion/deletion up to the root has its height and balance recomputed. During this process, if a node's new balance is 2 or -2, it needs to be rebalanced.

Rebalancing is done through rotation. Rotation shortens the taller subtree so the balance is restored. There are 4 different types of imbalances based on combinations of left and right subtrees. If the balance at a node is 2, that means the left subtree is taller. This is called an L imbalance. If the balance at a node is -2, that means the right subtree is taller. This is called an R imbalance. The steps required to rebalance depend on which of its subtrees needs to be shortened.

LL Rotation

An LL rotation is performed at X when X has a balance of 2 and XL (the left child of X) has a balance of 1. The rotation performed is a single clockwise rotation around X by moving XL up a level.

  1. X becomes XL's right child.
  2. XL's right child (T2) becomes X's left child.
  3. XL is the new root of the subtree.

RR Rotation

An RR rotation is performed at X when X has a balance of -2 and XR (the right child of X) has a balance of -1. The rotation performed is a single counter-clockwise rotation around X by moving XR up a level.

  1. X becomes XR's left child.
  2. XR's left child (T2) becomes X's right child.
  3. XR is the new root of the subtree.

LR Rotation

An LR rotation is performed at X when X has a balance of 2 and XL (the left child of X) has a balance of -1. Two rotations are required: a counter-clockwise rotation around XL and then a clockwise rotation around X.

Rotation 1 (counter-clockwise rotation around XL moving XLR up a level):

  1. XLR becomes left child of X.
  2. XL becomes left child of XLR.
  3. XLR's left subtree (T2L) becomes right subtree of XL.
  4. XLR's right subtree (T2R) remains the right subtree of XLR.

Rotation 2 (clockwise rotation around X moving XLR up a level):

  1. X becomes XLR's right child.
  2. XLR's right child (T2R) becomes X's left child.
  3. XLR is the new root of the subtree.

RL Rotation

An RL rotation is performed at X when X has a balance of -2 and XR (the right child of X) has a balance of 1. Two rotations are required: a clockwise rotation around XR and then a counter-clockwise rotation around X.

Rotation 1 (clockwise rotation around XR moving XRL up a level):

  1. XRL becomes right child of X.
  2. XR becomes right child of XRL.
  3. XRL's right subtree (T2R) becomes left subtree of XR.
  4. XRL's left subtree (T2L) remains the left subtree of XRL.

Rotation 2 (counter-clockwise rotation around X moving XRL up a level):

  1. X becomes XRL's left child.
  2. XRL's left child (T2L) becomes X's right child.
  3. XRL is the new root of the subtree.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2010