Saturday, December 13, 2014

Sum of All Nodes in a Binary in c



Search for a Particular Value in a Binary Tree
#include 
#include 
#include 
 
struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
};
 
struct btnode *root = NULL;
int flag;
 
void in_order_traversal(struct btnode *);
void in_order_search(struct btnode *,int);
struct btnode *newnode(int);
 
void main()
{ 
    
    int search_val;
    root = newnode(50);
    root->l = newnode(20);
    root->r = newnode(30);
    root->l->l = newnode(70);
    root->l->r = newnode(80);
    root->l->l->l = newnode(10);
    root->l->l->r = newnode(40);
    root->l->r->r = newnode(60);
 
    printf("The elements of Binary tree are:");
    in_order_traversal(root);
    printf("Enter the value to be searched:");
    scanf("%d", &search_val);
    in_order_search(root, search_val);
    if (flag  =  =  0)    
    {
        printf("Element not present in the binary tree\n");
    }
}
 
struct btnode* newnode(int value)
{
    struct btnode *temp = (struct btnode *)malloc(sizeof(struct btnode));
    temp->value = value;
    temp->l = NULL;
    temp->r = NULL;
    return temp;
}
 
 
void in_order_traversal(struct btnode *p)
{
    if (!p)
    {
        return;
    }
    in_order_traversal(p->l);
    printf("%d->", p->value);
    in_order_traversal(p->r);
}
 
void in_order_search(struct btnode *p, int val)
{
    if (!p)
    {
        return;
    }
    in_order_search(p->l, val);
    if(p->value == val)
    {
        printf("\nElement present in the binary tree.\n");
        flag = 1;
    }
    in_order_search(p->r, val);
}

Output
 
The elements of Binary tree are:10->70->40->20->80->60->50->30
Enter the value to be searched:60
Element present in the binary tree.
 
The elements of Binary tree are:10->70->40->20->80->60->50->30
Enter the value to be searched:99
Element not present in the binary tree
 
Find the Sum of All Nodes in a Binary Tree
 
#include 
#include 
#include 
 
struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
};
struct btnode *root = NULL;
int sum;
 
 
void in_order_traversal(struct btnode *);
void in_order_sum(struct btnode *);
struct btnode *newnode(int);
 
void main()
{ 
 
    
    root = newnode(50);
    root->l = newnode(20);
    root->r = newnode(30);
    root->l->l = newnode(70);
    root->l->r = newnode(80);
    root->l->l->l = newnode(10);
    root->l->l->r = newnode(40);
    root->l->r->r = newnode(60);
    printf("The elements of Binary tree are:");
    in_order_traversal(root);
    in_order_sum(root);
    printf("\nThe sum of all the elements are:%d", sum);
}
 
struct btnode* newnode(int value)
{
    struct btnode *temp = (struct btnode *)malloc(sizeof(struct btnode));
    temp->value = value;
    temp->l = NULL;
    temp->r = NULL;
    return temp;
}
 
void in_order_traversal(struct btnode *p)
{
    if (!p)
    {
        return;
    }
    in_order_traversal(p->l);
    printf("%d->",  p->value);
    in_order_traversal(p->r);
}
 
void in_order_sum(struct btnode *p)
{
    if (!p)
    {
        return;
    }
    in_order_sum(p->l);
    sum = sum + p->value;
    in_order_sum(p->r);
}

Output
 
The elements of Binary tree are:10->70->40->20->80->60->50->30
The sum of all the

No comments:

Post a Comment