2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 587d8257367417b2b2512c7b
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Add a New Element to a Binary Search Tree
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Now that we have an idea of the basics lets write a more complex method.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this challenge, we will create a method to add new values to our binary search tree. The method should be called < code > add< / code >  and it should accept an integer value to add to the tree. Take care to maintain the invariant of a binary search tree: the value in each left child should be less than or equal to the parent value, and the value in each right child should be greater than or equal to the parent value. Here, let's make it so our tree cannot hold duplicate values. If we try to add a value that already exists, the method should return < code > null< / code > . Otherwise, if the addition is successful, < code > undefined< / code >  should be returned.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Hint: trees are naturally recursive data structures!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: The < code > BinarySearchTree</ code >  data structure exists.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: The binary search tree has a method called < code > add</ code > .
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.add == 'function')})());
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: The add method adds elements according to the binary search tree rules.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); const expectedResult = [ 1, 4, 7, 8, 34, 45, 73, 87 ]; const result = test.inOrder(); return (expectedResult.toString() === result.toString()); })());
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Adding an element that already exists returns < code > null</ code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); return test.add(4) == null; })());
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								function Node(value) {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  this.value = value;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.left = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.right = null;
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function BinarySearchTree() {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  this.root = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code above this line
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### After Test
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-teardown' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								BinarySearchTree.prototype = {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  isBinarySearchTree() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (this.root == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      var check = true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      function checkTree(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (node.left != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          var left = node.left;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (left.value > node.value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            check = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            checkTree(left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (node.right != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          var right = node.right;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (right.value <  node.value )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            check = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            checkTree(right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      checkTree(this.root);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return check;
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								BinarySearchTree.prototype = {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  inOrder() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!this.root) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var result = new Array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    function traverseInOrder(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      node.left & &  traverseInOrder(node.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      result.push(node.value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      node.right & &  traverseInOrder(node.right);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    traverseInOrder(this.root);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function Node(value) {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  this.value = value;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.left = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.right = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function BinarySearchTree() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.root = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.add = function(element) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    let current = this.root;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!current) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.root = new Node(element);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      const searchTree = function(current) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (current.value > element) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (current.left) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            //si existe
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return searchTree(current.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            current.left = new Node(element);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else if (current.value <  element )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (current.right) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return searchTree(current.right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            current.right = new Node(element);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return searchTree(current);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >