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

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

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

  1. What is the height of the AVL Tree after inserting node 3? a) 1 b) 2 c) 3 d) 4

Answer: b) 2

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

  1. What is the height of the AVL Tree after inserting node 9? a) 3 b) 4 c) 5 d) 6

Answer: b) 4

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

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

  1. What is the maximum height of an AVL Tree with 7 nodes? a) 2 b) 3 c) 4 d) 5

Answer: b) 3

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

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

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

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

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

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

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

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

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

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

AVL Tree is a self-balancing binary search tree in which the difference between the heights of the left and right subtrees of any node is at most one. AVL Trees are commonly used for searching and storing large datasets. To build an AVL Tree, we first insert the nodes in a binary search tree manner. As we insert each node, we check the balance factor of the current node, which is the difference between the heights of the left and right subtrees. If the balance factor is greater than one, we perform rotations to maintain balance. For example, let's consider the following sequence of nodes: 30, 15, 10, 20, 40, 35, 50. We start by inserting the root node 30, followed by 15, which becomes the left child of 30. Next, we insert 10, which becomes the left child of 15, and then 20, which becomes the right child of 15. At this point, the balance factor of node 15 is greater than one, so we perform a right rotation to balance the tree. The tree now looks like this:
markdown
20 / \ 15 30 / / \ 10 25 40
We then insert 40, which becomes the right child of 30, and then 35, which becomes the left child of 40. The balance factor of node 30 is now greater than one, so we perform a left rotation to balance the tree. The final AVL Tree looks like this:
markdown
20 / \ 15 35 / / \ 10 30 40 / 25
In this AVL Tree, the height of the left and right subtrees of each node differs by at most one, ensuring efficient search, insertion, and deletion operations with a guaranteed time complexity of O(log n).