An AVL tree is a balanced binary search tree where the height of the left and right sub-tree of every node differs by at most one. It was named after the inventors Adelson-Velsky and Landis.
When a node is inserted or deleted from an AVL tree, the tree is rebalanced by performing rotations. A rotation is a tree operation that preserves the BST property and balances the height of the tree.
Here’s an example of an AVL tree:
10
/ \
6 15
/ \ /
4 8 12
/ \
11 14
This tree is balanced because the height of the left sub-tree is 2 and the height of the right sub-tree is 3, which differs by at most 1.
If a new node is inserted with value 7, the tree would be rebalanced by performing a left rotation at node 6:
10
/ \
8 15
/ \ /
6 9 12
/
4 7
8
After the rotation, the tree is still balanced with the height of the left sub-tree at 2 and the height of the right sub-tree at 3.
What is an AVL tree?
Answer: AVL tree is a self-balancing binary search tree that maintains the balance factor between -1 to +1 in each node.
How does AVL tree maintain its balance?
Answer: AVL tree maintains its balance by performing rotation operations when the balance factor is greater than 1 or less than -1 in any node.
What is the time complexity of inserting a node in AVL tree?
Answer: The time complexity of inserting a node in an AVL tree is O(log n), where n is the total number of nodes in the tree.
Can two AVL trees have the same set of elements but different structures?
Answer: Yes, two AVL trees can have the same set of elements but different structures.
What is the height of an AVL tree with n nodes?
Answer: The height of an AVL tree with n nodes is O(log n).