I am having trouble proving binary tree properties using induction:
Property 1 - A tree with N internal nodes has a maximum height of N+1
base case - 0 internal nodes has a height of 0
assume - a tree with k internal nodes has a maximum height of k+1 where k exists in n
show - true for all k+1 or true for all k-1
Property 2 - A tree with a tree with N internal nodes has N + 1 leaf nodes
base case - 0 internal nodes has 1 leaf node (null)
assume - a tree with k internal nodes has k + 1 leaf nodes where k exists in n
show - true for all k+1 or true for all k-1
Is my setup correct? And if so, how do I actually show these things. Everything I have tried has just ended up becoming a mess. Thanks for the help
You're missing a few things.
For property 1, your base case must be consistent with what you're trying to prove. So a tree with 0 internal nodes must have a height of at most 0+1=1. This is true: consider a tree with only a root.
For the inductive step, consider a tree with k-1 internal nodes. If its height is at most k (assumed), adding one more node can only raise it to k+1, and no higher. So the k-internal-node tree has height at most k+1.
Property 2 is false. Counterexample:
5
4 6
3 7
2 8
5 internal nodes, 2 leaves.
So there's probably something different you wanted to prove.
Related
A weight-balanced tree is a binary tree in which for each node, the number of nodes in the left sub tree is at least half and at most twice the number of nodes in the right sub tree. The maximum possible height (number of nodes on the path from the root to the furthest leaf) of such a tree on n nodes is best described by which of the following?
A. log2(n)
B. log4/3(n)
C. log3(n)
D. log3/2(n)
My Try:
The number of nodes in the left sub tree is at least half and at most twice the number of nodes in the right sub tree.
There n nodes in the tree, one node is root now (n-1) nodes are left. to get the maximum height of the tree we divide these (n-1) nodes in three parts each of size n−13
Now keep two parts in LST and one part in RST.
LST = 2∗(n−1)/3, and
RST=(n−1)/3
Therefore, T(n)= T(2/3(n-1) + (n-1)/3) and for maximum height we will only consider H(n)=H(2/3(n-1))+1
and H(1)=0
I tried to solve the H(n) Recurrence using substitution but i'm stuck at a point:
2^k/3^k(n-k)=1 Here how to solve for k ? Please help
You have done almost correct but there is a mistake in the recursion statement.
It is should be like this : Root + Left Sub Tree + Right Sub Tree.
And if we have taken 1 node for root than it will be 1 and rest remaining nodes will be
(n-1). And from the remaining nodes we have to distribute in such a way that we get
maximum height with the following condition.
Let nodes in LST be : nl and Let nodes in RST be : nr
Equation/Condtion : nr/2 <= nl <= 2*nr
From that we can get the value of nr which will be n-1/3. Refer the figure for
calculation.
Now we came to the point where you have done the mistake.
T(n) = T(n-1/3) + T(2(n-1)/3) + 1 Where T(1) = 1 And T(0) = (0)
And then we will get log 3/2 n in the end.
Figure with calculations :
Given a complete and full binary tree with n nodes, what is the average number of descendants a node has? For example, the root node has n - 1 descendants and each leaf node has 0 descendants, but considering all nodes, what is the average?
Let's change a little your question for a short moment: we call "descendants" will be the number of descendants including the node itself.
A leaf has 1 descendant, its parent has 3 descendants, then 7, then 15, etc. These numbers are of kind 2^k - 1 where k is the number of levels from the bottom of the tree (with k=1 fot the leaves).
For K levels, you have obviously 2^K-1 nodes in your tree; since you called this value n, you have n=2^K-1 and K=log2(n+1).
Still calling a descendant as I previously did (see later for your exact question), you have 1 descendant for 2^(K-1) nodes (the leaves), 3 descendants for 2^(K-2) nodes, ... until n descendants for 2^(K-K)=1 node.
The average number of descendants will be:
Sum(k=1,K, 2^(K-k) * (2^k-1) ) / n
According to your definition of the "descendants" (excluding the node itself) you have to substract 1:
Sum(k=1,K, 2^(K-k) * (2^k-1) ) / n - 1
And by replacing K, you get:
Sum(k=1,log2(n+1), (2^k-1)(n+1) / 2^k ) / n - 1
With the program Pari-GP, I type:
f(n)=sum(k=1,round(log(n+1)/log(2)), (2^k-1)*(n+1) / 2^k ) / n - 1
and I get:
f(1)=0
f(3)=2/3
f(7)=10/7
f(15)=34/15
which looks like A036799(n)/n. If the sequence is actually the same (I didn't check carefully), you could write the simpler expression (without the sum):
f(n)= ((log(n+1)/log(2)-2)*(n+1)+2)/n
I have a tree that has the following form:
On the first pictures, the height of the tree is 1 and there are 3 total nodes. 2 for 7 on the next and 3 for 15 for the last one. How can I determine how many number of nodes a tree of this form of l height will have? Also, what kind of tree is that (is it called something in particular?)?
That is a perfect binary tree
You can get the number of node considering this recursive approch:
n(0) = 1
n(l+1) = n(l) + 2^l
so
n(l) = 2^(l+1) - 1
A complete binary tree at depth ‘d’ is the strictly binary tree, where all the leaves are at level d.
for fig1, d=1
for fig2, d=2
for fig3, d=3
So, Suppose a binary tree T of depth d. Then at most 2(d+1)-1 nodes n can be there in T.
for fig1, d=1; 2(1+1)-1=2(2)-1=4-1=3
for fig2, d=2; 2(2+1)-1=2(3)-1=8-1=7
for fig3 d=3; 2(3+1)-1=2(4)-1=16-1=15
The height(h) and depth(d) of a tree (the length of the longest path from the root to leaf node) are numerically equal.
Here's an answer detailing how to compute depth and height.
The number of nodes in a complete tree is...
n = 2^(h+1) - 1.
What you're describing sounds like "a perfect binary tree".
"a binary tree is a tree data structure in which each node has at most two children"
http://en.wikipedia.org/wiki/Binary_tree
A perfect tree is "A binary tree with all leaf nodes at the same depth."
http://xlinux.nist.gov/dads//HTML/perfectBinaryTree.html
height to maximum number of nodes in perfect binary tree
=2^(height+1)-1
number of nodes to minimum height
=CEILING(LOG(nodes+1,2)-1,1)
Definitions associated with Binary trees can be found at the Wikipedia wiki cited earlier.
This can also be understood like this.
In case of a perfect binary tree
total number of leaf nodes are 2^H (H = Height of the tree)
and total number of internal nodes are 2^H - 1
Hence the total number of nodes will be 2^H + 2^H - 1 which is 2^(H+1) - 1 as mentioned by others.
Hope this will help.
I am a freshman in Computer Science University, so please give me a understandable justification.
I have a binary tree that is equilibrated by height which has 635 nodes. What is the number of comparisons that will occur in the worst case scenario and why?
Here's one way to think about this. Every time you do a comparison in a binary search tree, one of the following happens:
You have walked off the tree. In this case, you're done.
The value you're looking for matches the node you're currently exploring. In this case, you're done.
The value you're looking for does not match the node you're exploring. In that case, you either descend to the left or descend to the right.
The key observation here is that after each step, you either terminate (yay!) or descend lower in the tree. At each point, you make one comparison. Since you can't descend forever, there are only so many comparisons that you can make - specifically, if the tree has height h, the maximum number of comparisons you can make is h + 1, which happens if you do one comparison per level.
In your question, you're given that you have a balanced binary search tree of 635 nodes. It's not 100% clear what "balanced" means in this context, since there are many different ways of determining whether a tree is balanced and they all lead to different tree heights. I'm going to assume that you are given a complete binary search tree, which is one in which all levels except the last are filled.
The reason this is important is that if you have a complete binary search tree of height h, it can have at most 2h + 1 - 1 nodes in it. If we try to solve for the height of the tree in terms of the number of nodes, we get this:
n = 2h+1 - 1
n + 1 = 2h+1
lg (n + 1) = h + 1
lg (n + 1) - 1 = h
Therefore, if you have the number of nodes n, you can determine the minimum height of a complete binary search tree holding n nodes. In your case, n = 635, so we get
lg (635 + 1) - 1 = h
lg (636) - 1 = h
9.312882955 - 1 = h
8.312882955 = h
Therefore, the tree has height 8.312882955. Of course, trees can't have fractional height, so we can take the ceiling to find that the height of the tree would be 9. Since the maximum number of comparisons made is h + 1, there are at most 10 comparisons made when doing a lookup.
Hope this helps!
Without any loss of generality you can say the maximum no. of comparison will be the height of the BST ... you dont have to visit every node in the node because each comparison takes you closer to the node...
Let's say it is a balanced BST (all nodes except last have 2 child nodes).
For instance,
Level 0 --> Height 1 --> Number of nodes = 1
Level 1 --> Height 2 --> Number of nodes = 2
Level 2 --> Height 3 --> Number of nodes = 3
Level 3 --> Height 4 --> Number of nodes = 8
......
......
Level n --> Height n+1 --> Number of nodes = 2^n or 2^(h-1)
Using the above logic, you can derive the search time for best, worst or average case.
Can anybody give me proof how the number of nodes in strictly binary tree is 2n-1 where n is the number of leaf nodes??
Proof by induction.
Base case is when you have one leaf. Suppose it is true for k leaves. Then you should proove for k+1. So you get the new node, his parent and his other leaf (by definition of strict binary tree). The rest leaves are k-1 and then you can use the induction hypothesis. So the actual number of nodes are 2*(k-1) + 3 = 2k+1 == 2*(k+1)-1.
just go with the basics, assuming there are x nodes in total, then we have n nodes with degree 1(leaves), 1 with degree 2(the root) and x-n-1 with degree 3(the inner nodes)
as a tree with x nodes will have x-1 edges. so summing
n + 3*(x-n-1) + 2 = 2(x-1) (equating the total degrees)
solving for x we get x = 2n-1
I'm guessing that what you really want is something like a proof that the depth is log2(N), where N is the number of nodes. In this case, the answer is fairly simple: for any given depth D, the number of nodes is 2D.
Edit: in response to edited question: the same fact pretty much applies. Since the number of nodes at any depth is 2D, the number of nodes further up the tree is 2D-1 + 2D-2 + ...20 = 2D-1. Therefore, the total number of nodes in a balanced binary tree is 2D + 2D-1. If you set n = 2D, you've gone the full circle back to the original equation.
I think you are trying to work out a proof for: N = 2L - 1 where L is the number
of leaf nodes and N is the total number of nodes in a binary tree.
For this formula to hold you need to put a few restrictions on how the binary
tree is constructed. Each node is either a leaf, which means it has no children, or
it is an internal node. Internal nodes have 3
possible configurations:
2 child nodes
1 child and 1 internal node
2 internal nodes
All three configurations imply that an internal node connects to two other nodes. This explicitly
rules out the situation where node connects to a single child as in:
o
/
o
Informal Proof
Start with a minimal tree of 1 leaf: L = 1, N = 1 substitute into N = 2L - 1 and the see that
the formula holds true (1 = 1, so far so good).
Now add another minimal chunk to the tree. To do that you need to add another two nodes and
tree looks like:
o
/ \
o o
Notice that you must add nodes in pairs to satisfy the restriction stated earlier.
Adding a pair of nodes always adds
one leaf (two new leaf nodes, but you loose one as it becomes an internal node). Node growth
progresses as the series: 1, 3, 5, 7, 9... but leaf growth is: 1, 2, 3, 4, 5... That is why the formula
N = 2L - 1 holds for this type of tree.
You might use mathematical induction to construct a formal proof, but this works find for me.
Proof by mathematical induction:
The statement that there are (2n-1) of nodes in a strictly binary tree with n leaf nodes is true for n=1. { tree with only one node i.e root node }
let us assume that the statement is true for tree with n-1 leaf nodes. Thus the tree has 2(n-1)-1 = 2n-3 nodes
to form a tree with n leaf nodes we need to add 2 child nodes to any of the leaf nodes in the above tree. Thus the total number of nodes = 2n-3+2 = 2n-1.
hence, proved
To prove: A strictly binary tree with n leaves contains 2n-1 nodes.
Show P(1): A strictly binary tree with 1 leaf contains 2(1)-1 = 1 node.
Show P(2): A strictly binary tree with 2 leaves contains 2(2)-1 = 3 nodes.
Show P(3): A strictly binary tree with 3 leaves contains 2(3)-1 = 5 nodes.
Assume P(K): A strictly binary tree with K leaves contains 2K-1 nodes.
Prove P(K+1): A strictly binary tree with K+1 leaves contains 2(K+1)-1 nodes.
2(K+1)-1 = 2K+2-1
= 2K+1
= 2K-1 +2*
* This result indicates that, for each leaf that is added, another node must be added to the father of the leaf , in order for it to continue to be a strictly binary tree. So, for every additional leaf, a total of two nodes must be added, as expected.
int N = 1000; insert here the value of N
int sum = 0; // the number of total nodes
int currFactor = 1;
for (int i = 0; i< log(N); ++i) //the is log(N) levels
{
sum += currFactor;
currFactor *= 2; //in each level the number of node is double than the upper level
}
if(sum == 2*N - 1)
{
cout<<"wow that the number of nodes is 2*N-1";
}