← Back to Home

CITS2200 - Lecture 15
AVL Trees - Slides

AVL Trees

  • An AVL tree is a binary search tree (BST)
  • Every internal nodes’ children can only differ in height by at most 1
    • If this condition is not met, the tree is unbalanced
  • Height of an AVL tree is

⚡ Named after inventors Adelson-Velsky & Landis (1962)


Insertion & Imbalance

  • Always done by expanding an external node
  • When a node is added, it may lead to the tree being unbalanced
  • The tree then needs to be restructured

  • The tree on the right is unbalanced:
    • Node 78: , but Node 17:
    • difference between siblings
    • tree is unbalanced

Restructuring process:

  • Identify problem nodes:
    • highest unbalanced node → node 78
    • child of with the larger height → node 50
    • child of with the larger height → node 62

Trinode Restructuring

  • To restore balance, we perform rotations
  • Let be an inorder listing of
    • Inorder listing Left-Root-Right
    • node will become parent node for and
  • Two main scenarios:
    • Single rotation form a straight line
      Left rotation about :
    • Double rotation form a zig-zag
  • Only one rebalancing step is required for insertion

Removal

Initial step do regular BST deletion:

  • Case A: Node is a leaf → delete node
  • Case B: Node has one child → replace node with child
  • Case C: Node has two children
    • Replace node with in-order successor
      • In-order successor is smallest node in right subtree for BST
    • Then, delete that successor

Retracing and Rebalancing

  • After node is gone, retrace path from deleted node’s parent up to root
  • Check each nodes balance factor BF:
  • If , the node is unbalanced
  • Four rotations we can perform to fix this, as seen in the table below

AVL Tree Performance

  • Single restructure
  • Searching
  • Insertion
  • Removal

CITS2200 - Lecture 17