answersLogoWhite

0

The number of nodes in any subtree is the number of nodes in its left subtree, plus the number of nodes in its right subtree, plus one, so you can use a recursive algorithm and start at the root.

unsigned intbinarytree_count_recursive(const node *root)

{

unsigned int count = 0;

if (root != NULL) {

count = 1 + binarytree_count_recursive(root->left)

+ binarytree_count_recursive(root->right);

}

return count;

}

User Avatar

Wiki User

14y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
JudyJudy
Simplicity is my specialty.
Chat with Judy
LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
More answers

int count (const Tree *t)

{

int sum;

if (!t) return 0;

else sum=1;

if (t->left) sum += count (t->left);

if (t->right) sum += count (t->right);

return sum;

}

advanced:

int count (const Tree *t)

{

int sum= 0;

while (t) {

sum++;

if (t->left) {

if (t->right) sum += count (t->right);

t = t->left;

} else t = t->right;

}

return sum;

}

User Avatar

Wiki User

15y ago
User Avatar

#include <stdio.h>

#include <stdlib.h>

/* A binary tree node has data, pointer to left child

and a pointer to right child */

struct node

{

int data;

struct node* left;

struct node* right;

};

/* Function to get the count of leaf nodes in a binary tree*/

unsigned int getLeafCount(struct node* node)

{

if(node NULL && node->right==NULL)

return 1;

else

return getLeafCount(node->left)+

getLeafCount(node->right);

}

/* Helper function that allocates a new node with the

given data and NULL left and right pointers. */

struct node* newNode(int data)

{

struct node* node = (struct node*)

malloc(sizeof(struct node));

node->data = data;

node->left = NULL;

node->right = NULL;

return(node);

}

/*Driver program to test above functions*/

int main()

{

/*create a tree*/

struct node *root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

/*get leaf count of the above created tree*/

printf("Leaf count of the tree is %d", getLeafCount(root));

getchar();

return 0;

}

User Avatar

Wiki User

12y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Program for count the total number of node in binary tree?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Program to count number of leaf node in binary tree in c plus plus?

Add the following recursive method to your binary tree's node class: size_t Node::count_leaves() { if (!left &amp;&amp; !right) return 1; // this node is a leaf size_t count = 0; if (left) count += left-count_leaves(); // count leaves on left if (right) count += right-leaves(); // count leaves on right; return count; // return total leaves. } To count the leaves of the entire tree, call the method on the root node of the tree. To count the leaves of a subtree, call the method on the root node of the subtree.


What is the algorithm for finding the number of binary digits required to represent a positive decimal integer?

Algorithm bit_count is:Input: an integer n, such that n >= 0Output: the minimum number of binary digits (bits) required to represent nlet count := 0;repeat {n := n / 2; // integer division: ignore any remaindercount := count + 1;} until n = 0;return count;This works because each binary digit represents an increasing power of 2, starting from 2^0. Thus if we repeatedly divide a value by 2 until the result is 0, the total number of divisions tells us the minimum number of bits required to represent the value.Take the value 42 as an example:42 / 2 = 2121 / 2 = 10 (r 1)10 / 2 = 55 / 2 = 2 (r 1)2 / 2 = 11 / 2 = 0 (r 1)That's 6 divisions in total so we need at least 6 binary digits to represent 42 in binary. Given that 42 is 101010 in binary, this is correct.Let's try 31:31 / 2 = 15 (r 1)15 / 2 = 7 (r 1)7 / 2 = 3 (r 1)3 / 2 = 1 (r 1)1 / 2 = 0 (r 1)5 divisions so 5 bits. 31 in binary is 11111, so that's also correct. 11111 is also the maximum value we can represent with just 5 bits so it follows that 32 would need 6 bits:32 / 2 = 1616 / 2 = 88 / 2 = 44 / 2 = 22 / 2 = 11 / 2 = 0 (r 1)QED


How do you write an if-else statement that increments the value of count by 1 if the value of total is evenly divisible by four or five and increments the value of count by 2 otherwise?

Here's a code snippet that solves that problem. if((total%5==0) (total%4==0)) { count += 1; } else { count += 2; }


Write c plus plus program to find all number from 112 to 212 with the cumulative total?

int total = 0; int n; for( n = 112; n &lt;= 212; ++n) { total += n; } printf("%d\n", total);


A full node is a node with two children Prove that the number of full nodes plus one is equal to the number of leaves in a binary tree?

Let N = the number of nodes, F = number of full nodes, L = the number of leaves, and H = the number of nodes with one child (or half nodes). The total number of nodes in a binary tree equals N = F + H + L. Because each full node is incident on two outgoing edges, each half node is incident on one outgoing edge, and each leaf is incident on no outgoing edge it follows that the total number of edges in a binary tree equals 2F + H. It is also true that the total number of edges in a tree equals N 1. Thus, 2F + H = N 1 H = N 1 2F Subbing this value of H into N = F + H + L gives, N = F + N 1 2F + L N N + 1 = F + L F + 1 = L