Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:47:50+00:00 2026-05-23T02:47:50+00:00

Here is the code of binary search tree #include<stdio.h> #include<conio.h> #includemalloc.h struct node {

  • 0

Here is the code of binary search tree

#include<stdio.h>
#include<conio.h>
#include"malloc.h"

struct node
{
    int data;
    struct node* left;
    struct node* right;
};
int size(struct node* n)
{
    if(n==NULL)
       return 0;
    else
       return (size(n->left)+1+size(n->right));
}

int maxdepth(struct node* n)
{
    int ldepth,rdepth;
    if(n==NULL)
    {
       return 0;
    }
    else
    {
       ldepth=maxdepth(n->left);
       rdepth=maxdepth(n->right);
       if(ldepth>rdepth)
          return (ldepth+1);
       else
          return (rdepth+1);
    }
}

int lookup(struct node* node,int target)
{
    if(node==NULL)
       return 0;
    else if(target==node->data)
       return 1;
    else if(target<node->data)
       return(lookup(node->left,target));
    else
       return(lookup(node->right,target));
}

struct node* newnode(int data)
{
     struct node* newnod=(struct node*)malloc(sizeof(struct node));
     newnod->data=data;
     newnod->left=NULL;
     newnod->right=NULL;
     return newnod;
}

struct node* insert(struct node* root,int target)
{
    if(root==NULL)
        return(newnode(target));
    else if(target<=root->data)
        root->left=insert(root->left,target);
    else 
        root->right=insert(root->right,target);
    return root;
}

void main()
{
    int result,s,max;
    struct node* newnode=NULL;
    clrscr();
    newnode=insert(newnode,2);
    newnode=insert(newnode,3);
    newnode=insert(newnode,4);
    max=maxdepth(newnode);
    printf("maxdepth %d\n",max);
    s=size(newnode);
    result=lookup(newnode,3);
    printf("size %d\n",s);
    printf("%d",result);
    getch();
}

When I runs this program . I get maxdepth has 3.

If I change the maxdepth function as

int maxdepth(struct node* n)
{
    int ldepth,rdepth;
    if(n==NULL)
    {
        return 0;
    }
    else
    {
        ldepth=maxdepth(n->left);
        rdepth=maxdepth(n->right);
        if(ldepth>rdepth)
            return (ldepth);
        else
            return (rdepth);
    }
}

I get the maxdepth value as 0. what is the problem? I couldn’t not figure it out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:47:51+00:00Added an answer on May 23, 2026 at 2:47 am

    You are not counting the current node, so a +1 is needed.

          {
            ldepth = maxdepth(n->left);
            rdepth = maxdepth(n->right);
    
            if(ldepth > rdepth)
              return ldepth + 1;
            else
              return rdepth + 1;
          }
    

    Without the +1 maxdepth always will return 0. Because ldepth and rdepth will always be 0.

    An example of a tree with 3 nodes:

       A
     /   \
    B     C
    

    Now you call maxdepth(A), this will do: ldepth = maxdepth(B); rdepth = maxdepth(C);, then maxDepth(B) will do: ldepth = maxdepth(null); rdepth = maxdepth(null); /* ldepth and rdepth are now 0 */, so maxDepth(B) will return as a result 0. Similar maxDepth(C) will return 0. You will do then:

    if(ldepth > rdepth)
      return ldepth;
    else
      return rdepth;
    

    But both ldepth and rdepth are 0, so rdepth will be returned which is 0. Finally maxdepth(A) will return 0 as a result.

    That’s why +1 is needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a binary search tree. Here is the code I
Here is my code. The complete binary tree has 2^k nodes at depth k.
I am trying to search through a binary search tree and store each node
I am trying to traverse Binary search tree with the follwoing code and my
I am implementing a Binary Search tree in C++ I have the following code
Here is the situation, I am developing a binary search tree and in each
I have to implement a binary search tree using C++ for one of assignments.
the code below belongs to binary search algorithm,user enter numbers in textbox1 and enter
I have two questions about binary search trees - one about the code I
I was trying to implement binary search tree but I think I have made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.