Program to Find the Maximum Depth and Minimum Depth Of a Tree

Program to Find the Maximum Depth and Minimum Depth Of a Tree 




#include <stdio.h>
#include <stdlib.h>

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

void create()
{
    int data;

    printf("Enter data of node to be inserted : ");
    scanf("%d", &data);
    temp = (struct btnode *)malloc(1*sizeof(struct btnode));
    temp->value = data;
    temp->l = temp->r = NULL;
}


void search(struct btnode *t)
{
    if ((temp->value > t->value) && (t->r != NULL))
        search(t->r);
    else if ((temp->value > t->value) && (t->r == NULL))
        t->r = temp;
    else if ((temp->value < t->value) && (t->l != NULL))
        search(t->l);
    else if ((temp->value < t->value) && (t->l == NULL))
        t->l = temp;
}
void insert()
{
    create();
    if (root == NULL)
        root = temp;
    else
        search(root);
}

void inorder(struct btnode *t)
{
    if (root == NULL)
    {
        printf("No elements in a tree to display");
        return;
    }
    if (t->l != NULL)
        inorder(t->l);
    printf("%d -> ", t->value);
    if (t->r != NULL)
        inorder(t->r);
}

int maxDepth(struct btnode* root)
{
    if(root->l==NULL && root->r==NULL)
    {
        return 1;
    }
    if(root->l!=NULL && root->r==NULL)
    {
        return 1+maxDepth(root->l);
    }
    if(root->r!=NULL && root->l==NULL)
    {
        return 1+maxDepth(root->r);
    }

if(maxDepth(root->l)>maxDepth(root->r))
{
    return maxDepth(root->l)+1;
}
else
{
    return maxDepth(root->r)+1;
}

}

int minDepth(struct btnode *root)
{

    if (root == NULL)
        return 0;

    if (root->l == NULL && root->r == NULL)
       return 1;
    if (root->r!=NULL && root->l==NULL)
       return minDepth(root->r) + 1;

    if (root->l!=NULL && root->r==NULL)
       return minDepth(root->l) + 1;


    if(minDepth(root->l)> minDepth(root->r))
       {
           return (minDepth(root->r)+1);
       }
       else
       {
            return (minDepth(root->l)+1);
       }
}


int main()
{
    int ch;
    int a1,a2;
    while(1)
    {
    printf("\n");
    printf("OPERATIONS ---\n");
    printf("1.Insert an element into tree\n");
    printf("2.Inorder Traversal\n");
    printf("3.Min Depth of a tree\n");
    printf("4.max Depth of a tree\n");
    printf("5 Exit\n");
    printf("Enter your choice : ");
    scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            insert();
            break;
        case 2:
            inorder(root);
            break;
        case 3:
            printf("%d",minDepth(root));
            break;
        case 4:
            printf("%d",maxDepth(root));
            break;
        case 5:
            exit(0);
        default :
            printf("Wrong choice, Please enter correct choice  ");
            break;
        }
    }

    return 0;
}


No comments:

Post a Comment

All Repeated Words In Text File And Their Occurrences In Java

Find All Repeated Words In Text File And Their Occurrences In Java Place your file location in the argument of the FileReader. If t...