Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views5 pages

DSA Experiment 9

The document outlines the implementation of an AVL tree, a height-balanced binary search tree that ensures the balance factor between subtrees is no more than 1. It details the objectives, outcomes, and prerequisites for understanding AVL trees, along with the theory behind balancing through rotations. Additionally, it poses questions regarding AVL trees, their applications, and comparisons with other tree structures.

Uploaded by

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

DSA Experiment 9

The document outlines the implementation of an AVL tree, a height-balanced binary search tree that ensures the balance factor between subtrees is no more than 1. It details the objectives, outcomes, and prerequisites for understanding AVL trees, along with the theory behind balancing through rotations. Additionally, it poses questions regarding AVL trees, their applications, and comparisons with other tree structures.

Uploaded by

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

Group D Experiments

Experiment No.9

TITLE: Implementation of Adelson, Velskii, and Landi (AVL) tree

AIM/PROBLEM STATEMENT: Dictionary stores keywords & its meanings. Provide facil-
ity for adding new keywords, deleting keywords, updating values of any entry. Provide facility
to display whole data sorted in ascending/ Descending order. Also find how many maximum
comparisons may require for any keyword. Use Height balance tree and find the complexity for
finding a keyword.

OBJECTIVES:

1. To understand tree data structure.


2. To understand practical implementation and usage of non-linear data structures to solving prob-
lems of AVL Tree.

OUTCOMES:

1. Apply and analyze non-linear data structures to solve real world complex problems.

PRE-REQUISITES:

1. Knowledge of C++ programming.


2. Knowledge of AVL tree

THEORY:

It is observed that BST's worst-case performance is closest to linear search algorithms,


that is Ο(n). In real-time data, we cannot predict data pattern and their frequencies. So, a
need arises to balance out the existing BST.
Named after their inventor Adelson, Velski & Landis, AVL trees are height balancing binary
search tree. AVL tree checks the height of the left and the right sub-trees and assures that the
difference is not more than 1. This difference is called the Balance Factor.

Here we see that the first tree is balanced and the next two trees are not balanced −

In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the dif-
ference is 2. In the third tree, the right subtree of A has height 2 and the left is missing, so it is 0,
and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1.

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

If the difference in the height of left and right sub-trees is more than 1, the tree is balanced using
some rotation techniques.

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

The first two rotations are single rotations and the next two rotations are double rotations. To
have an unbalanced tree, we at least need a tree of height 2. With this simple tree, let's under-
stand them one by one.

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 un-
derstand 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.

State Action

A node has been inserted into the right subtree of the left subtree. This
makes Canunbalancednode.ThesescenarioscauseAVLtreetoperformlef-
t-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.

State Action

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 sub-
tree 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.

Aleftrotation isperformed by making B thenewrootnode of thesubtree. Ab-


ecomes the left subtree of its right subtree B.

The tree is now balanced.

QUESTIONS:
1. What is an AVL tree. Explain with the help of example. what are the applications of AVL
tree.
2. What is the difference between OBST, Huffman’s tree and AVL tree
3. Explain Single rotation and Double rotation with example?
4.Write down the time and space complexity of AVL Tree.

You might also like