Thursday 16 November 2017

C Program to Find 2 Nodes with Longest Distance and Display using Inorder Traversal


Code:

#include   stdio.h
#include   stdlib.h

struct btnode
{
   int value;
   struct btnode *left, *right;
};
typedef struct btnode node;

/* function prototypes */
void insert(node *, node *);
void inorder(node *);
int  height(node *);

node *temp, *root = NULL;

void main()
{
    node *new = NULL ;
    int num  = 1;

    printf("Enter the elements of the tree(enter 0 to exit)\n");
    while (1)
    {
        scanf("%d",  &num);
        if (num == 0)
            break;
        new  =  malloc(sizeof(node));
        new->left  =  new->right  =  NULL;
        new->value  =  num;
        if (root  == NULL)
            root  =  new;
        else
        {
            insert(new, root);
        }
    }
    printf("elements in a tree in inorder are\n");
    inorder(root);
    height(root);
}

/* displaying nodes of a tree using inorder */
void inorder(node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d -> ", root->value);
        inorder(root->right);
    }
}

/* inserting nodes into a tree */
void insert(node * new , node *root)
{
    if (new->value>root->value)
    {
        if (root->right  == NULL)
            root->right  =  new;
        else
            insert(new, root->right);
    }
    if (new->valuevalue)
    {
        if (root->left  == NULL)
            root->left = new;
        else
            insert(new, root->left);
    }
}

/* to find the longest path */
int height(node *temp)
{
    temp = root;

    if (temp  == NULL)
        printf("tree is empty\n");
    else
    {
        printf("\nlongest path is\n");
        while (temp->left != NULL)
        {
            if (temp->left == NULL)
                temp = temp->right;
            else
                temp = temp->left;
        }
        printf("%d ->", temp->value);
        temp = root;
        while (temp->right != NULL)
        {
              if (temp->right  == NULL)
                temp = temp->left;
            else
                temp = temp->right;
        }
        printf(" %d", temp->value);
    }
}



Output:

Enter the elements of the tree(enter 0 to exit)
40
20
30
50
60
80
90
0
elements in a tree in inorder are
20 -> 30 -> 40 -> 50 -> 60 -> 80 -> 90
longest path is
20 -> 90




More C Programs:











100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...