0% found this document useful (0 votes)
20 views

Assignment 4 Data

Uploaded by

Moh Ashour
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Assignment 4 Data

Uploaded by

Moh Ashour
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 4

Submitted by: Mohamed Hamad Ashour


ID: 22120631
Course Name: Data Structure and Algorithms
Submitted to: Dr. Hasan & Eng. Asmaa Thabet & Eng. Hagar
Date of Submitting: 12/17/2024

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).

Q3 What is the balance factor of a node in an AVL tree?


The balance factor of a node is the difference between the heights of its left and right subtrees.
It can have values of -1, 0, or 1.

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).

Q6 What is the purpose of rotations in an AVL tree?


Rotations help maintain the balance of the tree after insertions or deletions.

Q7 What information is missing from the node structure of an AVL tree?


The missing details typically include the height of the node and pointers to its left and right child
nodes.

Q8 What type of rotation is needed for a left-right imbalance in an AVL tree?


A left-right imbalance requires a double rotation: first a left rotation, followed by a right
rotation.

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;

y->height = max(height(y->left), height(y->right)) + 1;


x->height = max(height(x->left), height(x->right)) + 1;

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.

Q13 What rotation is needed for a right-left imbalance in an AVL tree?


A right-left imbalance requires a double rotation: first a right rotation, followed by a left
rotation.

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;

x->height = max(height(x->left), height(x->right)) + 1;


y->height = max(height(y->left), height(y->right)) + 1;

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.

Q18 How is the height of a node in an AVL tree calculated?


The height of a node is determined as 1 plus the maximum height of its left and right subtrees.

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.

Q21 What is the balance factor of an empty tree?


The balance factor of an empty tree is 0.

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.

Q23 What properties ensure an AVL tree remains balanced?


The height difference between the left and right subtrees of any node must not exceed one.

Q24 How do you handle deleting a node from an AVL tree?


After deletion, the balance factor of each node is checked, and necessary rotations are
performed to restore balance.
Q25 Write the C++ function to update the height of a node in an AVL tree.
void updateHeight(Node* node) {
node->height = max(height(node->left), height(node->right)) + 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

Q29 What is the maximum height of an AVL tree with 7 nodes?


The maximum height is 3.

Q30

Q31
Q32

Q33

Q34

Q35 Which of the following is not AVL tree


B
Q36

Q37

Q38

You might also like