GATE Computer ScienceVery hardGATE CS (PYQ pattern)

An AVL tree is built by inserting 1, 2, ..., 15 in increasing order (the perfect tree with root 8). Then keys 1, 3, 5, 2, 4 are deleted in that order (a node with two children is replaced by its inorder successor, and rebalancing walks back up to the root, rotating wherever |balance| = 2; a double rotation counts as 2). How many rotations do the five deletions cause, and what is the preorder traversal of the final tree?

  1. A. 1 rotations; 8 6 7 12 10 9 11 14 13 15
  2. B. 2 rotations; 8 6 7 12 10 9 11 14 13 15
  3. C. 0 rotations; 8 6 7 12 10 9 11 14 13 15
  4. D. 1 rotations; 6 7 8 9 10 11 12 13 14 15

Correct answer: A. 1 rotations; 8 6 7 12 10 9 11 14 13 15

Tracing the deletions with rebalancing at every ancestor gives 1 rotations and the preorder shown (the tree stays height-balanced throughout).

More Programming and Data Structures (GATE) questions

Exams that ask this topic