Assignment No. 9

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

W C D V T Total Dated

Assignment No. 9 (4) (4) (4) (4) (4) (20) Sign

Problem Definition:

Write a C++ program for implementation of AVL Tree.

6.1 Prerequisite:
Concepts of Trees.
Concepts of AVL technique of rotations.

6.2 Learning Objectives:


Understand the implementation AVL trees.
Understand what AVL Tree is.
Understand how to perform the rotations in AVL trees.

6.3 New Concepts:


Definition:-

An empty tree is height balanced tree if T is a non empty binary tree with TL and TR as its left
and right sub trees. The T is height balance if and only if
i) Its balance factor is 0, 1, -1.

a. AVL (Adelson-Velskii and Landis) Tree: A balance binary search tree. The best search
time, that is O (log N) search times. An AVL tree is defined to be a well-balanced binary search
tree in which each of its nodes has the AVL property. The AVL property is that the heights of the
left and right sub-trees of a node are either equal or if they differ only by 1.

1.4 Graphical Representation:


a.AVL-Tree
b. Height Balance Tree:

AVL tree is a height balance tree.

The height of the right sub tree and height of the left sub tree for any node cannot
differ by more than one.
This process is usually done through rotation.
Different Rotations:

1. Right-Right rotation:

Diagram 1:

A single counter clockwise rotate will balance it.


We have to perform right-right rotation because it involves the right-right sub tree.

2. Left-Left rotation:

Diagram 2:

A single counter clockwise rotate will balance it.


We have to perform left-left rotation because it involves the left-left sub tree.

3. Right-Left rotation:
Diagram 3:

A double clockwise rotate will balance it.


o 1st - single clockwise rotation with the parent of the inserted node (this produces
the tree in diagram 1)
o 2nd - single counter clockwise rotation on the root
Right-left rotation because it involves the right sub tree.

3. Left-Right rotation:

Diagram 4

A double clockwise rotate will balance it.


o 1st - single counter clockwise rotation with the parent of the inserted node (this
produces the tree in diagram 2)
o 2nd - single clockwise rotation with the root.
Left-Right rotation because it involves the left sub tree.

Operation on AVL Tree:-

6.5.1 Insertion of a node:-

a. Inserting in AVL tree is same as in binary search tree. Here also we will search
for the position where the new node is to be inserted and then insert the node.
b. To restore the property of AVL tree we should convert the tree in such a way that,
the new converted tree is balance tree i.e. the balance factor of each node should
be -1, 0, 1.
c. The new converted node should be a binary search tree with inorder traversals
same as that of original tree.
d. The outline of the procedure to insert of a node is as- insert node to its proper
place follow the same process as in binary search tree.
e. Calculate the balance factor of all the path starting from the inserted node to the
root node.
f. If the tree become unbalance after insertion then there is need to convert the
above tree by performing rotations.

6.5.2 Deletion of a node at any position:-

g. Read the node from the user which he wants to delete.


h. Find out the node position and delete the node.
i. Check for the balance factor.
j. If tree is imbalance then perform the rotations.
k. Stop.

Example:-

Given these data build an AVL tree.


12, 10, 3, 23, 11, 14, 2, 4, 1

6.7 Assignment Questions:-


1. What is AVL Tree?
2. What is Spanning Tree?
3. How to find minimum Spanning Tree?

6.8 Oral Question:-


1. What are the different rotations used in AVL trees?
2. Why AVL tree is called height balance tree?

You might also like