Mines (not) better - Check if Tree is Binary Search Tree

I think mine is better than the given ‘hint’. Especially as it can do the extra checks that are commented out and change default bool value to false in function parameters.
just wondering if in the wild the checks would be more vigorous.

  **Your code so far**
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
}
function isBinarySearchTree(node, bool=true) {
// Only change code below this line
node=node.root
if(!node){return false}
  if(node){
//      if('value' in node && 'left' in node && 'right' in node){bool=true}
//      bool = Object.keys(node).length === 3 ? bool : false
    if(node.left){ bool = node.left.value < node.value ? bool : false }
    if(node.right){ bool = node.right.value > node.value ? bool : false }
  }
  if(node.left){isBinarySearchTree(node.left, bool)}
  if(node.right){isBinarySearchTree(node.right, bool)}
  return bool
// Only change code above this line
}
let tree = new BinarySearchTree()
console.log(isBinarySearchTree(tree))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36

Challenge: Data Structures - Check if Tree is Binary Search Tree

Link to the challenge:

solved anyways, no worries

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

This part is not quite correct. An empty tree is a valid binary search tree.

You should declare the variable bool and give ti a more descriptive name.

i made some changes, but why would i declare the boolean seperately? i like to use function parameters for declaration when working with recursion, although i don’t seem to need to here.

var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
}
function isBinarySearchTree(node) {
// Only change code below this line
node=node.root
let isBST = true
if(!node){return isBST}
  if(node){
    if(!'value' in node && !'left' in node && !'right' in node) isBST = false
    isBST = Object.keys(node).length === 3 ? isBST : false
    if(node.left){ isBST = node.left.value < node.value ? isBST : false }
    if(node.right){ isBST = node.right.value > node.value ? isBST : false }
  }
  if(node.left){isBinarySearchTree(node.left)}
  if(node.right){isBinarySearchTree(node.right)}
  return isBST
// Only change code above this line
}
let tree = new BinarySearchTree()
console.log(isBinarySearchTree(tree))

You never declared boolean anywhere, not even as a function argument, so it is automatically created as a global variable. You aren’t using pure recursion but instead modifying a global variable. If this exercise was running in strict mode, that code would error.

function isBinarySearchTree(node, bool=true)
?
isn’t that a declaration in function parameter?

Ah, I missed that.

Some people really love smuggling state around in function parameters for recursion, but I don’t care for it. You already have a return value - just use it. Using the return value will simplify your code

This is still wrong. An empty tree is a valid binary search tree.

And there is no reason to forbid additional properties on a node

but…
if(node){
this accounts for empties

ok, didn’t know.

thanks

Ohh, are you checking that every single node was actually created by the Node function above? Why? I’m pretty paranoid, but that sort of logic would go into construction of the tree, not traversal.

Both Solution 1 and your code are needlessly complex. Solution 2 is more straightforward.

You want it to be easy and efficient to traverse the tree. I’d put node validation checks only in tree creation/node additon so they do not get repeated on every single tree traversal.

no, i was checking every node had those 3 properties, but if they can have others then no need

By ‘created by’ I mean ‘exactly matches the specification given’. You introduced very strong coupling between the internal design of the Node and this function for the Tree. That sort of coupling makes in harder for you to make small changes to your design without touching a bunch of places in the code.

1 Like
function isBinarySearchTree(node) {
// Only change code below this line
  node=node.root
  let isBST = true
  if(!node){return isBST}
  if(node.left){ isBST = node.left.value < node.value ? isBST : false }
  if(node.right){ isBST = node.right.value > node.value ? isBST : false }

  if(node.left){isBinarySearchTree(node.left)}
  if(node.right){isBinarySearchTree(node.right)}

return isBST
// Only change code above this line
}

You aren’t using the recursive call here, so I don’t think this will work in general.

corrected a typo but pls tell me if you meant something else, not sure if i’m missing a point

What happens in your code if a recursive call returns false? How is isBST updated?

its set to true? at the beginnning of function?

That’s internal to the function call (just like a passed in argument value would be too). You need to use the result of the function call somehow.

i thought i was doing that with the function parameter method. not sure what you mean if i declare the variable elsewhere.

The function parameter passes information in, it doesn’t pass information back out.

This isn’t the same as passing in a reference to an array