Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Trees
Home > Programs > Trees

45. Write a program to find the number of elements present in the tree

Answer:

#include <stdio.h>
#include <stdlib.h> 
/*Structure of node */
struct btnode 
{
    int value;
    struct btnode *l;
    struct btnode *r;
};
void createbinary();
void preorder(node *);
int count(node*);
node* add(int); 
typedef struct btnode node;
node *ptr, *root = NULL;
int  main()
{
    int c; 
    createbinary();
    preorder(root);
    c = count(root);
    printf("\nNumber of nodes in binary tree are:%d\n", c);
}
/*
  constructing the following binary tree
      50
      / \
     20 30
      / \ 
     70 80
     / \  \
    10 40 60
 */    
void createbinary()
{
    root = add(50);
    root->l = add(20);
    root->r = add(30);
    root->l->l = add(70);
    root->l->r = add(80);
    root->l->l->l = add(10);
    root->l->l->r = add(40);
    root->l->r->r = add(60);
}
 
/*Add the node to binary tree*/
node* add(int val)
{
    ptr = (node*)malloc(sizeof(node));
    if (ptr == NULL)
    {
        printf("Memory was not allocated");
        return;
    }
    ptr->value = val;
    ptr->l = NULL;
    ptr->r = NULL;
    return ptr;
}
 
/*counting the number of nodes in a tree*/
int count(node *n)
{
    int c = 1;
 
    if (n == NULL)
        return 0;
    else
    {
        c += count(n->l);
        c += count(n->r);
        return c;
     }
}
/*Displaying the nodes of tree in preorder*/
void preorder(node *t)
{
    if (t != NULL)
    {
        printf("%d->", t->value);    
        preorder(t->l);
        preorder(t->r);
    }
 }

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here: