GATE Computer ScienceHardGATE CS (PYQ pattern)
A complete binary tree with 20 nodes is stored in A[1..20] with A[i] = i, the children of i at 2i and 2i + 1. The routine v(i) does: if i > 20 return; v(2i); print A[i]; v(2i + 1). In the output of v(1), at which positions (1-indexed) are 1 and 5 printed?
- A. 13 and 11
- B. 12 and 11
- C. 13 and 12
- D. 11 and 10
Correct answer: A. 13 and 11
Inorder prints the whole left subtree of the root first. The left subtree of 1 contains 2, 4, 5, 8, 9, 10, 11, 16, 17, 18, 19, 20: twelve nodes, so 1 is printed 13th. Inside the subtree of 2, the left part rooted at 4 (4, 8, 9, 16, 17, 18, 19) has 7 nodes, then 2; the subtree of 5 starts with 10 and its child 20, so 5 comes after 7 + 1 + 2 nodes: position 11. Full order: 16 8 17 4 18 9 19 2 20 10 5 11 1 12 6 13 3 14 7 15.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int a[4][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int (p)[3] = a; p += 2; int q = p + 1; printf("%d %d %d %d", ((p - 1) + 2), (p)[1]…
- char n[] = {"delhi", "mumbai", "chennai", "kolkata"}; char pp = n; ++pp; printf("%s ", pp + 2); printf("%c ", ++pp); printf("%s ", pp[-1]…
- char src[] = "gate0cse"; char dst[20]; char d = dst, s = src; int n = 0; while ((d++ = s++)) n++; printf("%d %zu %zu", n, sizeof(src)…
- What is the total number of characters written to the output by this program? (Assume arguments of the last printf are evaluated before…
- unsigned char a = 250, b = 10; unsigned char c = a + b; int x = -1; unsigned int y = 1; printf("%d %d %d %d", c, a + b, x < y, (unsigned…
- int s = 0; for (int i = 0; i < 6; i++) { if (i == 1) continue; switch (i % 4) { case 0: s += 1; case 1: s += 10; break; case 2: s += 100…