0

I am trying to calculate complexity of a recursion method inside another methos in a simple while loop.

I dont know how to asnwer this question, I think To just apply the answer O(N*Y) while Y stands for the tree on the recursion.

Tree recursion that gets the number of leafs that bigger Than x:

public static int bigger(BinaryNode<Integer> t, int x)
{
 if(t==null)
 {
 return 0;
 }
 if (t.getValue()>x) {
 return 1;
 }
 return bigger(t.getLeft(),x) + bigger(t.getRight(),x);
}

This is the Method I need to calculate the Complexity for(uses Bigger() in each iteration):

public static Stack<Item> bigInTree(BinaryNode<Integer> bt,Stack<Integer> s) {
 Stack<Item> n = new Stack<>();
 while (!s.isEmpty())
 {
 n.push(new Item(s.top(),bigger(bt,s.top())));
 s.pop();
 }
 return n;
}

Is it O(N^2) or can i simply call it O(N*Y) (Y= number of leafs on t)

Let me know what you think

asked Jul 27, 2022 at 19:41
2
  • 1
    O(Number of elements in the tree * Number of the elements in the stack). You can give those things whatever identifiers you want. e.g. O(N*M) Commented Jul 27, 2022 at 19:45
  • Thank you so much! I just needed someone to confirm♥ Commented Jul 27, 2022 at 19:49

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.