GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Enumerate ordered binary tree shapes

5 questions solved the same way, newest first. Back to Data Structures

How these are solved

Split n nodes into left and right subtrees of sizes k and n-1-k; multiply independent shape counts and sum over root splits (Catalan recurrence). Assign fixed traversal labels afterward when required.

1 mark · MCQ
Report issue

Three-node binary-tree shapes

Tree Properties & Traversals

Question 13, GATE 2007: How many different binary trees can be formed using three unlabeled nodes?
Options:
(A) 1
(B) 5
(C) 4
(D) 3
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. Distinguish left and right child positions. There are four three-node chains (LL, LR, RL, RR) and one root with two children.

2. Derivation and interpretation. For ordered binary trees, let T(0)=1 and T(n)=sum T(i)T(n−1−i). Then T(1)=1,T(2)=2 and T(3)=2+1+2=5, B. Left and right child positions are distinguished, as in the standard binary-tree convention; unlabeled does not mean unordered.

Original paper, page 3

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

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

Descriptive
Report issue

Enumerate binary trees with fixed preorder

Tree Properties & Traversals

Question 6, GATE 2002: Draw every binary tree with exactly three labeled nodes A, B, C for which preorder traversal visits C, B, A in that order.
Tap to enlarge
Shared context

Find every ordered binary tree containing exactly the distinct labels A, B, C whose preorder sequence is C,B,A. Left and right child positions are distinguished.

Answer & solution

Answer: Exactly 5 ordered binary trees (no official key was published)

1. Use the first preorder symbol to fix the root. Preorder visits root, then left subtree, then right subtree. Therefore every requested tree must have C at its root.

2. Partition the two remaining nodes between C’s subtrees. The possible left/right subtree sizes are (2,0), (0,2), and (1,1). These cases exhaust the possibilities because there are exactly two nodes besides C.

3. Handle the two single-subtree cases. For sizes (2,0), B is C’s left child, and A is either B’s left child or B’s right child: two trees. For sizes (0,2), B is C’s right child, and A again has two child positions: two more trees. B must precede A because their subtree’s preorder must be B,A.

4. Handle sizes (1,1). Preorder visits the entire left subtree before the right, so the left child is B and the right child is A: one tree. Reversing those children would give C,A,B and is not allowed.

5. Draw the resulting 2 + 2 + 1 = 5 ordered binary trees:

(1)      C         (2)      C
        /                 /
       B                 B
      /                   \
     A                     A

(3)      C         (4)      C
          \                 \
           B                 B
          /                   \
         A                     A

(5)      C
        / \
       B   A

6. Verify each tree by reading root-left-right. Each gives C,B,A. The exhaustive subtree-size split proves that no additional ordered binary-tree shape can qualify. In explicit node(left,right) notation, with - for no child, the five trees are C(B(A(-,-),-),-), C(B(-,A(-,-)),-), C(-,B(A(-,-),-)), C(-,B(-,A(-,-))), and C(B(-,-),A(-,-)).

Original paper, page 7

Descriptive
Report issue

Enumerate postorder binary trees

Tree Properties & Traversals

Question 6, GATE 1995: How many ordered binary trees on three nodes have postorder A,B,C? Draw all of them.
Tap to enlarge
Answer & solution

Answer: 5 (no official key was published)

Postorder visits the left subtree, then the right subtree, then the root, so C must be the root. Partition the first two labels between its subtrees. If both subtrees are nonempty, each has one node: left A and right B. If only one subtree is nonempty, that subtree has postorder A,B, so its root is B and A can be either B’s left or right child. The two-node subtree itself can be C’s left or right child, giving four more trees.

A compact complete drawing uses node(left,right), with ∅ denoting an absent child:
1. C(A,B)
2. C(B(A,∅),∅)
3. C(B(∅,A),∅)
4. C(∅,B(A,∅))
5. C(∅,B(∅,A)).
Here A or B written without parentheses is a leaf. Each drawing emits A then B then C under postorder. The cases exhaust left-subtree sizes 0,1,2, giving the Catalan count C₃=C₀C₂+C₁C₁+C₂C₀=2+1+2=5. Left and right child positions are distinguished; these are not restricted to full binary trees.

Original paper, page 6