Lecture 4 - AVL Trees
Lecture 4 - AVL Trees
By Ali Saeed
Introduction
• AVL Tree is an example of balanced binary
search tree
• Balanced binary tree is defined as: A binary tree
in which the left and right subtrees of every
node difference in height by no more than 1
• Examples:
– AVL Tree
– Red Black Tree
Definition
• In computer science, an AVL tree (named after
inventors Adelson-Velsky and Landis) is a self-
balancing binary search tree.
• Invented in 1962
• In an AVL tree, the heights of the
two child subtrees of any node differ by at most
one;
• if at any time they differ by more than one,
rebalancing is done to restore this property
Cont…
• Lookup, insertion, and deletion all
take O(log n) time in both the average and
worst cases, where n is the number of nodes
in the tree prior to the operation.
• Insertions and deletions may require the tree
to be rebalanced by one or more tree
rotations.
Cont…
• AVL trees are often compared with red–black
trees because both support the same set of
operations and take O(log n)
• Maximum possible number of nodes in AVL or
BST tree of height H
= 2H+1 – 1
Solve the problem of BST
Balance Factor
• Following formula is used to compute balance
factor of a node (N):
Balance factor = heightOfLeftSubtree - heightOfRightSubtree
• 1, 2, 3
Answer
Right Rotation
• AVL tree may become unbalanced, if a node is
inserted in the left subtree of the left subtree.
The tree then needs a right rotation.
Example