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 7628531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:36:25+00:00 2026-05-31T05:36:25+00:00

Here is the code that I have written to validate a BST. Is it

  • 0

Here is the code that I have written to validate a BST.

Is it correct? If not, how would I do this?

int validate(node *root)
{
    if(root==NULL) return 1;
    else if(root->lchild!=NULL && (root->lchild)->data >=root->data) return 0;
    else if(root->rchild!=NULL && (root->rchild)->data <=root->data) return 0;
    validate(root->lchild);
    validate(root->rchild);
    return 1;
}
  • 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-31T05:36:26+00:00Added an answer on May 31, 2026 at 5:36 am

    Let’s say you have the following tree:

          20
         /  \
       10    30
      /
    15
    

    and start at the root with your code:

    1 int validate(node *root) {
    2     if(root==NULL) return 1;
    3     else if(root->lchild!=NULL && (root->lchild)->data >=root->data) return 0;
    4     else if(root->rchild!=NULL && (root->rchild)->data <=root->data) return 0;
    5     validate(root->lchild);
    6     validate(root->rchild);
    7     return 1;
    8 }
    

    Now the first three if statements (lines 2 through 4) don’t “fire” at the root node because the first two levels of the tree are okay (the left node is less than 20 and the right node is greater than 20). So you then try to validate the left subtree (the node containing 10) by a recursive call at line 5.

    In that call, it’s not okay since its left node (15) is larger than it is and line 3 will return zero to indicate it’s bad.

    However, because you’ve called validate and thrown away the return value, it simply carries on to line 6 then eventually returns 1 on that final line 7, even though the tree is not valid.

    What you need to do is to to pass up the lower level results to the upper levels so that they can be acted upon.

    You can also get rid of those else if things since they’re useless following a return, and I’m not a big fan of using the variable root in this case since it’s only root at the top level of the recursion (it may confuse some).

    I’d go with something like (with appropriate comments):

    int validate (node *testNode) {
        // Terminal case, empty sub-tree is valid.
    
        if (testNode == NULL)
            return 1;
    
        // Left node >= current means invalid
    
        if (testNode->lchild != NULL)
            if (testNode->lchild->data >= testNode->data)
                return 0;
    
        // Right node <= current means invalid
    
        if (testNode->rchild != NULL)
            if (testNode->rchild->data <= testNode->data)
                return 0;
    
        // Invalid entire left subtree means invalid.
    
        if (! validate (testNode->lchild))
            return 0;
    
        // Otherwise return state of entire right subtree.
        return validate (testNode->rchild);
    }
    

    You may also want to think about whether you want duplicate values in your tree. If you do, the comparisons should be < and > rather than <= and >=.

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

Sidebar

Related Questions

I have a piece of code here that does not work despite me using
I have no idea what this means. But here is the code that it
I have this html code that i want to edit with jQuery. Here is
I have this code that I have written in Ruby, but when trying to
I have some terribly written ASP.NET code that is just not working right (go
JAVA Code Here is some part of my code that I have written in
Hi I have written this code that with output you can get that .remove()
Here's the c# code that I have: private double get806Fees (Loan loan) { Loan.Fee.Items
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
We have some old C code here that's built with nmake. Is there an

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.