Complexity of Algorithm to calculate number of nodes in a binary tree

Click For Summary
SUMMARY

The algorithm for calculating the number of nodes in a binary tree is defined as follows: int CN(struct node *node), which recursively counts nodes by returning 1 plus the counts of the left and right subtrees. The time complexity of this algorithm is definitively O(n), as proven through strong induction, where the recurrence relation is expressed as T(n) = T(n/2) + T(n/2) + k. Despite initial assumptions of O(n log n) due to the number of additions, the constant overhead does not scale with n, confirming that the overall complexity remains linear.

PREREQUISITES
  • Understanding of binary trees and their properties
  • Familiarity with recursive algorithms and their execution
  • Knowledge of time complexity analysis and Big O notation
  • Experience with mathematical induction as a proof technique
NEXT STEPS
  • Study the principles of mathematical induction in algorithm analysis
  • Learn about different types of binary trees, including full and balanced trees
  • Explore other recursive algorithms and their time complexities
  • Investigate the implications of constant factors in time complexity
USEFUL FOR

Computer science students, software developers, and algorithm enthusiasts who are interested in understanding recursive algorithms and their performance characteristics in binary tree structures.

Chinta
Messages
8
Reaction score
0
I guess this is the first question on partly CS topic in this forum. But I think you guys will be able to help me.

I have an algorithm which goes as follows:

int CN(struct node *node)
{
if(node==null)
return 0;
return 1 + CN(node->left) + CN(node->right);
}

My question is that how to calculate the complexity of the above code and what is the complexity in terms of number of nodes n.
The answer that I'm guessing is O(nlogn). But the answer is given as O(n); I'm clueless how to approach to get O(n)?
 
Technology news on Phys.org
Chinta said:
I guess this is the first question on partly CS topic in this forum. But I think you guys will be able to help me.

I have an algorithm which goes as follows:

int CN(struct node *node)
{
if(node==null)
return 0;
return 1 + CN(node->left) + CN(node->right);
}

My question is that how to calculate the complexity of the above code and what is the complexity in terms of number of nodes n.
The answer that I'm guessing is O(nlogn). But the answer is given as O(n); I'm clueless how to approach to get O(n)?

Lets assume this is a binary tree, and we have decided on worst case. Suppose that \(t(n)\) is the worst case time for a tree of \(n\)-nodes, then strong induction can be used to show that \(t(n) \in O(n)\) using the fact that \(t(n)=k+t(a)+t(n-a-1)\) for some constant \(k\) and \(a\) nodes in the r-tree and \(n-a\) in the l-tree.

That is we are not going to calculate the complexity we are going to prove it is \(O(n)\).

CB
 
As you said that let's assume this as a binary tree, then we have a=(n/2) and (n-a-1)=(n/2)-1.
Then T(n) can be written as follows:

T(n)=T(n/2) + T{(n/2) -1} + k
=2*T(n/2) + k
as a result of which
T(n)
∈ O(n) .


But in the worst case is "k" not a linear function of n? Assuming each addition takes constant time c, in the worst case it can become "c*(n-1)" [because #additions=(n-1) in worst case,where n = #nodes] whence-forth this recurrence becomes T(n)=2*T(n/2)+k=2*T(n/2) + c(n-1) => T(n) ∈ O(nlogn).

I don't know whether this explanation is right or not..please help!
 
Last edited:
CaptainBlack said:
Suppose that \(t(n)\) is the worst case time for a tree of \(n\)-nodes, then strong induction can be used to show that \(t(n) \in O(n)\) using the fact that \(t(n)=k+t(a)+t(n-a-1)\) for some constant \(k\) and \(a\) nodes in the r-tree and \(n-a\) in the l-tree.
In order to prove that t(n) is O(n) by induction on n we need to consider some different property P(n); then we prove ∀n P(n) by induction, and "t(n) is O(n)" is going to be a simple corollary of ∀n P(n). This is because we cannot say, e.g., t(0) is O(n): for such statement we have to consider a complete function, not just its individual value. We can, for example, say that there exists a constant C such that t(n) <= C * n.

Chinta said:
As you said that let's assume this as a binary tree, then we have a=(n/2) and (n-a-1)=(n/2)-1.
The fact that the tree is binary does not mean that both subtrees have an (almost) equal number of leaves.

Chinta said:
But in the worst case is "k" not a linear function of n? Assuming each addition takes constant time c, in the worst case it can become "c*(n-1)" [because #additions=(n-1) in worst case,where n = #nodes]
What do you mean by an addition?

I would first prove that the body of the function CN is executed exactly n times where n is the number of nodes. This can be proved by strong induction n as CB said. Each execution of CN, given the results of the recursive calls, takes a constant time, from where it follows that there exists a constant C such that t(n) <= C * n.
 
Evgeny.Makarov said:
In order to prove that t(n) is O(n) by induction on n we need to consider some different property P(n); then we prove ∀n P(n) by induction, and "t(n) is O(n)" is going to be a simple corollary of ∀n P(n). This is because we cannot say, e.g., t(0) is O(n): for such statement we have to consider a complete function, not just its individual value. We can, for example, say that there exists a constant C such that t(n) <= C * n.

I did not want to be too specific, but what I envisaged for the induction step is the assumption that there is some constant \(C\) such that for all \(n\le N\) for some \(N \in \mathbb{N} \) we had \(t(n)<Cn\). Then when we choose any \( C\ge k \) induction on \(N\) should do the trick, since then we would have proven that there exists a \(C\) such that \(t(r)<C\times r\) for all \(r \in \mathbb{N}\).

CB
 
Last edited:
I am not concerned about the inductive proof. All I want to know is that whether the constant "k" will be there or not? Because there are (n-1) additions being performed for the statement 1 + CN(node->left) + CN(node->right) and assuming each addition takes "c" unit time then we are getting c(n-1) instead of the constant k in the recurrence t(n)= k+t(n-a)+t(n-a-1). That is what leads to O(nlogn) time,otherwise the time is linear i.e. O(n).

Another thing is that in the worst case the binary tree would have been a full binary tree,in which the number of nodes in the left tree and right tree would almost be the same resulting to "a=n/2" almost.

Help me if I'm wrong somewhere!
 
Last edited:
Chinta said:
I am not concerned about the inductive proof. All I want to know is that whether the constant "k" will be there or not? Because there are (n-1) additions being performed for the statement 1 + CN(node->left) + CN(node->right) and assuming each addition takes "c" unit time then we are getting c(n-1) instead of the constant k in the recurrence t(n)= k+t(n-a)+t(n-a-1). That is what leads to O(nlogn) time,otherwise the time is linear i.e. O(n).

Another thing is that in the worst case the binary tree would have been a full binary tree,in which the number of nodes in the left tree and right tree would almost be the same resulting to "a=n/2" almost.

Help me if I'm wrong somewhere!

The constant represents the overheads and common operations of counting a root node and calling the counting routine with the left and right subtrees.

until proven otherwise tiy do not know that the worst case timing of an n-node tree is for a full tree.

CB
 
So is k a constant at all or a linear function of n?
 
Chinta said:
So is k a constant at all or a linear function of n?

It is a constant (it does not depend on n since all you are doing is some pointer manipulation and calling functions etc).

CB
 
  • #10
Many thanks...I had a wrong concept about this thing. You made it clear.

(Whew)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
5K