21 Lecture
CS301
Midterm & Final Term Short Notes
AVL Tree Building Example
To build an AVL Tree, we start with an empty tree and add nodes to it one by one. For each insertion, we perform the necessary rotations to maintain balance. Here's an example: Insert nodes 5, 2, 8, 1, 3, 6, 9 into an empty AVL Tree: Inser
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is the first node added to the AVL Tree in the building example? a) 1 b) 2 c) 5 d) 8
Answer: c) 5
- What is the second node added to the AVL Tree in the building example? a) 1 b) 2 c) 3 d) 8
Answer: b) 2
- How many rotations are performed to maintain balance after inserting node 1 in the AVL Tree? a) 0 b) 1 c) 2 d) 3
Answer: b) 1
- What is the height of the AVL Tree after inserting node 3? a) 1 b) 2 c) 3 d) 4
Answer: b) 2
- Which rotation is performed after inserting node 6 to maintain balance in the AVL Tree? a) Left rotation b) Right rotation c) Left-right rotation d) Right-left rotation
Answer: a) Left rotation
- What is the height of the AVL Tree after inserting node 9? a) 3 b) 4 c) 5 d) 6
Answer: b) 4
- What is the root node of the AVL Tree after inserting all the nodes? a) 1 b) 2 c) 5 d) 8
Answer: c) 5
- Which is the last node added to the AVL Tree in the building example? a) 6 b) 8 c) 9 d) 3
Answer: c) 9
- What is the maximum height of an AVL Tree with 7 nodes? a) 2 b) 3 c) 4 d) 5
Answer: b) 3
- How many rotations are performed in total to maintain balance while building the AVL Tree in the example? a) 2 b) 3 c) 4 d) 5
Answer: c) 4
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is an AVL Tree and what is its significance in computer science?
Answer: An AVL Tree is a self-balancing binary search tree in which the heights of the left and right subtrees of any node differ by at most one. Its significance in computer science lies in its ability to maintain efficient search, insertion and deletion operations with a guaranteed time complexity of O(log n).
- What is the difference between a balanced binary search tree and an unbalanced binary search tree?
Answer: A balanced binary search tree maintains a balance between the height of the left and right subtrees of any node, ensuring a time complexity of O(log n) for its operations. An unbalanced binary search tree, on the other hand, can have a skewed structure that results in a time complexity of O(n) for its operations.
- How does the AVL Tree maintain balance during insertion and deletion operations?
Answer: The AVL Tree maintains balance during insertion and deletion operations by performing rotations at the nodes where the balance factor (the difference between the heights of the left and right subtrees) is greater than one. The rotations are performed to move the subtrees and maintain the balance factor within the acceptable range.
- What is the height of a perfectly balanced AVL Tree with 10 nodes?
Answer: The height of a perfectly balanced AVL Tree with 10 nodes would be 4.
- What is the difference between a single rotation and a double rotation in an AVL Tree?
Answer: A single rotation is performed to balance an AVL Tree when the balance factor of a node is greater than one, and it involves rotating the node and its subtree once. A double rotation, on the other hand, involves performing two rotations to balance the tree, either in the same or opposite directions.
- Can a binary search tree be both height-balanced and weight-balanced?
Answer: Yes, a binary search tree can be both height-balanced and weight-balanced. AVL Trees are an example of a height-balanced binary search tree, while Red-Black Trees are an example of a weight-balanced binary search tree.
- Why is it important to maintain balance in a binary search tree?
Answer: It is important to maintain balance in a binary search tree to ensure that the search, insertion and deletion operations have a guaranteed time complexity of O(log n), making them efficient for large datasets.
- What is the time complexity of searching for a node in an AVL Tree?
Answer: The time complexity of searching for a node in an AVL Tree is O(log n).
- Can an AVL Tree have duplicate nodes?
Answer: Yes, an AVL Tree can have duplicate nodes, but they would need to be stored in a specific way, such as storing a count for each duplicate node.
- What is the root node of an AVL Tree?
Answer: The root node of an AVL Tree is the topmost node in the tree, from which all other nodes are descended.
20
/ \
15 30
/ / \
10 25 40
20
/ \
15 35
/ / \
10 30 40
/
25