2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5cc0c1b32479e176caf3b422
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Check if Tree is Binary Search Tree
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301624
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: check-if-tree-is-binary-search-tree
							 
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Since you already know what a binary search tree is, this challenge will establish how it is you can tell that a tree is a binary search tree or not.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The main distinction of a binary search tree is that the nodes are ordered in an organized fashion. Nodes have at most 2 child nodes (placed to the right and/or left) based on if the child node's value is greater than or equal to (right) or less than (left) the parent node.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In this challenge, you will create a utility for your tree. Write a JavaScript method `isBinarySearchTree`  which takes a tree as an input and returns a boolean value for whether the tree is a binary search tree or not. Use recursion whenever possible.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Your Binary Search Tree should return true when checked with `isBinarySearchTree()` .
							 
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.push(1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.push(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.push(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.push(2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.push(4);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return isBinarySearchTree(test) == true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								BinarySearchTree.prototype.push = function(val) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  var root = this.root;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (!root) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.root = new Node(val);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  var currentNode = root;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  var newNode = new Node(val);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while (currentNode) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (val <  currentNode.value )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (!currentNode.left) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        currentNode.left = newNode;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        currentNode = currentNode.left;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (!currentNode.right) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        currentNode.right = newNode;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        currentNode = currentNode.right;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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(tree) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2019-06-10 23:35:21 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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(tree) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (tree.root == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let isBST = true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    function checkTree(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (node.left != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const left = node.left;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (left.value > node.value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          isBST = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          checkTree(left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (node.right != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const right = node.right;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (right.value <  node.value )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          isBST = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          checkTree(right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    checkTree(tree.root);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return isBST;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```