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

Click For Summary

Discussion Overview

The discussion revolves around calculating the time complexity of an algorithm designed to count the number of nodes in a binary tree. Participants explore the implications of different assumptions and methods for determining the complexity, including strong induction and recurrence relations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents an algorithm for counting nodes in a binary tree and questions the complexity, initially guessing O(n log n) but noting that a source claims it is O(n).
  • Another participant suggests using strong induction to prove that the time complexity is O(n), referencing a recurrence relation.
  • Concerns are raised about whether the constant "k" in the recurrence relation is linear with respect to n, with some arguing that it should account for the number of additions performed in the algorithm.
  • Participants discuss the implications of the tree structure on the complexity, noting that in the worst case, the tree could be a full binary tree, affecting the distribution of nodes in subtrees.
  • There is a debate about whether the constant "k" is indeed a constant or if it varies with n, with some asserting it remains constant due to the nature of the operations involved.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the constant "k" and its impact on the overall time complexity. While some support the O(n) conclusion, others argue for O(n log n) based on their interpretations of the algorithm's operations and the structure of the binary tree. The discussion remains unresolved with multiple competing views.

Contextual Notes

Participants highlight the need for careful consideration of the assumptions made about the tree structure and the operations performed in the algorithm. The discussion includes references to specific mathematical properties and induction methods, which may not be universally applicable without further clarification.

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
2K
  • · 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