The AVL Tree is a binary search tree that has an additional balance condition. This balance condition must be easy to maintain and ensures that the depth of the tree is O(logN). The simplest idea is to require that the left and right sub-trees have the same height. Recursion dictates that this idea applies to all nodes in the tree, since each node is itself a root of some sub-tree.
AVL is a height balanced tree. A height balanced tree is one in which the difference in the height of the two sub-trees for any node is less than or equal to some specified amount.
Balance Factor
Balance is defined by height of left sub-tree - height of right sub-tree. For an AVL tree the value of the balance factor of any node is -1, 0, or 1. If it is other than these three values then the tree is not balanced.
Insertion In AVL Tree
We can insert a new node in an AVL tree by finding its appropriate position similar to that of the binary search tree.
- If the data item with key 'k' is inserted into an empty AVL tree, then the node with key 'k' is set to be the root node.
- If tree contains only single node i.e. root node then the insertion of the node with key 'k' depends upon its value. If the value of 'k' is less than the key value of the root then it is inserted to the left of the root otherwise right of the root.
- If on inserting the node with key 'k' the height of the right sub-tree of the root has increased. Similarly if on inserting a node with key 'k' the height of the left sub-tree of the root is increased.
LL Rotation: The new node 'x' is inserted in the left sub-tree of root node whose(root) balance factor becomes +2 after insertion. Below in the Fig 2 we will see the LL rotation.
RR Rotation: Unbalance occurs due to insertion in the right sub-tree of the right child of the root node and balance factor of root node becomes -2.We will see the RR rotation in the following Fig 3.
LR Rotation: Unbalance occurred due to the insertion in the right sub-tree of the left child of the root node. We will clear this rotation in the following Fig 4.
RL Rotation: Unbalance occurred due to the insertion in the left subtree of the right child of the root node. We will clear this rotation in the following Fig 5.
Insertion In AVL Tree Using C
Deletion In AVL Tree
The deletion of a node from an AVL tree is exactly the same as the deletion of a node from the BST. The sequence of steps to be followed in deleting a node from an AVL tree is as follows:
- Initially, the AVL tree is searched to find the node to be deleted.
- The procedure used to delete a node in an AVL tree is the same as deleting the node in the binary search tree.
- After deletion of the node, check the balance factor of each node.
- Rebalance the AVL tree if the tree is unbalanced. In this situation we need some rotations.
R0 And L0 Rotation: When we delete a node and that node is present at the right sub-tree. In this situation th parent node of left sub-tree whose balance factor is 0 is rotated in right direction. Similarly for L0 rotation, the deleted node is present at left sub-tree of root node. We will see the rotation in following Fig 1.
R1 And L1 Rotation: It is same as R0 or L0 rotation. Only the difference is that balance factor of parent node of left sub-tree or right sub-tree is +1 here.We will see the rotation in following Fig 2.
R(-1) And L(-1) Rotation: In this case balance factor of parent node is -1. we will see the rotation in following Fig 3.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.