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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:47:32+00:00 2026-06-12T16:47:32+00:00

i propose a recursive implementation for checking whether binary search tree is valid: /*

  • 0

i propose a recursive implementation for checking whether binary search tree is valid:

/*
  Return true if binary tree is a binary search tree
*/
bool BinaryTree::isBinarySearchTree(BinaryTree* tree, int& prev)
{
    if(!isBinarySearchTree(tree->left, tree->data)) // left
         return false;

    if(tree->value > prev) // here
        return false;
    else
        prev = tree->value;

    return isBinaryTree(tree->right); // right
}

i have big doubt on the second check,

    if(tree->value > prev) // here
        return false;

whats your favorite c++ implementation for this problem?

EDIT

how would you extend to find larger BST in given tree?

  • 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-06-12T16:47:34+00:00Added an answer on June 12, 2026 at 4:47 pm

    It’s amazing how many people get this wrong.

    Here’s an example of a tree which the naive solution fails to reject:

                    5
                  /   \
                 /     \
                4       6
               / \     / \
              1   7   1   7
    

    Every invocation of a naive check will succeed, since every parent is between its children. Yet, it is clearly not a well-formed binary search tree.

    Here’s a quick solution:

    bool test(Tree* n,
      int min=std::numeric_limits<int>::min(),
      int max=std::numeric_limits<int>::max()) {
      return !n || ( 
             min < n->data && n->data < max
             && test(n->left, min, n->data)
             && test(n->right, n->data, max));
    }
    

    This isn’t perfect, because it requires that neither INT_MIN nor INT_MAX be present in the tree. Often, BST nodes are ordered by <= instead of <, and making that change would only reserve one value instead of two. Fixing the whole thing is left as an exercise.

    Here’s a demonstration of how the naive test gets it wrong:

    #include <iostream>
    #include <limits>
    #define T new_tree
    
    struct Tree{
      Tree* left;
      int data;
      Tree* right;
    };
    
    Tree* T(int v) { return new Tree{0, v, 0}; }
    Tree* T(Tree* l, int v, Tree* r) { return new Tree{l, v, r}; }
    
    bool naive_test(Tree* n) {
      return n == 0 || ((n->left == 0 || n->data > n->left->data)
          && (n->right == 0 || n->data < n->right->data)
          && naive_test(n->left) && naive_test(n->right));
    }
    
    bool test(Tree* n,
      int min=std::numeric_limits<int>::min(),
      int max=std::numeric_limits<int>::max()) {
      return !n || ( 
             min < n->data && n->data < max
             && test(n->left, min, n->data)
             && test(n->right, n->data, max));
    }
    
    const char* goodbad(bool b) { return b ? "good" : "bad"; }
    
    int main(int argc, char**argv) {
      auto t = T( T( T(1),4,T(7)), 5, T(T(1),6,T(7)));
      std::cerr << "Naive test says " << goodbad(naive_test(t))
                << "; Test says " << goodbad(test(t)) << std::endl;
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The behavior I propose: A user loads up my search page, www.site.com/search, types their
Could someone propose better and/or more elegant implementation of this: let each xs =
For example, propose map<int,void*>hold where void* is always storing pointers from classA is it
Herb Sutter propose a simple implementation of make_unique() there: http://herbsutter.com/gotw/_102/ Here it is: template<typename
Propose to consider the following problem. Suppose we have some composite object. Are there
Abstract: Can you propose a mathematical-ish algorithm over a plane of pixels that will
Today I had a nice opportunity from my manager to propose new technologies to
We need to design a secure web application. I would like to propose a
I like Scala's propose of operator precedence but in some rare cases, unmodified rules
We are doing a feasibility on what we're going to propose as a solution

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.