Table of Contents
What is AVL tree explain how a node is inserted into an AVL tree and how a node is deleted from an AVL tree?
AVL Tree can be defined as height balanced binary search tree in which each node is associated with a balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree….Complexity.
Algorithm | Average case | Worst case |
---|---|---|
Insert | o(log n) | o(log n) |
Delete | o(log n) | o(log n) |
Which rotation is required while constructing the AVL from the given nodes?
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.
How deletion is performed in AVL?
Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced in order to maintain the AVLness. For this purpose, we need to perform rotations. The two types of rotations are L rotation and R rotation.
How many rotations are required during the construction of an AVL tree if the following elements are to be added in the order given?
Ans: C RL + LR + RR 2 LL rotations and 3 RR rotations are required.
What is the height of an AVL tree after insertion?
The height of an AVL tree is always O (Logn) where n is the number of nodes in the tree (See this video lecture for proof). To make sure that the given tree remains AVL after every insertion, we must augment the standard BST insert operation to perform some re-balancing.
What is the balance factor in AVL trees?
In AVL trees, after each operation like insertion and deletion, the balance factor of every node needs to be checked. If every node satisfies the balance factor condition, then the operation can be concluded. Otherwise, the tree needs to be rebalanced using rotation operations.
What is the difference between RL rotation and AVL tree?
In RL Rotation, first every node moves one position to right then one position to left from the current position. AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node can’t be more than 1.
What is the time complexity of an AVL insert?
Updating the height and getting the balance factor also takes constant time. So the time complexity of AVL insert remains same as BST insert which is O (h) where h is the height of the tree. Since AVL tree is balanced, the height is O (Logn). So time complexity of AVL insert is O (Logn).