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

DSA-II UNIT II_ AVL TREE Introduction

An AVL tree is a self-balancing binary search tree where the height difference between left and right subtrees of any node is at most one. It utilizes rotations (single and double) to maintain balance during insertions, ensuring efficient operations. The balance factor of each node is calculated to determine if rotations are necessary for maintaining tree balance.

Uploaded by

Ms. Pavithra D
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)
2 views

DSA-II UNIT II_ AVL TREE Introduction

An AVL tree is a self-balancing binary search tree where the height difference between left and right subtrees of any node is at most one. It utilizes rotations (single and double) to maintain balance during insertions, ensuring efficient operations. The balance factor of each node is calculated to determine if rotations are necessary for maintaining tree balance.

Uploaded by

Ms. Pavithra D
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/ 15

DATA STRUCTURES AND ALGORITHMS-II

UNIT-II

AVL Tree
WHY AVL TREE?
🞆 What if the input to binary search tree comes
in a sorted (ascending or descending)
manner? It will then look like this
AVL TREE
🞆 AVL tree is a binary search tree in which the
difference of heights of left and right subtrees
of any node is less than or equal to one.
🞆 The technique of balancing the height of
binary trees was developed by Adelson,
Velskii, and Landi and hence given the short
form as AVL tree or Balanced Binary Tree.
🞆 The Balance factor of a node in a binary tree
can have value 1, -1, 0, depending on whether
the height of its left subtree is greater, less
than or equal to the height of the right
subtree.
AN AVL TREE CAN BE DEFINED AS
FOLLOWS:
🞆 Let T be a non-empty binary tree with TL and
TR as its left and right subtrees. The tree is
height balanced if:
🞆 TL and TR are height balanced
🞆 hL - hR <= 1, where hL - hR are the heights of
TL and TR

BalanceFactor = height(left-subtree) − height(right-subtree)


REPRESENTATION OF AVL TREE
Struct AVLNode
{
int data;
struct AVLNode *left,*right;
int balfactor;
};
AVL ROTATIONS
🞆 To balance itself, an AVL tree may perform
the following four kinds of rotations −
🞆 Single Rotation
⚫ Left rotation(LL Rotation)
⚫ Right rotation(RR Rotation)
🞆 Double Rotation
⚫ Left-Right rotation(LR Rotation)
⚫ Right-Left rotation(RL Rotation)
FOR INSERTION
🞆 Step 1: First, insert a new element into the tree
using BST's (Binary Search Tree) insertion logic.
🞆 Step 2: After inserting the elements you have
to check the Balance Factor of each node.
🞆 Step 3: When the Balance Factor of every node
will be found like 0 or 1 or -1 then the
algorithm will proceed for the next operation.
🞆 Step 4: When the balance factor of any node
comes other than the above three values then
the tree is said to be imbalanced. Then perform
the suitable Rotation to make it balanced and
then the algorithm will proceed for the next
operation.
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

In our example, node A has become unbalanced as a node is


inserted in the right subtree of A's right subtree. We perform
the left rotation by making A the left-subtree of B.
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.

🞆 As depicted, the unbalanced node becomes


the right child of its left child by performing a
right rotation.
LEFT-RIGHT ROTATION

🞆 Double rotations are slightly complex version


of already explained versions of rotations. To
understand them better, we should take note
of each action performed while rotation.
🞆 Let's first check how to perform Left-Right
rotation. A left-right rotation is a combination
of left rotation followed by right rotation.
A node has been inserted into the right
subtree of the left subtree. This
makes C an unbalanced node. These
scenarios cause AVL tree to perform left-
right rotation.

We first perform the left rotation on the left


subtree of C. This makes A, the left
subtree of B.
Node C is still unbalanced, however now, it
is because of the left-subtree of the left-
subtree.

We shall now right-rotate the tree,


making B the new root node of this
subtree. C now becomes the right subtree
of its own left subtree.

The tree is now balanced.


RIGHT-LEFT ROTATION

🞆 The second type of double rotation is Right-


Left Rotation. It is a combination of right
rotation followed by left rotation.
A node has been inserted into the left
subtree of the right subtree. This makes A,
an unbalanced node with balance factor 2.

First, we perform the right rotation


along C node, making C the right subtree
of its own left subtree B. Now, B becomes
the right subtree of A.
Node A is still unbalanced because of the
right subtree of its right subtree and
requires a left rotation.

A left rotation is performed by making B the


new root node of the subtree. A becomes
the left subtree of its right subtree B.

The tree is now balanced.

You might also like