adplus-dvertising
frame-decoration

Question

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 search(root.right,key);
 }
 else
 return search(root.left,key);
}

b)

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

c)

public Tree search(Tree root, int key)
{
 if( root == null)
        {
  return root;
 }
 if( root.key < key )
        {
  return search(root.right,key);
 }
 else
  return search(root.left,key);
}

d) None of the mentioned

a.

a

b.

b

c.

c

d.

d

Answer: (a).a

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. How to search for a key in a binary search tree?

Similar Questions

Discover Related MCQs

Q. What is the speciality about the inorder traversal of a binary search tree?

Q. What are the worst case and average case complexities of a binary search tree?

Q. What are the conditions for an optimal binary search tree and what is its advantage?

Q. What is an AVL tree?

Q. Why we need to a binary tree which is height balanced?

Q. What is the maximum height of an AVL tree with p nodes?

Q. To restore the AVL property after inserting a element, we start at the insertion point and move towards root of that tree. is this statement true?

Q. Given an empty AVL tree, how would you construct AVL tree when a set of numbers are given without performing any rotations?

Q. What maximum difference in heights between the leafs of a AVL tree is possible?

Q. Why to prefer red-black trees over AVL trees?

Q. What is a Cartesian tree?

Q. Which of the below statements are true:

i.Cartesian tree is not a height balanced tree

ii.Cartesian tree of a sequence of unique numbers can be unique generated

Q. What is the speciality of cartesian sorting?

Q. Consider a sequence of numbers to have repetitions, how a cartesian tree can be constructed in such situations without violating any rules?

Q. After applying the below operations on a input sequence, what happens?

i. construct a cartesian tree for input sequence

ii. put the root element of above tree in a priority queue

iii. if( priority queue is not empty) then

.search and delete minimum value in priority queue

.add that to output

.add cartesian tree children of above node to priority queue

Q. Cartesian trees are most suitable for?

Q. A treap is a cartesian tree with

Q. Cartesian trees solve range minimum query problem in constant time

Q. Consider below sequences:

array=60 90 10 100 40 150 90

reverse 2 to 3

array=60 10 90 100 40 150 90

reverse 3 to 6

array= 60 100 150 40 100 90 90

now printout from 1 to 6 :-- 60 100 150 40 100 90

How to achieve the above operation efficiently?

Q. What is a weight balanced tree?