Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Department of

Computer Science
and Engineering
ADVACNED OBJECT ORIENTED
PROGRAMMING
22CS2116AA
Topic:

AVL TREES
Session - 2

CREATED BY K. VICTOR BABU


AIM OF THE
SESSION
To familiarize students with the basic concept of
AVL Trees

INSTRUCTIONAL
OBJECTIVES

This Session is designed to:


To know how to design the AVLTree class
To insert elements into an AVL tree
To implement node rebalancing
To delete elements from an AVL tree
LEARNING OUTCOMES

At the end of this session, you should be able to:


To implement the AVLTree class
To test the AVLTree class
To analyze the complexity of search, insert, and delete operations in AVL trees
CREATED BY K. VICTOR BABU
SESSION INTRODUCTION

AVL TREES:

AVL trees are well-balanced. AVL trees were invented by


two Russian computer scientists G. M. Adelson-Velsky and
E. M. Landis in 1962. In an AVL tree, the difference between
the heights of two subtrees for every node is 0 or 1. It can be
shown that the maximum height of an AVL tree is O(logn).

CREATED BY K. VICTOR BABU


SESSION INTRODUCTION

• An AVL Tree is a “height balanced Binary Search Tree”.


• “balancefactor” property is used to balance the BST height .
• The difference between the height of every left and right subtree of every
node in the AVL Tree is either {-1,0,1}.
• Tree needs to be balanced if the balance factor is not in the range of -1 to 1.
• Balance factor = height of Left Subtree – height of Right Subtree
If balance factor of any node is 1  Left Subtree is one level higher than the Right Subtree.
If balance factor of any node is 0  Left Subtree and Right Subtree are of equal height.
If balance factor of any node is -1  Left Subtree is one level lower than the Right Subtree.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION

BALANCE FACTOR/LEFT-HEAVY/RIGHT-
HEAVY

• The process for inserting or deleting an element in an AVL tree is the same as in a regular binary
search tree. The difference is that you may have to rebalance the tree after an insertion or deletion
operation. The balance factor of a node is the height of its right subtree minus the height of its left
subtree. A node is said to be balanced if its balance factor is -1, 0, or 1. A node is said to be left-
heavy if its balance factor is -1. A node is said to be right-heavy if its balance factor is +1.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

BALANCING TREES

• If a node is not balanced after an insertion or deletion operation, you need to rebalance it.
The process of rebalancing a node is called a rotation. There are four possible rotations.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)
LL IMBALANCE AND LL ROTATION
• LL Rotation: An LL imbalance occurs at a node A such that A
has a balance factor -2 and a left child B with a balance factor -
1 or 0. This type of imbalance can be fixed by performing a
single right rotation at A.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)
RR IMBALANCE AND RR ROTATION
• RR Rotation: An RR imbalance occurs at a node A such that A
has a balance factor +2 and a right child B with a balance factor
+1 or 0. This type of imbalance can be fixed by performing a
single left rotation at A.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

LR IMBALANCE AND LR ROTATION

• LR Rotation: An LR imbalance occurs at a node A such that A


has a balance factor -2 and a left child B with a balance factor
+1. Assume B’s right child is C. This type of imbalance can be
fixed by performing a double rotation at A (first a single left
rotation at B and then a single right rotation at A).

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)
RL IMBALANCE AND RL ROTATION
• RL Rotation: An RL imbalance occurs at a node A such that A
has a balance factor +2 and a right child B with a balance factor
-1. Assume B’s left child is C. This type of imbalance can be
fixed by performing a double rotation at A (first a single right
rotation at B and then a single left rotation at A).

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO
THE SESSION

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)
DESIGNING CLASSES FOR AVL TREES
• An AVL tree is a binary tree. So you can define the AVLTree class to extend
the BinaryTree class.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

Operations on AVL Trees:


All AVL Tree operations are similar to that of BST except insertion and deletion.
Upon every insertion and deletion, we need to check whether the tree is balanced or
not.
If required, the rotations are performed after each operation to balance the tree.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

AVL Tree Complexities:


AVL Tree never exceed log n height where as in BST it may vary from log n(normal tree)
to n (Skewed tree).

Algorithm Average Case Worst Case


Space O(n) O(n)

Insertion O(log n) O(log n)

Searching O(log n) O(log n)

Deletion O(log n) O(log n)

CREATED BY K. VICTOR BABU


EXAMPLES

Case Study on AVL Tree

H, I, J, B, A, E, C, F, D, G, K, L
40, 20, 10, 25, 30, 22, 50

CREATED BY K. VICTOR BABU


THANK YOU

Team – Course Name

CREATED BY K. VICTOR BABU

You might also like