Data Structure Questions and Answers-Binary Search Tree

YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

CLICK HERE TO DOWNLOAD

Data Structure Questions and Answers-Binary Search Tree

Question 1 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which of the following is false about a binary search tree?
A
The left child is always lesser than its parent
B
The right child is always greater than its parent
C
The left and right sub-trees should also be binary search trees
D
None of the mentioned
Question 1 Explanation: 
All the options hold good for a binary search tree and can be considered as a definition for a BST.

Question 2 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
How to search for a key in a binary search tree?
A

public Tree search(Tree root, int key) { 	if( root == null || root.key == key ) { 		return root; 	} 	if( root.key < key ) { 		return
B

public Tree search(Tree root, int key) { 	if( root == null || root.key == key ) { 		return root; 	} 	if( root.key < key ) { 		return
C

public Tree search(Tree root, int key) { 	if( root == null) { 		return root; 	} 	if( root.key < key ) { 		return search(root.right, key
D
None of the mentioned
Question 2 Explanation: 
As we know that the left child is lesser than the parent, if the root's key is greater than the given key, we look only into the left sub-tree, similarly for right sub-tree.

Question 3 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the speciality about the inorder traversal of a binary search tree?
A
It traverses in a non increasing order
B
It traverses in an increasing order
C
It traverses in a random fashion
D
None of the mentioned
Question 3 Explanation: 
As a binary search tree consists of elements lesser than the node to the left and the ones greater than the node to the right, an inorder traversal will give the elements in an increasing order.

Question 4 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What does the following piece of code do?

public void func(Tree root) { 	func(root.left()); 	func(root.right()); 	System.out.println(root.data()); }
A
Preorder traversal
B
Inorder traversal
C
Postorder traversal
D
Level order traversal
Question 4 Explanation: 
In a postorder traversal, first the left child is visited, then the right child and finally the parent.

Question 5 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What does the following piece of code do?

public void func(Tree root) { 	System.out.println(root.data()); 	func(root.left()); 	func(root.right()); }
A
Preorder traversal
B
Inorder traversal
C
Postorder traversal
D
Level order traversal
Question 5 Explanation: 
In a preorder traversal, first the parent is visited, then the left child and finally the right child.

There are 5 questions to complete.