Suppose a binary search tree has been constructed from the following sequence of numbers in the order in which they arrive : 6, 2, 10, 1, 5, 7, 11, 3, 9, 4, 8. Consider the following piece of code :

Show(root) { if (root !=NULL)

{ printf(“% d”, root → key);

show (root → right);

show (root → left);

}

else

return ; 

}

The sequence printed will be : 

This question was previously asked in
NIELIT Scientific Assistant CS 5 Dec 2021 Official Paper
View all NIELIT Scientific Assistant Papers >
  1. 6, 11, 10, 7, 8, 9, 2, 4, 3, 5, 1 
  2. 6, 11, 7, 9, 8, 10, 2, 5, 1, 3, 4
  3. 6, 10, 11, 7, 9, 8, 2, 5, 3, 4, 1 
  4. 6, 10, 2, 11, 7, 9, 8, 5, 3, 4, 1

Answer (Detailed Solution Below)

Option 1 : 6, 11, 10, 7, 8, 9, 2, 4, 3, 5, 1 
Free
NIELIT Scientific Assistant Quantitative Aptitude Mock Test
0.5 K Users
20 Questions 20 Marks 30 Mins

Detailed Solution

Download Solution PDF

The correct answer is Option 1.

Key Points

  • The given code performs a traversal on a binary search tree (BST).
  • This traversal is a variant of the pre-order traversal, but it visits the right subtree before the left subtree.
  • In a pre-order traversal, the nodes are visited in the following order: Root, Right, Left.
  • Given the sequence of numbers: 6, 2, 10, 1, 5, 7, 11, 3, 9, 4, 8, the BST is constructed as follows:
    • 6 is the root node.
    • 2 is inserted to the left of 6.
    • 10 is inserted to the right of 6.
    • 1 is inserted to the left of 2.
    • 5 is inserted to the right of 2.
    • 7 is inserted to the left of 10.
    • 11 is inserted to the right of 10.
    • 3 is inserted to the left of 5.
    • 9 is inserted to the right of 7.
    • 4 is inserted to the right of 3.
    • 8 is inserted to the left of 9.
  • The traversal in the order: Root, Right, Left results in the sequence: 6, 10, 11, 7, 9, 8, 2, 5, 3, 4, 1.

Additional Information

  • In a standard pre-order traversal (Root, Left, Right), the sequence would be different.
  • This traversal is useful when you need to process the nodes in a specific order that prioritizes the right subtree over the left subtree.
  • Binary search trees provide efficient searching, insertion, and deletion operations with an average time complexity of O(log n).
  • Understanding different tree traversal techniques is crucial for various applications in computer science, including parsing expressions and optimizing search algorithms.
Latest NIELIT Scientific Assistant Updates

Last updated on Feb 20, 2025

-> A total number of 113 revised vacancies have been announced for the post of Scientific Assistant in Computer Science (CS), Information Technology (IT), and Electronics & Communication (EC) streams.

-> Online application form, last date has been extended up to from 17th April 2025.

->The NIELT has revised the Essential Qualifications for the post of Scientific Assistant. Candidates must possess (M.Sc.)/ (MS)/ (MCA) / (B.E.)/ (B.Tech) in relevant disciplines.

 

-> The NIELIT Scientific Assistant 2025 Notification has been released by the National Institute of Electronics and Information Technology (NIELIT).

More Linked List Questions

Get Free Access Now
Hot Links: teen patti jodi teen patti wink teen patti - 3patti cards game teen patti real cash 2024