Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Wednesday, June 11, 2014

Diameter of a Binary Tree

The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).(reference geeksforgeeks.org)


The diameter of a tree T is the largest of the following quantities:
* the diameter of T’s left subtree
* the diameter of T’s right subtree
* the longest path between leaves that goes through the root of T (this can be computed from the heights of the subtrees of T)

Implementation:
#include<stdio.h>
#include<iostream>
#include<conio.h>

using namespace std;

struct node
{
    int val,leftVal,rightVal;
    node *left;
    node *right;
}*root;

void make_tree(node **temp,int i)
{
    if((*temp) == NULL)
    {
        (*temp) = new node;
        (*temp)->val = i;
        (*temp)->leftVal = (*temp)->rightVal = 0;
        (*temp)->right = (*temp)->left = NULL;
    }

    else if((*temp)->val > i)
    {
        make_tree(&(*temp)->left,i);
    }

    else
    {
        make_tree(&(*temp)->right,i);
    }
}

void post_order(node *temp)
{
    if(temp == NULL)
        return;

    post_order(temp->left);
    post_order(temp->right);
    printf("%i\t",temp->val);
}

void del(node **temp)
{
    if((*temp) == NULL)
        return;

    del(&(*temp)->left);
    del(&(*temp)->right);

    node *ptr = (*temp);
    *temp = NULL;

    getch();

    cout<<ptr->val<<endl;
    delete ptr;

}

void diameter(node *temp, int *max)
{
    if(temp == NULL)
        return;

    else
    {
        diameter(temp->left,max);
        diameter(temp->right,max);

        if(temp->left)
            temp->leftVal = (temp->left->leftVal > temp->left->rightVal) 
? temp->left->leftVal + 1 : temp->left->rightVal + 1;
        else
            temp->leftVal = 0;

        if(temp->right)
            temp->rightVal = (temp->right->leftVal > temp->right->rightVal)
 ? temp->right->leftVal + 1 : temp->right->rightVal + 1;
        else
            temp->rightVal = 0;

        if( *max < temp->rightVal + temp->leftVal + 1)
            *max = temp->rightVal + temp->leftVal + 1;
    }
}
int main()
{
    int n,i,max = -1234;

    cin>>n;

    while(n--)
    {
        cin>>i;

        make_tree(&root,i);
    }

    cout<<"Post - Order Traversal   :   ";

    post_order(root);

    diameter(root,&max);

    cout<<"\nThe Diameter of Given Tree :   "<<max<<"\n";

    cout<<"\nNow, Deleting Nodes :   ";

    del(&root);

    return 0;
}


No comments:

Post a Comment

Your comment is valuable to us