Multiple choice

What is the running time of the algorithm?

An iterative algorithm to search for key k in a tree T is given below. Assume that the tree has n nodes and height h.
search(T,k)
{
x = root(t);
while(x!=null or x!= k)
{
if(x < k) x= right[x];
else x = left[x];
}
return x;
}

  1. O(n)

  2. O(n2)

  3. O(nh)

  4. O(h)

  5. O(h2)

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

 This is correct. Since the height of the tree is h, the search process will go upto height h only.