How would I go about solving this problem: Use big O notation to give the number of nodes that can be contained in a tree of depth n
I'm not looking for the exact answer or anything, Just how I would go about to work out the answer?
How would I go about solving this problem: Use big O notation to give the number of nodes that can be contained in a tree of depth n
I'm not looking for the exact answer or anything, Just how I would go about to work out the answer?
This was already alluded to in another answer, but specifically there are two relevant quantities in determining the big-O notation for this problem.
It's already obvious that if the tree has an infinite depth, then there will be an infinite number of nodes. What big-O notation is really trying to capture here is the rate at which the number of nodes in the tree will grow to infinity.
Since each layer of the tree will have approximately k
times the number of nodes in the previous layer. In the first layer let's say we have some number a
nodes, in the second layer then we have k*a
nodes, and in the third layer we have k*k*a
nodes.
One important piece about complexity is that constants (like a) don't really matter in the grand scheme of things, since 2*infinity
is still infinity. Therefore the relevant progression in the previous step through is:
1 -> k -> k*k ->...
The function seems to be of the form, from these few examples, of k^something
, you said not to provide the answer, so I won't directly give the answer, but I think this answer puts your right on the doorstep of it.
Good luck!
First you must can find a function f(n) representing the number of nodes that can contain in a tree of depth n, then you derive big O from n.
http://en.wikipedia.org/wiki/Big_O_notation
A simple way to derive big O from f(n) is as follow, as noted in the above link:
If f(x) is a sum of several terms, the one with the largest growth rate is kept, and all others omitted.
If f(x) is a product of several factors, any constants (terms in the product that do not depend on x) are omitted.
So if you found f(n) = 100 * 2^n + 100000000 * n^ 2 + 2000000000000000000000000
then big O will be O(2^n)
You need to find function which represents the upper bound of number of nodes in tree of depth n. So you need to look at the case where every node has maximal available number of childs. In case of binary tree every node has to have 2 childs so just draw some binary tree's and look for scheme. Look at the function below:
f(n) = number of nodes in depth n
f(1) = 1
f(2) = 2
f(3) = 4
f(4) = 8
f(n) = 2 * f(n-1) = 2 * 2 * f(n-2) = 2 * 2 * 2 * f(n-3) = 2*2*2*2...*2*f(1)
(you multiply by two n-1 times ) 2^(n-1) * 1 = 2^(n-1)
So to get upper bound of nodes in binary tree of depth n you need to sum these values:
Sum of f(n) for n from [1, n] = Sum of 2^(n-1) where n is from 1 to n = 2^(N) - 1
So the maximum number of nodes in binary tree is bounded by 2^(n) - 1 where n is depth of binary tree which means it's belong asomptically to O(2^n). (in big-Oh notation you drop every constant).