AVL Trees

CSC 385 - Data Structures and Algorithms

Brian-Thomas Rogers

University of Illinois Springfield

College of Health, Science, and Technology

Objectives

Objectives

  • Know what balance means in the context of binary search trees
  • Understand the balancing rules for an AVL Trees
  • Know the algorithms for performing the four different rotations
  • Know that the worst-case running times for an unbalanced tree are \(O(n)\) but \(O(log n)\) for a properly balanced tree

AVL Tree Basics

AVL Tree Basics

  • Created by Adelson-Velsky and Landis
  • AVL Trees are balanced Binary Search Trees using the following property
    • For any given node in an AVL tree, the heights of the node’s left and right subtrees may differ by at most 1
  • If the ballance of a binary tree is lost after adding or removing items the AVL Tree rearranges the nodes by rotation to keep maintain balance

Rotations

Imbalance Cases

  • There are 4 cases when a node X becomes imbalanced
    • Case 1: Imbalance is in the left subtree of X’s left child
    • Case 2: Imbalance is in the right subtree of X’s right child
    • Case 3: Imbalance is in the right subtree of X’s left child
    • Case 4: Imbalance is in the left subtree of X’s right child

Imbalance Case 1

The imbalance is fixed with a right rotation

  • Algorithm for right rotation about N
    1. C = N.left
    2. N.left = C.right
    3. C.right = N

Imbalance Case 1 Initial Tree

G 10 10 6 6 10->6 15 15 10->15 3 3 6->3 8 8 6->8 12 12 15->12 18 18 15->18 2 2 3->2 4 4 3->4 7 7 8->7 9 9 8->9

Imbalance Case 1 Rotation

G 10 10 6 6 10->6 15 15 10->15 10:e->15:ne rotate 6:nw->10:w rotate 3 3 6->3 8 8 6->8 12 12 15->12 18 18 15->18 2 2 3->2 4 4 3->4 8->10 attach as left subtree 7 7 8->7 9 9 8->9 1 1 2->1
  • Add node 1 to the tree
  • The imbalance is detected by the root node 10
    • Left subtree height is 4 while right subtree height is 2
    • The imbalance is in 10’s left child’s left subtree.
  • A right rotation happens about the root

Imbalance Case 1 After Rotation

G 6 6 3 3 6->3 10 10 6->10 2 2 3->2 4 4 3->4 8 8 10->8 15 15 10->15 1 1 2->1 7 7 8->7 9 9 8->9 12 12 15->12 18 18 15->18
  • Height of both left and right subtrees are now 3
  • The tree is balanced

Imbalance Case 2

The fix for this case is by using a left rotation

  • Algorithm for left rotation about node N
    1. C = N.right
    2. N.right = C.left
    3. C.left = N

Imbalance Case 2 Initial Tree

G 10 10 6 6 10->6 20 20 10->20 3 3 6->3 8 8 6->8 17 17 20->17 25 25 20->25 15 15 17->15 18 18 17->18 22 22 25->22 30 30 25->30

Imbalance Case 2 Rotation

G 10 10 6 6 10->6 10:w->6:nw rotate 20 20 10->20 3 3 6->3 8 8 6->8 20:ne->10:e rotate 17 17 20->17 25 25 20->25 17:nw->10:s attach 15 15 17->15 18 18 17->18 22 22 25->22 30 30 25->30 40 40 30->40
  • Add node 40 to the tree
  • The imbalance is detected by the root node 10
    • Left subtree has height 2 while right subtree has height 4
    • The imbalance is in 10’s right child’s right subtree
  • A left rotation happens about the root

Imbalance Case 2 After Rotation

G 20 20 10 10 20->10 25 25 20->25 6 6 10->6 17 17 10->17 22 22 25->22 30 30 25->30 3 3 6->3 8 8 6->8 15 15 17->15 18 18 17->18 40 40 30->40
  • The height of the left and right subtree at the root is 3.

Imbalance Case 3

  • The imbalance of case 3 is fixed by doing double rotations
    • First, perform a left rotation on the imbalanced child
    • Then, perform a right rotation on the parent

Imbalance Case 3 Initial Tree

G 12 12 5 5 12->5 15 15 12->15 3 3 5->3 8 8 5->8 13 13 15->13 18 18 15->18 2 2 3->2 4 4 3->4 7 7 8->7 9 9 8->9

Imbalance Case 3 First Rotation

G 12 12 5 5 12->5 15 15 12->15 3 3 5->3 5:w->3:nw rotate 8 8 5->8 13 13 15->13 18 18 15->18 2 2 3->2 4 4 3->4 8:ne->5:e rotate 7 7 8->7 9 9 8->9 7->5 attach 10 10 9->10
  • Tree becomes imbalanced when 10 is added
  • Imbalance occurs in 12’s left child’s right subtree
    • Left Rotation occurs on the left child first
    • Result on next slide

Imbalance Case 3 Second Rotation

G 12 12 8 8 12->8 15 15 12->15 12:e->15:ne rotate 8:nw->12:w rotate 5 5 8->5 9 9 8->9 13 13 15->13 18 18 15->18 3 3 5->3 7 7 5->7 9->12 attach 10 10 9->10 2 2 3->2 4 4 3->4
  • Tree is still imbalanced so we do a right rotation about 12 now

Imbalance Case 3 After Rotations

G 8 8 5 5 8->5 12 12 8->12 3 3 5->3 7 7 5->7 9 9 12->9 15 15 12->15 2 2 3->2 4 4 3->4 10 10 9->10 13 13 15->13 18 18 15->18
  • After the last rotation the tree becomes balanced

Imbalance Case 4

  • The imbalance of case 4 is fixed by doing double rotations
    • First, perform a right rotation on the imbalanced child
    • Then, perform a left rotation on the parent

Imbalance Case 4 Initial Tree

G 12 12 5 5 12->5 20 20 12->20 3 3 5->3 8 8 5->8 15 15 20->15 30 30 20->30 14 14 15->14 17 17 15->17 25 25 30->25 40 40 30->40

Imbalance Case 4 Right Rotation

G 12 12 5 5 12->5 20 20 12->20 3 3 5->3 8 8 5->8 15 15 20->15 30 30 20->30 20:e->30:ne rotate 15:w->20:w rotate 14 14 15->14 17 17 15->17 25 25 30->25 40 40 30->40 17->20 attach 18 18 17->18
  • Tree imbalanced after adding 18
    • Perform right rotation about 20
    • Result on next page

Imbalance Case 4 Left Rotation

G 12 12 5 5 12->5 12:w->5:nw rotate 15 15 12->15 3 3 5->3 8 8 5->8 15:ne->12:e rotate 14 14 15->14 20 20 15->20 14->12 attach 17 17 20->17 30 30 20->30 18 18 17->18 25 25 30->25 40 40 30->40
  • Tree is stil imbalanced so now we do a left rotation

Imbalance Case 4 After Rotations

G 15 15 12 12 15->12 20 20 15->20 5 5 12->5 14 14 12->14 17 17 20->17 30 30 20->30 3 3 5->3 8 8 5->8 18 18 17->18 25 25 30->25 40 40 30->40
  • Tree is balanced after performing both rotations

Notes

  • The examples have the rotation in the root of the tree but this is not always the case
  • Here is an example where a rotation happens but not on root
G 10 10 5 5 10->5 20 20 10->20 30 30 20->30 40 40 30->40 add
  • Here, the imbalance occurs in 20’s right child’s right subtree after adding 40
  • This requires a left rotation about 20
  • Root will then have a balance of left height 1, right height 2 after the rotation

AVL Tree Implementation

AVL Tree Implementation

  • Similar to the binary search tree with the following additions
    • The TreeNode class now has additional properties to keep track of the height of the subtrees
    • There are 2 additional methods for the rotations, a method to rebalance, and utility methods to fix the height of a node and a method to calculate the balance of a subtree
    • The add and remove must include the balance correction mechanism
    • The add and remove becomes a two-pass algorithm
      • First pass either adds or removes a node by traversing down the tree
      • Second pass corrects imbalances by backtracking the path taken and fixing the height property of each node and correcting any imbalances that occur

Practice Questions

Question 1

  • What rotation/rotations need to be performed on the following tree after John is added?
G Tania Tania Ellie Ellie Tania->Ellie John John Ellie->John add

Answer 1

  • Working backwards from John we see Ellie is balanced with left height 0 and right height 1 but Tania is imbalanced with left height 2 and right height 0.
  • Where does the imbalance occur?
    • Tania’s left child’s right subtree
    • This is case 3 so a left rotation then right rotation
G Tania Tania Ellie Ellie Tania->Ellie Ellie:w->leftE:nw rotate John John Ellie->John John:ne->Ellie:e rotate

Perform Left Rotation about Ellie

G Tania Tania John John Tania->John Tania:e->rightT:ne Rotate John:nw->Tania:w Rotate Ellie Ellie John->Ellie

Rotate right about Tania

Answer 1 Complete

G John John Ellie Ellie John->Ellie Tania Tania John->Tania

Practice Question 2

  • What rotation/rotations need to be performed to balance the following Tree after adding Nat?
G Frank Frank Ellie Ellie Frank->Ellie Oscar Oscar Frank->Oscar Matt Matt Oscar->Matt Pat Pat Oscar->Pat Nat Nat Matt->Nat add

Answer 2

  • Working backwards from Nat we see that Matt is balanced, Oscar is balanced, but Frank is not
  • Where does the imbalance occur?
    • Frank’s right child’s left subtree
    • This is case 4 so a right rotation and then a left rotation
G Frank Frank Ellie Ellie Frank->Ellie Oscar Oscar Frank->Oscar Matt Matt Oscar->Matt Pat Pat Oscar->Pat Oscar:e->Pat:ne rotate Matt:nw->Oscar rotate Nat Nat Matt->Nat Nat->Oscar attach

Perform Left Rotation about Oscar

G Frank Frank Ellie Ellie Frank->Ellie Frank:w->Ellie:nw rotate Matt Matt Frank->Matt Matt:ne->Frank:e rotate Oscar Oscar Matt->Oscar Nat Nat Oscar->Nat Pat Pat Oscar->Pat

Rotate right about Frank

Answer 2 Complete

G Matt Matt Frank Frank Matt->Frank Oscar Oscar Matt->Oscar Ellie Ellie Frank->Ellie Nat Nat Oscar->Nat Pat Pat Oscar->Pat

End