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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:28:56+00:00 2026-05-19T01:28:56+00:00

I have written the following code to check if a tree is a Binary

  • 0

I have written the following code to check if a tree is a Binary search tree. Please help me check the code:

Okay! The code is edited now. This simple solution was suggested by someone in the posts below:

IsValidBST(root,-infinity,infinity);

bool IsValidBST(BinaryNode node, int MIN, int MAX) 
{
     if(node == null)
         return true;
     if(node.element > MIN 
         && node.element < MAX
         && IsValidBST(node.left,MIN,node.element)
         && IsValidBST(node.right,node.element,MAX))
         return true;
     else 
         return false;
}
  • 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-19T01:28:57+00:00Added an answer on May 19, 2026 at 1:28 am

    A Method should only do one thing at a time. Also the way you do things are generally Weird.
    I will give you some almost-Java pseudocode. Sorry for that, but I have not touched Java for some Time. I hope it helps. Look at the comments I also did on the Question and I hope you sort it out!

    Call your isBST like that :

    public boolean isBst(BNode node)
    {
        return isBinarySearchTree(node , Integer.MIN_VALUE , Integer.MIN_VALUE);
    }
    

    Internally :

    public boolean isBinarySearchTree(BNode node , int min , int max)
    {
        if(node.data < min || node.data > max)
            return false;
        //Check this node!
        //This algorithm doesn't recurse with null Arguments.
        //When a null is found the method returns true;
        //Look and you will find out.
        /*
         * Checking for Left SubTree
         */
        boolean leftIsBst = false;
        //If the Left Node Exists
        if(node.left != null)
        {
            //and the Left Data are Smaller than the Node Data
            if(node.left.data < node.data)
            {
                //Check if the subtree is Valid as well
                leftIsBst = isBinarySearchTree(node.left , min , node.data);
            }else
            {
                //Else if the Left data are Bigger return false;
                leftIsBst = false;
            }
        }else //if the Left Node Doesn't Exist return true;
        {
            leftIsBst = true;
        }
    
        /*
         * Checking for Right SubTree - Similar Logic
         */
        boolean rightIsBst = false;
        //If the Right Node Exists
        if(node.right != null)
        {
            //and the Right Data are Bigger (or Equal) than the Node Data
            if(node.right.data >= node.data)
            {
                //Check if the subtree is Valid as well
                rightIsBst = isBinarySearchTree(node.right , node.data+1 , max);
            }else
            {
                //Else if the Right data are Smaller return false;
                rightIsBst = false;
            }
        }else //if the Right Node Doesn't Exist return true;
        {
            rightIsBst = true;
        }
    
        //if both are true then this means that subtrees are BST too
        return (leftIsBst && rightIsBst);
    }
    

    Now : If you want to find the Min and Max Values of each Subtree you should use a Container (I used an ArrayList) and store a triplet of Node, Min, Max which represents the root node and the values (obviously).

    eg.

    /*
     * A Class which is used when getting subTrees Values
     */
    class TreeValues
    {
        BNode root; //Which node those values apply for
        int Min;
        int Max;
        TreeValues(BNode _node , _min , _max)
        {
            root = _node;
            Min = _min;
            Max = _max;
        }
    }
    

    And a :

    /*
     * Use this as your container to store Min and Max of the whole
     */
    ArrayList<TreeValues> myValues = new ArrayList<TreeValues>;
    

    Now this is a method which finds the Min and Max values of a given node:

    /*
     * Method Used to get Values for one Subtree
     * Returns a TreeValues Object containing that (sub-)trees values
     */ 
    public TreeValues GetSubTreeValues(BNode node)
    {
        //Keep information on the data of the Subtree's Startnode
        //We gonna need it later
        BNode SubtreeRoot = node;
    
        //The Min value of a BST Tree exists in the leftmost child
        //and the Max in the RightMost child
    
        int MinValue = 0;
    
        //If there is not a Left Child
        if(node.left == null)
        {
            //The Min Value is this node's data
            MinValue = node.data;
        }else
        {
            //Get me the Leftmost Child
            while(node.left != null)
            {
                node = node.left;
            }
            MinValue = node.data;
        }
        //Reset the node to original value
        node = SubtreeRoot; //Edit - fix
        //Similarly for the Right Child.
        if(node.right == null)
        {
            MaxValue = node.data;
        }else
        {
            int MaxValue = 0;
            //Similarly
            while(node.right != null)
            {
                node = node.right;
            }
            MaxValue = node.data;
        }
        //Return the info.
        return new TreeValues(SubtreeRoot , MinValue , MaxValue);   
    }
    

    But this returns Values for one Node only, So we gonna use this to find for the Whole Tree:

    public void GetTreeValues(BNode node)
    {
        //Add this node to the Container with Tree Data 
        myValues.add(GetSubTreeValues(node));
    
        //Get Left Child Values, if it exists ...
        if(node.left != null)
            GetTreeValues(node.left);
        //Similarly.
        if(node.right != null)
            GetTreeValues(node.right);
        //Nothing is returned, we put everything to the myValues container
        return; 
    }
    

    Using this methods, your call should look like

    if(isBinarySearchTree(root))
        GetTreeValues(root);
    //else ... Do Something
    

    This is almost Java. It should work with some modification and fix. Find a good OO book, it will help you. Note, that this solution could be broke down into more methods.

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

Sidebar

Related Questions

I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written the following piece of code if( (!isset($_SESSION['home'])) || (!isset($_SESSION['away'])) ) I
I have written a function having the following signature: def action_handler(request, model): This action_handler
I'm trying to run some tests on some Ajax code we have written, now
I have .Net 2.0 Windows form containing combobxes. I have written following code to
I have written the following code #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem.hpp>
I have written a code in PLSQL. Where in I need to Check if
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
i have written the following code to download file. java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
I have written the following scala code to download a file . The file

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.