GATE / CS ARCHIVE

GATE CSE 2025 SET 2 / QUESTION 35

GATE CSE 2025 Set 2 Q35

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2025 Set 2 paper

GATE 2025 Set 2, Q35 1 mark · NAT
Report issue

Binary Search & Balanced Trees

Question 35, GATE 2025: Insert 10,−4,15,30,20,5,60,19 in that order into an initially empty ordinary binary search tree, with no balancing rotations. How many edges are on the path from node 19 to the root?
Tap to enlarge
Answer & solution

Official answer: 4

Step through it (10 steps)

Insert by comparing each new key with the nodes on its search path. The first key 10 becomes the root. −4 is its left child and 15 its right child. Next 30 becomes the right child of 15; 20 becomes the left child of 30. The key 5 goes right of −4, and 60 goes right of 30.

For 19, compare with 10: go right. Compare with 15: go right. Compare with 30: go left. Compare with 20: go left and insert there. Thus its root path is 10→15→30→20→19.

There are five nodes on that path but four edges, so the requested distance is 4. This is its depth when the root has depth zero. The left branch containing −4 and 5, and the right branch containing 60, do not contribute to this path. Answer: 4.

Original paper, page 26 · Answer key