0% found this document useful (0 votes)
118 views

Lecture 4 - AVL Trees

- AVL trees are self-balancing binary search trees where the heights of the two child subtrees of any node differ by at most one. - Rebalancing is done through rotations (left, right, left-right, right-left) to maintain this balancing property and keep lookup, insertion, and deletion operations efficient at O(log n) time. - The balance factor of a node, computed as the height of its left subtree minus the height of its right subtree, is used to determine which rotation is necessary to rebalance the tree.

Uploaded by

Arbaz Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Lecture 4 - AVL Trees

- AVL trees are self-balancing binary search trees where the heights of the two child subtrees of any node differ by at most one. - Rebalancing is done through rotations (left, right, left-right, right-left) to maintain this balancing property and keep lookup, insertion, and deletion operations efficient at O(log n) time. - The balance factor of a node, computed as the height of its left subtree minus the height of its right subtree, is used to determine which rotation is necessary to rebalance the tree.

Uploaded by

Arbaz Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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

• Balance factor should be in the following


range:
Balance Factor Example
Class Exercise
Compute the Balance Factor of give tree:
Class Exercise (Balance Factor)
AVL Rotations
• To balance itself, an AVL tree may perform the
following four kinds of rotations:
– Left rotation
– Right rotation
– Left-Right rotation
– Right-Left rotation
 
AVL Rotations
Left Rotation
• If a tree becomes unbalanced, when a node is
inserted into the right subtree of the right
subtree, then we perform a single left rotation
Example
• Add following values in AVL tree:

• 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

• Insert 3,2,1 in AVL Tree


Answer
Left-Right Rotation
• Double rotation is required in the case
• Following are steps:
Example
• Insert 3, 1, 2 in AVL tree
Answer
Right-Left Rotation
• Following are steps:
Example
• Insert 1,3,2 in AVL tree
Answer
Identify Rotation Type?
• Identify what kind of rotation is required in
the following examples:
– Insert 30,59,28
– Insert 130,69,111
– Insert 3,292,-28
– Insert -30,659,8
– Insert 30,59,200
• Thanks
• Questions?

You might also like