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
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.
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.
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
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.
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.