GATE CS 1996 · Arrays & Matrix Representation

MCQ · 1 mark · Problem PROG-DSTR-0264

Consider the following balanced (AVL) binary tree: with root a. The root's left child is b and its right child is e. Node b has left child c and right child d. Node c has only a left child, g. Node e has only a left child, f. Nodes d, g and f are leaves.How many nodes will become unbalanced when a node is inserted as a child of the node g?
  1. A
    1
  2. B
    3
  3. C
    7
  4. D
    8

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solution\[\boxed{\text{balance}(v) = h(\text{left}) - h(\text{right}),\ \text{unbalanced if } |\text{balance}| \ge 2}\]Heights (a leaf has height 0, an empty subtree \(-1\)) after inserting a new leaf x under g: x = 0, g = 1, c = 2, d = 0, b = 3, f = 0, e = 1, a = 4.c: left 1, right \(-1\): balance 2, unbalanced.b: left 2 (c), right 0 (d): balance 2, unbalanced.a: left 3 (b), right 1 (e): balance 2, unbalanced.g (balance \(\pm1\)) and e (balance 1) stay balanced. So 3 nodes become unbalanced (before the insertion every node had balance 0 or 1).Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com