Expected running time

Jul 3, 2013 at 10:24am
Just ask my running time big O over this function

1
2
3
4
5
6
  int Tree: numOfNodes(){ return CountNodes( root ); }

  int CountNodes( Tree * tree ){ 
        if( tree == NULL ) return 0;
        else
            return CountNodes( tree->left ) + CountNodes( tree->right ) + 1;
Jul 3, 2013 at 10:49am
how to define the big o notation for this?
Jul 4, 2013 at 11:38am
no ppl can help me d?
Jul 4, 2013 at 2:47pm
If you're after the algorithmic complexity of your function, rather than the actual running time, then this might help?

Big-Oh for Recursive Functions: Recurrence Relations
https://www.cs.duke.edu/~ola/ap/recurrence.html

Andy
Jul 4, 2013 at 2:48pm
You haven't show the definition of Tree, but from the presence of tree->left and tree->right, I'm inferring this is a binary tree of some sort.

For a binary tree, big O should be:
O(log n)
Jul 4, 2013 at 3:04pm
It visits all the nodes so it is not O(log n).
Last edited on Jul 4, 2013 at 3:07pm
Jul 6, 2013 at 8:40am
so u guys think hats is the answer?
it's visit all the nodes
Jul 6, 2013 at 9:08am
We have not given you the answer in big O notation like you asked for. I'm not going to give it to you because it looks like homework but if you know that it visits all the nodes once it shouldn't be hard to figure out.
Jul 6, 2013 at 10:12am
it's is not an homework . example

to determine the big o for my understanding.
just choose the upper Big O and take its as my complexity

i just want to know since it's not an while loop or for loop.
and i can't really get it how to determine the Big O
the recursive function i search at google

it's O( n . log n )
Jul 6, 2013 at 3:07pm
i might add that the runtime of this algorithm isn't all depending on the complexity,
the algorithm contains a lot of function calls, this will cause considerable overhead.
Topic archived. No new replies allowed.