Question 13Asked in UGC NET Computer Science, August 2016 (Paper II & III, Selected Questions)

The run time for traversing all the nodes of a binary search tree with n nodes and printing them in an order is :

  1. AO (lg n)
  2. BO(n lg n)
  3. CO(n)
  4. DO(n²)
Show answerHide answer

Correct answer: (C) O(n)

Explanation

Printing the elements in order means printing them in sorted order; inorder traversal of a BST gives the sorted order and visits each node once, taking O(n) time. For a balanced BST T(n) = 2T(n/2) + c and for a skewed tree T(n) = T(n – 1) + c; both solve to O(n).

From UGC NET Computer Science PYQ, August 2016 (Paper II & III, Selected Questions)

This is question 13 of UGC NET Computer Science PYQ, August 2016 (Paper II & III, Selected Questions). See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.