GATE / CS ARCHIVE

SUBJECT COLLECTION

Data Structures · Binary Search & Balanced Trees

46 questions, newest first

GATE CSE has asked 46 questions on Binary Search & Balanced Trees (Data Structures) from 1991 to 2026, in 28 of the 46 papers; the most recent is in GATE CSE 2026 Set 2. In the last five years it carried 1.4 marks per 100-mark paper on average, against 1.2 since 2003. Since 2014 the questions have been 11 MCQ, 5 NAT and 2 MSQ. 9 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
2 marks · MCQ
Report issue

Third insertion into a complete BST

Binary Search & Balanced Trees

Question 40, GATE 2026: The keys 1 through 15 are inserted in some order into an ordinary binary search tree, producing a complete binary tree. Which key can never be the third insertion: A 4, B 2, C 10, D 5?
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Givens and goal

The keys 1 through 15 are inserted in some order into an ordinary binary search tree, producing a complete binary tree. Which key can never be the third insertion: A 4, B 2, C 10, D 5?

Approach

Determine the forced final balanced shape and use the rule that each ancestor must be inserted before its descendants.

Step-by-step solution

1. A complete binary tree with 15 nodes has all four levels full. For the ordered keys 1 through 15, the BST invariant therefore fixes root 8, its children 4 and 12, and their children 2, 6, 10 and 14. The leaves are 1, 3, 5, 7, 9, 11, 13 and 15.
2. Ordinary BST insertion without rotations requires every ancestor of a node to have been inserted earlier. Otherwise an earlier inserted descendant would occupy an ancestor position and could not later move below it.
3. The ancestors of 5 are 8, 4 and 6. All three must already exist before 5 is inserted, so 5 cannot be inserted third.
4. Each other offered value can occur third: prefixes (8,12,4), (8,4,2) and (8,12,10) place 4, 2 and 10 third respectively. Each prefix respects the fixed tree's ancestor order, so it can be extended by inserting the remaining keys in an ancestor-before-descendant order to produce the required complete tree.

Common error: A completed tree need not stay complete at every intermediate insertion. Only the final shape is constrained here.

Forced final levels

    depth 0:                8
    depth 1:           4         12
    depth 2:         2   6     10   14
    depth 3:        1 3 5 7   9 11 13 15

Because 15=1+2+4+8 exactly fills four levels, the final complete tree is perfect. Ordinary unbalanced insertion does not move an existing node later, so the ancestor-before-descendant restriction applies to this fixed final shape. Rotations or a self-balancing insertion algorithm are not part of the question.

Original paper, page 29 · Answer key

2 marks · NAT
Report issue

Reconstructing a BST from preorder

Binary Search & Balanced Trees

Question 62, GATE 2026: A binary search tree has preorder sequence 50,25,13,40,30,47,75,60,70,80,77. Find the 1-based position of 60 in its postorder traversal.
Tap to enlarge
Answer & solution

Official answer: 7

Step through it (25 steps)

Givens and goal

A binary search tree has preorder sequence 50,25,13,40,30,47,75,60,70,80,77. Find the 1-based position of 60 in its postorder traversal.

Approach

Recursively split the preorder at each root according to BST value bounds, then traverse left, right, root.

Step-by-step solution

1. The first preorder entry is root 50. The following values below 50 form its left subtree: 25,13,40,30,47. The rest form its right subtree: 75,60,70,80,77.
2. In the left subtree, 25 has left child 13 and right subtree rooted at 40, whose children are 30 and 47. Its postorder is therefore 13,30,47,40,25.
3. In the right subtree, root 75 has a left subtree rooted at 60, containing 70 as 60’s right child. Its right subtree is rooted at 80, with left child 77. Thus that subtree’s postorder is 70,60,77,80,75.
4. The reconstructed tree is:

              50
          /        \
        25          75
       /  \        /  \
     13   40      60   80
         /  \      \  /
        30  47     70 77

5. Concatenate left-subtree postorder, right-subtree postorder and root: 13,30,47,40,25,70,60,77,80,75,50. The first five positions belong to the left subtree, position 6 is 70 and position 7 is 60.
6. Therefore the answer is 7. Preorder visits root first, while postorder visits root last; simply looking up 60’s original preorder position would answer the wrong traversal question.

Original paper, page 45 · Answer key

2 marks · MSQ
Report issue

Inserting keys after BST leaves

Binary Search & Balanced Trees

Question 49, GATE 2026: A BST has distinct real keys and n>0 leaves. For each leaf L with an inorder successor, choose one new key k strictly between key(L) and key(successor(L)); collect these in K and insert them. Select: A K has no duplicates; B K is nonempty; C height increases by at most one; D the node count doubles.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, C

Givens and goal

A BST has distinct real keys and n>0 leaves. For each leaf L with an inorder successor, choose one new key k strictly between key(L) and key(successor(L)); collect these in K and insert them. Select: A K has no duplicates; B K is nonempty; C height increases by at most one; D the node count doubles.

Approach

Use the disjoint sorted gaps associated with leaves and identify each insertion point.

Step-by-step solution

1. The inorder successor is the smallest existing key greater than a given key. Hence each chosen key lies in an open gap between consecutive existing sorted keys. Distinct leaves have distinct, disjoint gaps, so two selected values cannot be equal. This proves A.
2. For a leaf L with a successor, the search for a value in this gap follows the same ancestor comparisons as the search for L. Every ancestor bound is respected because no existing key lies between L and its successor. At L, the new value is larger, and L has no right child, so it is inserted as L’s right child.
3. Gaps belonging to different leaves are disjoint branches of this search structure. Inserting one selected key does not place it on another original leaf’s search path. Consequently each new node becomes a child of its own original leaf, irrespective of insertion order.
4. Every new depth is one more than an old leaf’s depth, so the final height is at most the original height plus one. It may increase by zero when deepest leaves have no insertion, proving C rather than an exact-one claim.
5. A singleton tree has one leaf, its maximum, with no successor. Then K is empty, refuting B and D. A right-only chain gives the same type of counterexample. More generally one inserted key per qualifying leaf does not imply one per original node.
6. Therefore A,C are true. Real-valued keys ensure a value can be chosen in every nonempty open gap.

Original paper, page 35 · Answer key

1 mark · MSQ
Report issue

Distinguishing BST guarantees from worst-case bounds

Binary Search & Balanced Trees

Question 26, GATE 2025: For a BST storing n distinct integers, assess: A “The maximum length of a path from the root node to any other node is (n−1).” B inorder traversal is always sorted. C worst-case search takes O(log₂ n). D every BST is a Min-Heap. Select all true statements. A’s wording is retained because it has a material quantifier ambiguity.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, B

Our answer: A;B under upper-bound interpretation; B under literal per-tree equality

Approach: Separate guarantees for every individual tree from the largest possible height among all n-node BSTs.
Inorder traversal visits the left subtree, root, then right subtree. By the BST ordering invariant all left keys are smaller and all right keys are larger. Applying that reasoning recursively produces sorted order, so B is unconditionally true.
Any root-to-node path contains at most n nodes, hence at most n−1 edges. A completely skewed BST attains this bound. Under the intended “maximum possible over all BSTs” interpretation, A is true and the official answer is A,B. Literally, however, A says that each tree’s maximum root path IS n−1. A balanced three-node BST has height 1 rather than 2, so under that reading A is false and only B survives.
C is false because search follows a root-to-node path and may take Θ(n) in a skewed BST; balancing is not promised. D is false because a BST need not be complete, and its left child can be smaller than its root, violating min-heap order.
Keep the official A;B for scoring while explicitly warning about A’s wording. The mathematical distinction must not be hidden by replacing “is” with “at most”.

Original paper, page 26 · Answer key

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

2 marks · MCQ
Report issue

What a BST reveals without its insertion order

Binary Search & Balanced Trees

Question 39, GATE 2024: A BST is built by inserting distinct integers from a known set V into an empty tree, with smaller keys in each left subtree. The insertion order and tree are unknown. What can always be determined from V? A inorder traversal; B root; C preorder; D postorder.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Use the BST order invariant and test the other traversals on two possible insertion sequences.

1. Derivation. An inorder traversal of a BST with distinct keys and smaller keys to the left always visits exactly the sorted set V, regardless of insertion order. The first inserted key becomes the root, and different insertion orders can change the root, preorder, and postorder. Hence only inorder is determined in general, option A. Trivial sets can coincidentally determine more, but do not establish those claims for arbitrary V.

2. Explanation and checks. For V={1,2,3}, inserting 2,1,3 gives root 2, preorder 2,1,3 and postorder 1,3,2. Inserting 1,2,3 gives root 1, preorder 1,2,3 and postorder 3,2,1. Both have inorder 1,2,3. Recursively, inorder visits smaller values, then the node, then larger values; thus it is the sorted list for every valid shape. The examples demonstrate why knowledge of the set alone cannot recover the shape-sensitive alternatives.

Original paper, page 26 · Answer key

1 mark · NAT
Report issue

Binary Search & Balanced Trees

Question 28, GATE 2022: A BST with 1000 distinct keys is a complete binary tree, stored in zero-based heap-array positions 0…999. At what index is its third-largest key?
Tap to enlarge
Answer & solution

Official answer: 509

The maximum key lies on the right spine, with zero-based array indices 0, 2, 6, 14, 30, 62, 126, 254, 510. Index 510 has no children because their indices exceed 999. The predecessor is its parent 254. The next predecessor is the maximum in 254’s left subtree, rooted at 509; 509 has no children either. Hence the third largest key is at index 509.

The heap-array formulas are left(i)=2i+1, right(i)=2i+2; they locate nodes, not key ordering. BST ordering implies reverse inorder visits largest first. At 254, the right child is 510 and left child 509. Both are leaves, so the first three reverse-inorder positions are 510,254,509.

Original paper, page 22 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 10, GATE 2021: A binary search tree T has n distinct elements. What is the time complexity of picking an element smaller than its maximum? A Θ(n log n); B Θ(n); C Θ(log n); D Θ(1). The task implicitly requires n≥2.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

If the root has a right child, the root itself is smaller than the maximum. If it has no right child, it is the maximum, so return its left child. For n≥2 that child exists in the latter case. Only a constant number of pointer tests is needed, Θ(1), D. The task implicitly needs at least two nodes; no such element exists for n=1.

An O(1) algorithm is: if root.right is non-null, return root.key; otherwise return root.left.key. In the first branch some right-subtree key is greater than the root, proving the root is not maximum. In the second branch the root is maximum; distinctness ensures its left child is strictly smaller. Searching for the second-largest element would be a different, potentially height-dependent task; the question asks for any smaller element.

Original paper, page 16 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 5, GATE 2020: A BST preorder is 15,10,12,11,20,18,16,19. Find postorder. A 10,11,12,15,16,18,19,20; B 11,12,10,16,19,18,20,15; C 20,19,18,16,15,12,11,10; D 19,16,18,20,11,12,10,15.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (19 steps)

Worked explanation

Preorder lists root, left subtree, right subtree; the BST ordering tells us where the left subtree ends. The reconstructed tree has edges 15→10(left),15→20(right),10→12(right),12→11(left),20→18(left),18→16(left),18→19(right). Postorder recursively visits left, right, root, hence the entire left subtree appears before the right subtree and 15 must be last. Option A is sorted inorder, C follows a different ordering, and D puts the right subtree before the left. This explicit edge list also makes the reconstruction checkable without a picture.

Derivation and answer

Root15 splits the preorder into left10, 12, 11 and right20, 18, 16, 19. On the left, 10 has right child12 with left child11, giving postorder 11, 12, 10. On the right, 20 has left child18 with children 16, 19, giving 16, 19, 18, 20. Append the root: 11, 12, 10, 16, 19, 18, 20, 15, B.

Original paper, page 5 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 6, GATE 2020: What is the worst-case time to insert n² elements into an AVL tree initially containing n elements? A Θ(n⁴); B Θ(n²); C Θ(n² log n); D Θ(n³).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Worked explanation

With m nodes, an AVL tree has height Θ(log m); locating an insertion position and propagating height changes take O(log m), while rotations do not introduce a larger order. During the sequence, m=n+i for 0≤i<n². Thus the total upper bound is Σ O(log(n+i))≤n² O(log(n+n²))=O(n² log n). For ordinary comparison-based insertion of distinct keys, an AVL tree’s external search paths have logarithmic depth; in particular an adversarial sequence can repeatedly force logarithmic work. A constant fraction of n² operations therefore contributes Ω(n² log n). Do not multiply by an extra n for rebalancing.

Derivation and answer

Throughout the n² insertions the AVL size lies between n and n+n², so each logarithmic insertion is O(log n). Their total is O(n²log n), and worst-case searches over a constant fraction of the later insertions require Ω(log n) each. Hence Θ(n²log n), C.

Original paper, page 5 · Answer key

2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 41, GATE 2020: A balanced BST holds n keys. Reporting all keys in [a,b] produces k outputs. What is the optimal worst-case complexity? A Θ(log n); B Θ(log n+k); C Θ(k log n); D Θ(n log k).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Worked explanation

A pruned inorder routine is: if node is null return; if key>a recurse left; if a≤key≤b output key; if key<b recurse right. With distinct keys, subtrees excluded by the interval are not scanned. Nodes visited without being reported lie along at most the two boundary-search paths, of height O(log n); every other visited node contributes an output. Thus a single traversal costs O(log n+k). Performing a fresh root-to-key successor search for each result would give an avoidable O(k log n). For a lower bound, a query locating a narrow boundary can require logarithmic comparisons and actually emitting k keys needs k operations.

Derivation and answer

Search down the balanced BST to find the lower bound a in O(log n). Then enumerate successive in-range keys with an inorder traversal, pruning subtrees entirely outside the interval. Boundary paths cost O(log n) and reported nodes cost O(k), so the total is O(log n+k). A worst-case boundary search needs logarithmic comparisons and reporting k distinct results needs Ω(k) output work. Thus the optimal worst-case bound is Θ(log n+k), B.

Original paper, page 13 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 6, GATE 2017: A binary search tree has 15 nodes. A one-node tree has height 0. Find minimum and maximum heights. A (4,15); B (3,14); C (4,14); D (3,15).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. Height counts edges, so levels 0 through h hold at most 1+2+...+2h=2h+1−1 nodes.

2. For 15 nodes, 2h+1−1≥15 requires h≥3. A perfect four-level tree realizes height 3 and can be assigned sorted BST keys.

3. At the other extreme, a one-child chain uses one edge per additional node, giving 14 edges. No simple root-to-leaf path can exceed 14 edges with 15 nodes. Answer B.

Original paper, page 4 · Answer key

2 marks · MCQ
Report issue

Binary Search & Balanced Trees

Question 36, GATE 2017: A binary search tree has preorder 12,8,6,2,7,9,10,16,15,19,17,20. Which is its postorder? A:2,6,7,8,9,10,12,15,16,17,19,20. B:2,7,6,10,9,8,15,17,20,19,16,12. C:7,2,6,8,9,10,20,17,19,15,16,12. D:7,6,2,10,9,8,15,16,17,20,19,12.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (27 steps)

1. Preorder visits root,left,right, so the first value 12 is the root. The contiguous values below 12 form its left preorder 8,6,2,7,9,10; the remaining values form right preorder 16,15,19,17,20.

2. Reconstruct the left side: root 8 has left root 6 with children 2 and 7, and right root 9 with right child 10. Its left-right-root postorder is 2,7,6,10,9,8.

3. On the right, root 16 has left child 15 and right root 19 with children 17 and 20. Its postorder is 15,17,20,19,16. Concatenate left postorder, right postorder and final root 12, obtaining B. Sorting the values would instead produce inorder, not postorder.

Original paper, page 14 · Answer key

2 marks · NAT
Report issue

Binary Search & Balanced Trees

Question 40, GATE 2016: How many permutations of 1,…,7, inserted into an empty BST, produce height 6? Height counts edges, so a one-node tree has height 0.
Tap to enlarge
Answer & solution

Official answer: 64.0 : 64.0

Our answer: 64

Height6 with seven nodes means every node lies on one chain. The first inserted value must be the smallest or largest remaining key; otherwise both subtrees would be nonempty and the tree could not be a chain. Recursively choose either endpoint for each of the first six insertions, with the last forced. This gives 26=64 distinct insertion orders.

For a chain, all remaining keys must fall on one side of each successive root. Thus the smallest or largest key is chosen repeatedly. This counts insertion permutations, not merely final shapes. For three keys the valid orders are 123,132,312,321, illustrating the recurrence C(k)=2C(k−1), C(1)=1.

Original paper, page 36 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 17, GATE 2015: Which sequences can be inorder traversals of BSTs? I 3,5,7,8,15,19,25; II 5,8,9,12,10,15,25; III 2,7,10,8,14,16,20; IV 4,6,7,9,18,20,25. A I and IV; B II and III; C II and IV; D II only.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

For distinct keys, recursively visiting left subtree, root, then right subtree produces strictly increasing order. I and IV meet this necessary condition and can be realized, for example, by inserting their keys in ascending order into right chains. II descends from 12 to 10, and III from 10 to 8, violating the property.

Solution: Inorder traversal of a BST with distinct keys must be increasing. I and IV are increasing. II places 12 before 10, and III places 10 before 8. Therefore I and IV, A.

Original paper, page 8 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 35, GATE 2015: What are worst-case insertion and deletion costs in a binary search tree with n keys? A Θ(log n),Θ(log n); B Θ(n),Θ(n); C Θ(n),Θ(log n); D Θ(log n),Θ(n).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

BST operations take O(h) time in terms of tree height h. Without a balancing guarantee, sorted insertion can create a chain with height n−1. Insertion may search to its bottom, and deletion of a specified key may likewise require a full-height search, with successor/predecessor work also bounded by height. Logarithmic guarantees require a balanced structure, which is not stated.

Solution: An unbalanced BST can be a chain of n nodes. Finding an insertion position or locating a node for deletion can then traverse Θ(n) nodes, so both worst-case costs are linear, B.

Original paper, page 13 · Answer key

1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 27, GATE 2015: Insert 71,65,84,69,67,83, in that order, into an initially empty binary search tree. Which element is at the lowest (deepest) level? (A) 65; (B) 67; (C) 69; (D) 83.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (8 steps)

1. Insertion gives root71, left65, right84; 69 is 65’s right child, 67 is 69’s left child, 83 is 84’s left child.

2. The deepest node is 67 at depth 3, B.

Original paper, page 62 · Answer key

2 marks · NAT
Report issue

Binary Search & Balanced Trees

Question 39, GATE 2014: An unaugmented balanced BST T stores n numbers. There are m keys in the interval from L to H, whose sum must be computed. If the tightest upper bound is O(nᵃ logᵇ n + mᶜ logᵈ n), find a+10b+100c+1000d.
Tap to enlarge
Answer & solution

Official answer: 110

Step 1: Use the search ordering to locate the interval's first relevant key or its boundary in O(log n), since the tree is balanced. Step 2: Traverse the m matching keys in inorder while pruning subtrees wholly outside the interval. With a traversal stack, the total is O(log n+m), not m independent root-to-key searches costing O(m log n). Step 3: Match this to the displayed form: n⁰log¹n + m¹log⁰n, so (a,b,c,d)=(0,1,1,0). The requested encoding is 0+10+100+0=110. Subtree-sum augmentation was not specified and would change the range-sum method.

Original paper, page 61 · Answer key

1 mark · MCQ
Report issue

Worst-case unbalanced BST insertion

Binary Search & Balanced Trees

Question 7, GATE 2013: Give the tightest listed worst-case upper bound for inserting into an ordinary n-node binary search tree: A O(1), B O(log n), C O(n), D O(n log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

In an ordinary unbalanced binary search tree, increasing insertions can create a chain of n nodes. Finding the next insertion position then takes a linear search down that chain. Worst-case insertion is O(n), C, not the logarithmic bound of balanced trees.

Insertion follows the search path until it reaches a missing child, then attaches the new node. Its cost is proportional to tree height. A balanced tree has logarithmic height, but no balance requirement is given. Inserting increasing keys creates a right-child chain; a later larger key must inspect all n existing nodes. Thus linear time is possible and O(n) is the tightest listed worst-case bound.

Original paper, page 2 · Answer key

2 marks · MCQ
Report issue

Recover BST postorder from preorder

Binary Search & Balanced Trees

Question 43, GATE 2013: A BST has preorder 30, 20, 10, 15, 25, 23, 39, 35, 42. Choose postorder: A10, 20, 15, 23, 25, 35, 42, 39, 30; B15, 10, 25, 23, 20, 42, 35, 39, 30; C15, 20, 10, 23, 25, 42, 35, 39, 30; D15, 10, 23, 25, 20, 35, 42, 39, 30.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (21 steps)

The root is 30. Its preorder left-subtree segment is 20, 10, 15, 25, 23 and its right-subtree segment is 39, 35, 42. Under 20, node 10 has right child 15, and node 25 has left child 23; their postorders are 15, 10 and 23, 25, giving the full left-subtree postorder 15, 10, 23, 25, 20. Under 39 are children 35 and 42, so its postorder is 35, 42, 39. Appending the root gives 15, 10, 23, 25, 20, 35, 42, 39, 30, option D.

Preorder identifies each subtree’s root first. Split the remaining values around 30: values below 30 form the left subtree and above 30 the right. Repeat around 20 and 39. Postorder reverses the placement of each root in the traversal: recursively emit its left subtree, then right subtree, then the root. This yields left block 15, 10, 23, 25, 20 and right block 35, 42, 39, followed by 30. Checking only that 30 is last is insufficient; every nested subtree must satisfy the same rule.

Original paper, page 10 · Answer key

1 mark · MCQ
Report issue

Balanced search tree with exponential size

Binary Search & Balanced Trees

Question 5, GATE 2012: What is the worst-case search time in a balanced binary search tree containing n·2^n elements?
(A) Θ(n log n) (B) Θ(n·2^n) (C) Θ(n) (D) Θ(log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A balanced binary search tree on N elements has worst-case search length Θ(log N). Here N=n·2n, so log₂N=log₂n+n=Θ(n). The answer is C, not Θ(log n), since n itself is not the number of elements.

Check: Search follows just one root-to-leaf branch and performs constant work per visited node. With base-two logarithms, n≤log₂(n2n)≤2n for n≥2, explicitly establishing matching linear upper and lower asymptotic bounds.

Original paper, page 2 · Answer key

2 marks · MCQ
Report issue

Unique BST labeling for a fixed shape

Binary Search & Balanced Trees

Question 29, GATE 2011: Given n distinct elements and a fixed unlabeled binary tree with n nodes, in how many ways can the elements populate that tree so it becomes a binary search tree?

(A) 0
(B) 1
(C) n!
(D) binomial(2n,n)/(n+1)
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Inorder traversal of a BST with distinct keys must list them in increasing order. The fixed ordered tree shape has a fixed inorder sequence of node positions, so assign the smallest key to the first position, and so on. This gives exactly one valid labeling, B; Catalan numbers count alternative shapes, not labelings of this supplied shape.

This assumes the usual ordered binary tree, in which left and right child positions are fixed. Inorder visits the entire left subtree, then the node, then the entire right subtree. At each node the BST condition requires every left key to be smaller and every right key larger. Therefore assigning sorted keys along inorder both constructs a valid solution and proves uniqueness. There is at least one solution for any such shape, which rules out zero as well.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Maximum AVL height with seven nodes

Binary Search & Balanced Trees

Question 37, GATE 2009: Maximum edge-height of an AVL tree with 7 nodes, with singleton height 0? A 2; B 3; C 4; D 5.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

The minimum-node recurrence for edge height h is N(h)=1+N(h−1)+N(h−2), with N(−1)=0, N(0)=1. Hence N(1)=2, N(2)=4, N(3)=7, N(4)=12. Seven nodes attain height 3 but cannot reach 4, so B.

To make an AVL tree as tall as possible with few nodes, give its two subtrees heights h−1 and h−2. Thus the minimum nodes at height h obey N(h)=1+N(h−1)+N(h−2). Starting with empty height−1 and singleton 0 gives 0, 1, 2, 4, 7, 12. Seven nodes suffice exactly for height 3, but any height 4 tree needs at least 12. This proves both attainability and impossibility, so B.

Original paper, page 9

2 marks · MCQ
Report issue

Reconstructing a BST from postorder

Binary Search & Balanced Trees

Question 46, GATE 2008: Given the postorder traversal P of a BST on distinct elements 1,…,n, what is the most efficient time to construct the unique BST having traversal P?

(A) Θ(log n)
(B) Θ(n)
(C) Θ(n log n)
(D) None: the tree cannot be uniquely determined
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Θ(n) time is sufficient and necessary. Read the postorder sequence backward: the next element is the root, followed by the right subtree in reverse postorder and then the left.
2. A recursive builder with allowed key interval (low,high) consumes a value only if it belongs in that interval; after creating a node with key r, build the right interval (r,high), then the left (low,r). Each key is consumed once and there are O(n) calls, so the time is O(n).
3. Distinct BST keys and their ordering make the construction unique. Reading and outputting n nodes requires Ω(n), yielding Θ(n).

Original paper, page 8

2 marks · MCQ
Report issue

Inorder traversal of a binary search tree

Binary Search & Balanced Trees

Question 33, GATE 2005: BST postorder is 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29. Candidate inorder A9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95; B9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29; C29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95; D95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Inorder traversal of a BST with distinct keys lists them in increasing order. Sorting the displayed key set gives 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95, option A. No full reconstruction from postorder is necessary.

For distinct keys in a BST, inorder visits every smaller key before the root and every larger key afterward, recursively producing the sorted key list. Sorting the supplied set gives A. There is no need to reconstruct the full shape from postorder unless the task asks for structure; postorder here merely specifies which keys are present.

Original paper, page 4

2 marks · MCQ
Report issue

Binary search trees on four keys

Binary Search & Balanced Trees

Question 35, GATE 2005: How many BSTs can be formed from four distinct keys? A5; B14; C24; D42.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

For distinct ordered keys, the number of BST shapes is the Catalan number C4. With C0=1, C1=1, C2=2, C3=5, the root split gives C4=1×5+1×2+2×1+5×1=14, B.

Choose each possible root rank and multiply the independent choices for left and right subtrees. Catalan recurrence C_n=Σ C_i C_(n−1−i), with C0=1, gives C1=1, C2=2, C3=5 and C4=5+2+2+5=14. This counts BST shapes with the sorted keys forced into inorder order, not arbitrary permutations of labels, so 4!=24 is not the answer.

Original paper, page 4

1 mark · MCQ
Report issue

Height after binary-search-tree insertions

Binary Search & Balanced Trees

Question 4, GATE 2004: Insert 10,1,3,5,15,12,16, in that order, into an empty binary search tree. What is its height, defined as the maximum edge distance from root to a leaf?
Options:
(A) 2
(B) 3
(C) 4
(D) 6
Question 4, GATE 2004: Insert 10,1,3,5,15,12,16, in that order, into an empty binary search tree. What is its height, defined as the maximum edge distance from root to a leaf?
Options:
(A) 2
(B) 3
(C) 4
(D) 6
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (9 steps)

1. Inserting10,1,3,5 produces the chain 10→1→3→5 on the left side. The remaining 15,12,16 form a right subtree rooted 15 with two children.

2. The deepest leaf 5 is three edges from 10, so the stated edge-distance height is 3, B.

3. Teaching check: The complete parent-child structure is 10:(left 1,right 15), 1:(right 3), 3:(right 5), 15:(left 12,right 16). The deepest path has four vertices but three edges. Distinguishing edge height from node count eliminates the off-by-one distractor.

Original paper, page 1

1 mark · MCQ
Report issue

Catalan recurrence for binary search trees

Binary Search & Balanced Trees

Question 6, GATE 2003: Let T(n) count binary search trees on n distinct keys. In T(n)=sum from k=1 to n of T(k−1)T(x), what is x? A. n−k+1; B. n−k; C. n−k−1; D. n−k−2.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Partition all trees by their root’s rank k. No key greater than the root may be in its left subtree, so the sizes are forced rather than chosen. For each left shape any right shape is allowed; multiply within a fixed k and sum over the disjoint root choices. Empty subtrees contribute T(0)=1.

2. If the root is the kth smallest key, its left subtree contains k−1 keys and its right subtree n−k. Their shapes can be chosen independently, giving T(k−1)T(n−k). Thus the missing argument x=n−k, B, with T(0)=1.

Original paper, page 1

1 mark · MCQ
Report issue

Inorder traversal of inserted keys

Binary Search & Balanced Trees

Question 19, GATE 2003: Insert 7,5,1,8,3,6,0,9,4,2, in this order, into an initially empty binary search tree using ordinary numerical order. What is its inorder traversal? A. 7,5,1,0,3,2,4,6,8,9; B. 0,2,4,3,1,6,5,9,8,7; C. 0,1,2,3,4,5,6,7,8,9; D. 9,8,6,4,2,3,0,1,5,7.
Question 19, GATE 2003: Insert 7,5,1,8,3,6,0,9,4,2, in this order, into an initially empty binary search tree using ordinary numerical order. What is its inorder traversal? A. 7,5,1,0,3,2,4,6,8,9; B. 0,2,4,3,1,6,5,9,8,7; C. 0,1,2,3,4,5,6,7,8,9; D. 9,8,6,4,2,3,0,1,5,7.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (23 steps)

1. At each node, all left-subtree keys are smaller and all right-subtree keys larger. Inorder recursively visits left subtree, root, right subtree, so this invariant sorts the output. The insertion order determines tree shape but cannot change this ordering property.

2. An inorder traversal of a BST visits its distinct keys in increasing order, independently of insertion order. The given keys are exactly 0 through 9, so the sequence is 0,1,2,3,4,5,6,7,8,9, C.

Original paper, page 2

2 marks · MCQ
Report issue

Ordered set supporting minimum deletion and unique insertion

Binary Search & Balanced Trees

Question 63, GATE 2003: A set of n integers must support (I) deleting its smallest element and (II) inserting an element only if it is not already present, each in O(log n) time. Which structure suffices?
A. A heap, but not a balanced binary search tree
B. A balanced binary search tree, but not a heap
C. Both a balanced binary search tree and a heap
D. Neither
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. A balanced binary search tree has height O(log n). Follow left children to the minimum and delete/rebalance in O(log n). Search for a proposed key and, if absent, insert/rebalance along one root-to-leaf path in O(log n).
2. A min-heap can delete its root and sift down in O(log n), and can insert a known-new key in O(log n).
3. However, a heap is only partially ordered. Testing whether an arbitrary key already appears can require inspecting Θ(n) positions; insertion-if-absent includes that test.
4. Thus the balanced tree alone meets both requirements, B. A heap augmented with a separate search index is not the plain heap being compared.

Solution: A balanced binary search tree supports membership search, insertion, and deletion of its minimum in O(log n) worst-case time. A plain heap supports minimum deletion and unrestricted insertion in O(log n), but checking whether an arbitrary proposed value is already present may require Θ(n) comparisons. Therefore the uniqueness requirement rules out the heap alone, leaving B. Augmenting a heap with a separate membership structure is outside the listed plain-structure comparison.

Original paper, page 9

2 marks · MCQ
Report issue

Height of a weight-balanced binary tree

Binary Search & Balanced Trees

Question 2.12, GATE 2002: A weight-balanced binary tree is defined so that, at every node, the left subtree’s node count is at least half and at most twice the right subtree’s count. Which expression best describes its maximum possible height with n nodes, counting nodes along the root-to-farthest-leaf path?
(A) log₂ n; (B) log₂⁄₃ n; (C) log₃ n; (D) log₃⁄₂ n.
Tap to enlarge
Your answer
Answer & solution

Answer: D as the standard growth-bound expression (no official key was published)

Step 1: Use the heavier child on a deepest path.

Step 2: If child sizes are a≥b, the weight constraint gives a≤2b. Since a+b=n−1, this implies a≤2(n−1)/3. Repeating the inequality along a path yields geometric shrinkage and a logarithmic height. Integer rounding prevents the option from being an exact height formula; its role is to express the extremal shrinkage factor rather than a literal answer for every small n.

Conclusion: A child subtree contains at most 2(n−1)/3 nodes. Following a deepest branch therefore shrinks the remaining size by at least a constant factor 3/2, yielding height at most log3/2n+O(1). Maximally skewed admissible weight splits attain the same leading logarithmic growth, so D is the conventional expression. The displayed logarithm is a growth bound, not an exact integer height for every n; rounding and admissible tree sizes matter.

Original paper, page 5

Descriptive
Report issue

Build a binary search tree

Binary Search & Balanced Trees

Question 14(a), GATE 2001: Insert 15,32,20,9,3,25,12,1 into an initially empty BST.
Question 14(a), GATE 2001: Insert 15,32,20,9,3,25,12,1 into an initially empty BST.
Tap to enlarge
Answer & solution

Answer: Root 15; left subtree 9(3(1,∅),12); right subtree 32(20(∅,25),∅). (no official key was published)

Step through it (19 steps)

Use the rule smaller keys go left and larger keys go right. Insert 15 as root; 32 right of 15; 20 left of 32; 9 left of 15; 3 left of 9; 25 right of 20; 12 right of 9; 1 left of 3.

        15
       /  \
      9    32
     / \   /
    3  12 20
   /       \
  1        25

Accessible description: root 15 has children 9 and 32. Node 9 has children 3 and 12; node 3 has left child 1. Node 32 has left child 20, whose right child is 25. All other links are null. Inorder traversal 1,3,9,12,15,20,25,32 verifies the ordering.

Original paper, page 9

Descriptive
Report issue

Delete the BST root

Binary Search & Balanced Trees

Question 14(b), GATE 2001: Delete key 15 from the BST built by insertion order 15,32,20,9,3,25,12,1.
Question 14(b), GATE 2001: Delete key 15 from the BST built by insertion order 15,32,20,9,3,25,12,1.
Question 14(b), GATE 2001: Delete key 15 from the BST built by insertion order 15,32,20,9,3,25,12,1.
Tap to enlarge
Answer & solution

Answer: Successor replacement yields root 20; predecessor replacement yields root 12. (no official key was published)

Choose inorder-successor replacement: the least key in the right subtree of 15 is 20. Copy 20 into the root and remove the old 20 node, reconnecting its right child 25 as the left child of 32.

        20
       /  \
      9    32
     / \   /
    3  12 25
   /
  1

Accessible description: root 20 has children 9 and 32; 9 has children 3 and 12; 3 has left child 1; 32 has left child 25. Alternatively, inorder-predecessor replacement uses 12 as root and removes the old leaf 12. That result is 12(9(3(1,∅),∅),32(20(∅,25),∅)). Both are valid because the question does not prescribe which replacement policy to use.

Original paper, page 9

Descriptive
Report issue

Minimum AVL recurrence

Binary Search & Balanced Trees

Question 21(a), GATE 1998: Derive the minimum number N(h) of nodes in an AVL tree of height h. State the height convention.
Question 21(a), GATE 1998: Derive the minimum number N(h) of nodes in an AVL tree of height h. State the height convention.
Tap to enlarge
Answer & solution

Answer: N(h)=1+N(h−1)+N(h−2); edge-height bases N(−1)=0,N(0)=1. (no official key was published)

Take height to count edges on a longest root-to-leaf path; an empty tree has height −1 and a leaf height 0. A tree of height h must have at least one child subtree of height h−1. AVL balance permits the other to be no shorter than h−2. To minimize nodes, choose exactly those two heights and make both subtrees themselves minimum-size AVL trees. Counting the root gives N(h)=1+N(h−1)+N(h−2), with N(−1)=0,N(0)=1.

This is both a lower bound and attainable: recursively attach extremal subtrees of heights h−1 and h−2 to a root. The height difference is one, so the construction is AVL. Writing M(h)=N(h)+1 gives M(h)=M(h−1)+M(h−2), and the bases imply N(h)=Fh+3−1 when F0=0,F1=1.

If height instead counts levels, the empty tree is height 0 and a leaf height 1, so the same recurrence has bases N(0)=0,N(1)=1 and formula Fh+2−1. The recurrence is structural; the indexing convention must not be mixed between parts.

Original paper, page 8

Descriptive
Report issue

Minimum AVL height eight

Binary Search & Balanced Trees

Question 21(b), GATE 1998: Find the smallest AVL tree size for height 8.
Question 21(b), GATE 1998: Find the smallest AVL tree size for height 8.
Tap to enlarge
Answer & solution

Answer: 88 nodes for edge height; 54 for level height. (no official key was published)

Using the edge-height convention from part (a), start N(−1)=0,N(0)=1 and apply N(h)=1+N(h−1)+N(h−2). The values at heights 1 through 8 are 2,4,7,12,20,33,54,88. Thus the smallest tree at edge height 8 has 88 nodes. Equivalently, F11−1=89−1=88.

If the source's unstated height convention counts levels, the same physical tree's level height is one larger. A level-height-8 minimum tree therefore has edge height 7 and 54 nodes. This is not a disagreement in AVL structure, only an indexing ambiguity. Specify the convention rather than asserting both numbers describe the same notion of height. The printed empty '(c)' after this question contains no task text and is not invented as another solution item.

Original paper, page 8

2 marks · MCQ
Report issue

Valid BST preorder

Binary Search & Balanced Trees

Question 4.5, GATE 1997: A BST contains 1…8. Which preorder is valid:53124786, 53126487, 53241678, 53124768?
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

For D, root 5 divides the remaining sequence into left subtree 3, 1, 2, 4 and right subtree 7, 6, 8. In the left subtree, 3 has left subtree 1→right child 2 and right child 4. In the right subtree, 7 has children 6 and 8. This constructs the required BST. A gives 7, 8, 6 after entering root 7’s right subtree before returning to a smaller value 6. B returns to 4 after beginning root 5’s right subtree at 6. C returns to 1 after entering the right subtree of 3 at 4. These violate preorder’s contiguous subtree blocks, leaving D.

Original paper, page 4

3 marks · Descriptive
Report issue

Bound size-balanced tree size

Binary Search & Balanced Trees

Question 16(a), GATE 1997: A size-balanced binary tree has, at every node, left and right subtree sizes differing by at most one. Node distance is the number of edges from the root, and tree height h is the greatest root-to-leaf distance. Prove by induction on h that a tree of height h has at least 2^h nodes.
Question 16(a), GATE 1997: A size-balanced binary tree has, at every node, left and right subtree sizes differing by at most one. Node distance is the number of edges from the root, and tree height h is the greatest root-to-leaf distance. Prove by induction on h that a tree of height h has at least 2^h nodes.
Tap to enlarge
Answer & solution

Answer: Every such tree has N≥2^h; the bound is attainable. (no official key was published)

Use induction on the nonnegative height h. For h=0 the tree consists of the root, so N=1=20.

For h≥1, at least one child subtree has height h−1; otherwise no root-to-leaf path could have length h. Call that subtree T and let its size be s. By the induction hypothesis, s≥2h−1. The other child subtree has size t≥s−1, because the defining balance condition bounds the size difference by one. Empty subtrees are allowed and have size zero, so this also handles h=1.

Counting the root gives

N=1+s+t ≥1+s+(s−1)=2s ≥2h.

This proves the assertion. We did not assume the two subtrees have equal height or that every level is full. Size balance is a statement about node counts, and the proof uses exactly that statement.

The bound is sharp. For h≥1, combine a minimum-size tree of height h−1, with 2h−1 nodes, and a perfect tree of height h−2, with 2h−1−1 nodes, below a new root. Their sizes differ by one and their subtrees satisfy the same condition. The total is 2h and the height is h. At h=1 the smaller subtree is empty. This construction also explains why the stronger bound 2h+1−1 would be false: a size-balanced tree need not be perfect at its last level.

Original paper, page 8

2 marks · Descriptive
Report issue

Count the penultimate tree level

Binary Search & Balanced Trees

Question 16(b), GATE 1997: A size-balanced binary tree has, at every node, left and right subtree sizes differing by at most one. Node distance is the number of edges from the root, and tree height h is the greatest root-to-leaf distance. For h≥1, how many nodes are at distance h−1 from the root?
Question 16(b), GATE 1997: A size-balanced binary tree has, at every node, left and right subtree sizes differing by at most one. Node distance is the number of edges from the root, and tree height h is the greatest root-to-leaf distance. For h≥1, how many nodes are at distance h−1 from the root?
Tap to enlarge
Answer & solution

Answer: Exactly 2^(h−1). (no official key was published)

The level at distance h−1 is full, so it contains 2h−1 nodes. To justify fullness rather than assume it, first use the proved size bound: a size-balanced tree of height h has N≥2h nodes.

For any node whose subtree has s nodes, its child sizes sum to s−1 and differ by at most one. Hence each child has at least floor((s−1)/2) nodes. Starting from N≥2h, induction on depth shows that every node at depth d≤h−1 has subtree size at least 2h−d−1. Indeed, a parent lower bound 2h−d−1 gives each child at least 2h−d−1−1.

In particular, at every depth d<h−1 this lower bound is at least 3. Such a subtree has both children nonempty. Beginning with one root, the node count therefore doubles at each step until depth h−1, giving exactly 2h−1 nodes there. The last level h may be only partly occupied, so this does not assert that the entire tree is perfect.

For h=1 the requested level is the root itself and the formula gives 1. For h=2 a smallest example has four nodes: its depth-1 level still has both children, while only one node need occur at depth 2. The paper explicitly states h≥1.

Original paper, page 8

1 mark · MCQ
Report issue

AVL ancestors after inserting below g

Binary Search & Balanced Trees

Question 1.14, GATE 1996: In the pictured AVL tree, a has children b,e; b has c,d; c has g; e has f. How many nodes become unbalanced when a new child is attached below g, before rebalancing?
Tap to enlarge
Your answer
Answer & solution

Answer: B: 3 (no official key was published)

The insertion increases the height of the subtree rooted at g by one. That increase propagates through c, b and a. At c, its already taller g side becomes two levels taller than its empty opposite side. The same extra level makes b and then a have height difference two. Node g itself has only one new child, so its balance magnitude is one; e and its descendants are unchanged. Thus c,b,a are the three unbalanced nodes before rotations. Counting after AVL repair would answer a different question.

Original paper, page 2

2 marks · MCQ
Report issue

Sizes of root subtrees after BST insertion

Binary Search & Balanced Trees

Question 2.14, GATE 1996: Insert 50,15,62,5,20,58,91,3,8,37,60,24 into an empty BST. Count nodes in the left and right subtrees of the root.
Tap to enlarge
Your answer
Answer & solution

Answer: B: 7 and 4 (no official key was published)

Step through it (14 steps)

The first key 50 remains the root in ordinary unbalanced BST insertion. Every later key below 50 belongs somewhere in its left subtree: 15,5,20,3,8,37,24, totaling seven. Every key above 50 belongs in its right subtree: 62,58,91,60, totaling four. Internal insertion paths affect shape but not which side of the root contains each key. The root itself is excluded from both counts, and 7+4+1=12 checks the total number of inserted keys.

Original paper, page 5

Descriptive
Report issue

Test a binary-search-tree probe sequence

Binary Search & Balanced Trees

Question 4(a), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 61, 52, 14, 17, 40, 43 can occur, and explain. GFG instead prints 11 in place of 17 at the fourth probe.
Question 4(a), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 61, 52, 14, 17, 40, 43 can occur, and explain. GFG instead prints 11 in place of 17 at the fourth probe.
Tap to enlarge
Answer & solution

Answer: Impossible (no official key was published)

A search path obeys every ancestor constraint, not merely the immediately preceding comparison. Maintain an open interval of permissible keys, initially (−∞,+∞). When a probe is below 43, searching right raises the lower bound to that probe. When a probe is above 43, searching left lowers the upper bound.

After 61, every later probe must lie in (−∞,61).
After 52, every later probe must lie in (−∞,52).
After 14, every later probe must lie in (14,52).
After 17, every later probe must lie in (17,52).
After 40, every later probe must lie in (40,52).
The final probe is 43, so the search succeeds.

All probes meet the bounds. A witnessing BST can be built as a chain with these keys, taking the left or right child according to each comparison with 43. Thus the bound test proves existence, rather than just failing to find an error.

With 17 the sequence is valid. With 11 it is invalid, because after visiting 14 the search for 43 must stay in the right subtree of 14, whose keys exceed 14. The 11 variant therefore has the opposite answer.

Original paper, page 7

Descriptive
Report issue

Test a binary-search-tree probe sequence

Binary Search & Balanced Trees

Question 4(b), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 2, 3, 50, 40, 60, 43 can occur, and explain.
Question 4(b), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 2, 3, 50, 40, 60, 43 can occur, and explain.
Tap to enlarge
Answer & solution

Answer: Impossible. (no official key was published)

A search path obeys every ancestor constraint, not merely the immediately preceding comparison. Maintain an open interval of permissible keys, initially (−∞,+∞). When a probe is below 43, searching right raises the lower bound to that probe. When a probe is above 43, searching left lowers the upper bound.

After 2, every later probe must lie in (2,+∞).
After 3, every later probe must lie in (3,+∞).
After 50, every later probe must lie in (3,50).
After 40, every later probe must lie in (40,50).
Probe 60 violates the inherited interval (40,50); the sequence is impossible.

The violated bound comes from an earlier ancestor. A later comparison cannot discard it: every node subsequently visited remains inside that ancestor’s chosen subtree. No choice of unvisited nodes can repair this path.

Original paper, page 7

Descriptive
Report issue

Test a binary-search-tree probe sequence

Binary Search & Balanced Trees

Question 4(c), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 10, 65, 31, 48, 37, 43 can occur, and explain.
Question 4(c), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 10, 65, 31, 48, 37, 43 can occur, and explain.
Tap to enlarge
Answer & solution

Answer: Possible. (no official key was published)

A search path obeys every ancestor constraint, not merely the immediately preceding comparison. Maintain an open interval of permissible keys, initially (−∞,+∞). When a probe is below 43, searching right raises the lower bound to that probe. When a probe is above 43, searching left lowers the upper bound.

After 10, every later probe must lie in (10,+∞).
After 65, every later probe must lie in (10,65).
After 31, every later probe must lie in (31,65).
After 48, every later probe must lie in (31,48).
After 37, every later probe must lie in (37,48).
The final probe is 43, so the search succeeds.

All probes meet the bounds. A witnessing BST can be built as a chain with these keys, taking the left or right child according to each comparison with 43. Thus the bound test proves existence, rather than just failing to find an error.

Original paper, page 7

Descriptive
Report issue

Test a binary-search-tree probe sequence

Binary Search & Balanced Trees

Question 4(d), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 81, 61, 52, 14, 41, 43 can occur, and explain.
Question 4(d), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 81, 61, 52, 14, 41, 43 can occur, and explain.
Tap to enlarge
Answer & solution

Answer: Possible. (no official key was published)

A search path obeys every ancestor constraint, not merely the immediately preceding comparison. Maintain an open interval of permissible keys, initially (−∞,+∞). When a probe is below 43, searching right raises the lower bound to that probe. When a probe is above 43, searching left lowers the upper bound.

After 81, every later probe must lie in (−∞,81).
After 61, every later probe must lie in (−∞,61).
After 52, every later probe must lie in (−∞,52).
After 14, every later probe must lie in (14,52).
After 41, every later probe must lie in (41,52).
The final probe is 43, so the search succeeds.

All probes meet the bounds. A witnessing BST can be built as a chain with these keys, taking the left or right child according to each comparison with 43. Thus the bound test proves existence, rather than just failing to find an error.

Original paper, page 7

Descriptive
Report issue

Test a binary-search-tree probe sequence

Binary Search & Balanced Trees

Question 4(e), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 17, 77, 27, 66, 18, 43 can occur, and explain.
Question 4(e), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 17, 77, 27, 66, 18, 43 can occur, and explain.
Tap to enlarge
Answer & solution

Answer: Impossible. (no official key was published)

A search path obeys every ancestor constraint, not merely the immediately preceding comparison. Maintain an open interval of permissible keys, initially (−∞,+∞). When a probe is below 43, searching right raises the lower bound to that probe. When a probe is above 43, searching left lowers the upper bound.

After 17, every later probe must lie in (17,+∞).
After 77, every later probe must lie in (17,77).
After 27, every later probe must lie in (27,77).
After 66, every later probe must lie in (27,66).
Probe 18 violates the inherited interval (27,66); the sequence is impossible.

The violated bound comes from an earlier ancestor. A later comparison cannot discard it: every node subsequently visited remains inside that ancestor’s chosen subtree. No choice of unvisited nodes can repair this path.

Original paper, page 7

2 marks · Descriptive
Report issue

Optimal search tree for three equally likely identifiers

Binary Search & Balanced Trees

Question 4(iii), GATE 1991: Construct an optimal binary search tree for ordered identifiers if, stop, while with equal successful-search probabilities.
Question 4(iii), GATE 1991: Construct an optimal binary search tree for ordered identifiers if, stop, while with equal successful-search probabilities.
Tap to enlarge
Answer & solution

Answer: Root stop; left child if; right child while (no official key was published)

The alphabetic order is if<stop<while. Placing the middle identifier at the root preserves search-tree ordering with the other two as its children. Successful search takes one comparison at the root and two at either child, for mean (1+2+2)/3=5/3. Any endpoint root forces the remaining two into one subtree, creating depths corresponding to comparison counts 1, 2, 3 and mean 2. Thus the balanced tree is optimal. The calculation uses equal successful-search probabilities; unspecified unequal unsuccessful-search gap weights would define a different optimization problem.

Original paper, page 5