GATE Computer ScienceVery hardGATE CS (PYQ pattern)
The number of nodes of a complete binary tree is found recursively: compute the leftmost-path height and the rightmost-path height of the current subtree; if they are equal the subtree is perfect and has 2^(h+1) - 1 nodes, else return 1 + count(left) + count(right). For a complete tree with 1000 nodes, how many calls to count are made (the first call included; a call on a null child counts)?
- A. 19
- B. 17
- C. 22
- D. 21
Correct answer: A. 19
Only the spine of non-perfect subtrees is expanded; each such node makes two child calls, one of which is perfect (returns immediately). The total is 19 calls.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A BST is built from the sorted array 1, 2, ..., 14 by recursion: the root is a[mid] with mid = (lo + hi) / 2 (integer division), then the…
- In a binary tree given as (key, left, right) = (10, (-20, (8), (-6, (4), -)), (15, (-3), (12, (7), (-9)))), a 'path' is any sequence of…
- An AVL tree is built by inserting 1, 2, ..., 15 in increasing order (the perfect tree with root 8). Then keys 1, 3, 5, 2, 4 are deleted in…
- Cuckoo hashing uses two tables of size 7: h1(k) = k mod 7 in T1 and h2(k) = floor(k / 7) mod 7 in T2. To insert x, place it in T1[h1(x)]…
- Keys 19, 36, 5, 21, 4, 26, 14, 17, 25, 12, 40, 33 are inserted into a chained hash table with 7 buckets, h(k) = k mod 7, each new key…
- A max-heap is built from [90, 80, 70, 60, 50, 40, 30, 20, 10, 15, 25, 35, 45, 55, 65] by bottom-up heapify. Then the key at index 14 is…