int countleaves(struct node* root){
if(root!=null)
{
countleaves(root->left);
if(root->left==NULL&&root->right==NULL)
{
count++;
}
countleaves(root->right);
}
}
Use the following formula: (2^n)-1. E.g., if the depth is 3, the number of nodes is (2^3)-1 = 8-1 = 7. Note that 7 is the maximum number of nodes, not the actual number of nodes. To count the actual nodes you must traverse the tree, updating an accumulator as you go.
The height of a complete binary tree is in terms of log(n) where n is the number of nodes in the tree. The height of a complete binary tree is the maximum number of edges from the root to a leaf, and in a complete binary tree, the number of leaf nodes is equal to the number of internal nodes plus 1. Since the number of leaf nodes in a complete binary tree is equal to 2^h where h is the height of the tree, we can use log2 to find the height of a complete binary tree in terms of the number of nodes.
Same as if two nodes are NOT equal in size. Size of nodes has nothing to do where to insert a new element. The insertion should be applying the search algorithm of that binary tree (so the new inserted element maybe found later). For balanced (in size) binary tree, the above still applied, because 50% of the time the tree is unbalanced (a binary tree with even number of elements is not balanced). Plus, those 2 nodes, may not be the "right" and "left" nodes of a given one, so 2 nodes equals in size has nothing to do with the way the elements being inserted into a binary tree.
int numNodes = 0; Node current = root; // if root is null, the number of nodes is 0 if(current != null) { // if root is not null, we have at least one node numNodes = 1; // count all nodes while(current .next != null) { ++numNodes; current = current.next; } }
12
The number of branches is 8.
level of a node in any binary tree can be calculated by summing up the number of nodes traversed from the root node of the tree to the node whose level has to be calculated!!!! dats it!! if count is the no. of elements passed, then floor(log2(count-1)) is the level
In general: There are 2n-1 nodes in a full binary tree. By the method of elimination: Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer is 15. niraj
Traverse the nodes from the beginning to the end, counting the nodes as you go. This takes linear time O(n) to count n nodes. A more efficient approach that enables constant-time O(1) counting is to encapsulate the list along with a node counter. Increment the counter each time a node is inserted and decrement when a node is extracted.
if u assign a 0th level to root of binary tree then,the minimum no. of nodes for depth K is k+1.
If the number of levels is L, the maximum number of nodes N in a binary tree is N = 2L-1. For L = 5, N equates to 31 thus.
2n-1