Assignment 4 Data
Assignment 4 Data
1
Q1 What is an AVL Tree?
An AVL Tree is a type of self-balancing binary search tree where the height difference between
the left and right subtrees of any node is at most one.
Q2 What are the key operations of an AVL Tree, and how do they differ from a standard binary
search tree?
The primary operations are insertion, deletion, and searching. Unlike a regular binary search
tree, AVL Trees perform rotations to maintain balance after insertions or deletions, ensuring
these operations remain efficient with a time complexity of O(log n).
Q4 Given the insertion sequence 10, 20, 30, what rotations are required to balance the AVL tree?
A single left rotation on the root node (10) is required to restore balance.
Q5 What is the time complexity for searching an element in an AVL tree with n nodes?
The search operation has a time complexity of O(log n).
Q9 What kind of imbalance is created by the insertion sequence 30, 20, 10 in an AVL tree?
This sequence produces a left-left imbalance, which can be corrected with a single right rotation.
Q10 Write the C++ function for a right rotation on a node in an AVL tree.
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
Q11
Q12 Is it possible for an AVL tree to become skewed like a linked list? Why or why not?
No, because AVL trees use rotations to preserve their balance, preventing them from becoming
skewed.
Q14 Write the C++ function for a left rotation on a node in an AVL tree.
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
Q15 What rotation is required if the root node has a balance factor of -2 and its right child has a
balance factor of -1?
A single left rotation on the root node is required.
Q16
Q17 True or False: Deleting a node from an AVL tree always requires a rotation.
False.
Q19 What is the main difference between insertions in a binary search tree and an AVL tree?
In an AVL tree, insertions may require rotations to maintain balance, unlike a regular binary
search tree.
Q20 If an AVL tree has a height of 5, what is the minimum number of nodes it can have?
The minimum number of nodes is 12.
Q22 What is the range of possible balance factors for any node in an AVL tree?
The balance factor can range from -1 to 1.
Q26 How do you verify if a given binary tree is a valid AVL tree?
Verify that the tree satisfies binary search tree properties and that the balance factor of each
node is within the range of -1 to 1.
Q27 How does an AVL tree maintain balance during insertion and deletion?
The tree performs rotations to ensure that the balance factor of all nodes remains between -1
and 1.
Q28
Q30
Q31
Q32
Q33
Q34
Q37
Q38