This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA

Download & View **Av 1** as PDF for free.

**Words:**681**Pages:**7

INTRODUCTION

As we know that searching in a binary search tree is efficient if the height of the left sub-tree and right sub-tree is same for a node. But frequent insertion and deletion in the tree affects the efficiency and makes a binary search tree inefficient. The efficiency of searching will be ideal if the difference in height of left and right sub-tree with respect of a node is at most one. Such a binary search tree is called balanced binary tree (sometimes called AVL Tree).

REPRESENTATION OF AVL TREE

In order to represent a node of an AVL Tree, we need four fields : - One for data, two for storing address of left and right child and one is required to hold the balance factor. The balance factor is calculated by subtracting the right sub-tree from the height of left sub-tree. The structure of AVL Tree can be represented by : Struct AVL { struct AVL *left; int data; struct AVL *right; int balfact; };

DETERMINATION OF BALANCE FACTOR

The value of balance factor may be –1, 0 or 1. Any value other than these represent that the tree is not an AVL Tree.

1.If the value of balance factor is –1, it shows that the height of right sub-tree is one more than the height of the left sub-tree with respect to the given node. 2.If the value of balance factor is 0, it shows that the height of right sub-tree is equal to the height of the left Sub-tree with respect to the given node. 3.If the value of balance factor is 1, it shows that the height of right sub-tree is one less than the height of the left sub-tree with respect to the given node.

INVENTION AND DEFINITION

It was invented in the year 1962 by two Russian mathematicians named G.M. Adelson-Velskii and E.M. Landis and so named AVL Tree. It is a binary tree in which difference of height of two sub-trees with respect to a node never differ by more than one(1).

INSERTION OF A NODE IN AVL TREE

Insertion can be done by finding an appropriate place for the node to be inserted. But this can disturb the balance of the tree if the difference of height of sub-trees with respect to a node exceeds the value one. If the insertion is done as a child of non-leaf node then it will not affect the balance, as the height doesn’t increase. But if the insertion is done as a child of leaf node, then it can bring the real disturbance in the balance of the tree. This depends on whether the node is inserted to the left sub-tree or the right sub-tree, which in turn changes the balance factor. If the node to be inserted is inserted as a node of a sub-tree of smaller height then there will be effect. If the height of both the left and right sub-tree is same then insertionto any of them doesn’t affect the balance of AVL Tree. But if it is inserted as a node of sub-tree of larger height, then the balance will be disturbed. To rebalance the tree, the nodes need to be properly adjusted. So, after insertion of a new node the tree is traversed starting from the new node to the node where the balance has been disturbed. The nodes are adjusted in such a way that the balance is regained. ALGORITHM FOR INSERTION ALGORITHM FOR REBALANCING

ALGORITHM FOR DELETION IN AVL TREE

A node in AVL Tree is deleted as it is deleted in the binary search tree. The only difference is that we have to do rebalancing which is done similar to that of insertion of a node in AVL Tree.The algorithm for deletion and rebalancing is given below:

ALGORITHM FOR DELETION ALGORITHM FOR REBALANCING

REBALANCING OF AVL TREE

When we insert a node to the taller sub-tree, four cases arise and we have different rebalancing methods to bring it back to a balanced tree form. 1.Left Rotation 2.Right Rotation 3.Right and Left Rotation 4.Left and Right Rotation

As we know that searching in a binary search tree is efficient if the height of the left sub-tree and right sub-tree is same for a node. But frequent insertion and deletion in the tree affects the efficiency and makes a binary search tree inefficient. The efficiency of searching will be ideal if the difference in height of left and right sub-tree with respect of a node is at most one. Such a binary search tree is called balanced binary tree (sometimes called AVL Tree).

REPRESENTATION OF AVL TREE

In order to represent a node of an AVL Tree, we need four fields : - One for data, two for storing address of left and right child and one is required to hold the balance factor. The balance factor is calculated by subtracting the right sub-tree from the height of left sub-tree. The structure of AVL Tree can be represented by : Struct AVL { struct AVL *left; int data; struct AVL *right; int balfact; };

DETERMINATION OF BALANCE FACTOR

The value of balance factor may be –1, 0 or 1. Any value other than these represent that the tree is not an AVL Tree.

1.If the value of balance factor is –1, it shows that the height of right sub-tree is one more than the height of the left sub-tree with respect to the given node. 2.If the value of balance factor is 0, it shows that the height of right sub-tree is equal to the height of the left Sub-tree with respect to the given node. 3.If the value of balance factor is 1, it shows that the height of right sub-tree is one less than the height of the left sub-tree with respect to the given node.

INVENTION AND DEFINITION

It was invented in the year 1962 by two Russian mathematicians named G.M. Adelson-Velskii and E.M. Landis and so named AVL Tree. It is a binary tree in which difference of height of two sub-trees with respect to a node never differ by more than one(1).

INSERTION OF A NODE IN AVL TREE

Insertion can be done by finding an appropriate place for the node to be inserted. But this can disturb the balance of the tree if the difference of height of sub-trees with respect to a node exceeds the value one. If the insertion is done as a child of non-leaf node then it will not affect the balance, as the height doesn’t increase. But if the insertion is done as a child of leaf node, then it can bring the real disturbance in the balance of the tree. This depends on whether the node is inserted to the left sub-tree or the right sub-tree, which in turn changes the balance factor. If the node to be inserted is inserted as a node of a sub-tree of smaller height then there will be effect. If the height of both the left and right sub-tree is same then insertionto any of them doesn’t affect the balance of AVL Tree. But if it is inserted as a node of sub-tree of larger height, then the balance will be disturbed. To rebalance the tree, the nodes need to be properly adjusted. So, after insertion of a new node the tree is traversed starting from the new node to the node where the balance has been disturbed. The nodes are adjusted in such a way that the balance is regained. ALGORITHM FOR INSERTION ALGORITHM FOR REBALANCING

ALGORITHM FOR DELETION IN AVL TREE

A node in AVL Tree is deleted as it is deleted in the binary search tree. The only difference is that we have to do rebalancing which is done similar to that of insertion of a node in AVL Tree.The algorithm for deletion and rebalancing is given below:

ALGORITHM FOR DELETION ALGORITHM FOR REBALANCING

REBALANCING OF AVL TREE

When we insert a node to the taller sub-tree, four cases arise and we have different rebalancing methods to bring it back to a balanced tree form. 1.Left Rotation 2.Right Rotation 3.Right and Left Rotation 4.Left and Right Rotation