2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								id: 587d8258367417b2b2512c81
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								title: Exclua um nó com um filho em uma árvore binária de busca
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								challengeType: 1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								forumTopicId: 301638
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								dashedName: delete-a-node-with-one-child-in-a-binary-search-tree
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --description--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Agora que podemos excluir nós de folhas vamos passar para o segundo caso: excluir um nó com um filho. Para este caso, vamos supor uma árvore com os seguintes nós: 1 — 2 — 3, onde 1 é a raiz. Para excluir 2, temos simplesmente de fazer a referência da direita em 1 apontar para 3. De modo mais geral, para excluir um nó com apenas um filho, fazemos com que o pai desse nó referencie o próximo nó da árvore.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --instructions--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Fornecemos parte do código em nosso método `remove`  que realiza as tarefas do último desafio. Encontramos o destino a ser excluído e seu pai e definimos o número de filhos que o nó de destino possui. Vamos adicionar o próximo caso aqui para os nós de destino com apenas um filho. Aqui, teremos que determinar se o filho único é um ramo à esquerda ou à direita na árvore e, então, definir a referência correta no pai para que aponte para este nó. Além disso, vamos levar em conta o caso em que o destino é o nó raiz (o que significa que o nó pai será `null` ). Sinta-se à vontade para substituir todo o código inicial por seu próprio código, contanto que ele passe nos testes.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --hints--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								A estrutura de dados `BinarySearchTree`  deve existir.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return typeof test == 'object';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								A árvore binária de busca deve ter um método chamado `remove` .
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return typeof test.remove == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Tentar remover um elemento que não existe deve retornar `null` .
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return test.remove(100) == null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Se o nó raiz não tem filhos, a exclusão deve definir a raiz como `null` .
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(500);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(500);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return test.inorder() == null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								O método `remove`  deve remover os nós de folha da árvore.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(6);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(12);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(12);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return test.inorder().join('') == '567';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								O método `remove`  deve remover os nós com um filho.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(-1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(16);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(16);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return test.inorder().join('') == '-1';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-02 23:05:44 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Remover a raiz de uma árvore com dois nós deve definir o segundo nó como a raiz.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-15 00:49:18 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(15);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.add(27);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    test.remove(15);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    return test.inorder().join('') == '27';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --seed--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## --after-user-code--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								BinarySearchTree.prototype = Object.assign(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  BinarySearchTree.prototype,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    add: function(value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      var node = this.root;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if (node == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        this.root = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        function searchTree(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          if (value <  node.value )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            if (node.left == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              node.left = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } else if (node.left != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              return searchTree(node.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          } else if (value > node.value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            if (node.right == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              node.right = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } else if (node.right != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								              return searchTree(node.right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return searchTree(node);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    inorder: function() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if (this.root == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        var result = new Array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        function traverseInOrder(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          if (node.left != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            traverseInOrder(node.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          result.push(node.value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          if (node.right != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            traverseInOrder(node.right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        traverseInOrder(this.root);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return result;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## --seed-contents--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  this.remove = function(value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (this.root === null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var target;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var parent = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    // Find the target value and its parent
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    (function findValue(node = this.root) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if (value == node.value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        target = node;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else if (value <  node.value  & &  node . left  ! = =  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        parent = node;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return findValue(node.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else if (value <  node.value  & &  node . left  = ==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else if (value > node.value & &  node.right !== null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        parent = node;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return findValue(node.right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }.bind(this)());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (target === null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    // Count the children of the target to delete
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var children =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      (target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    // Case 1: Target has no children
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if (children === 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if (target == this.root) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        this.root = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        if (parent.left == target) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          parent.left = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          parent.right = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    // Case 2: Target has one child
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    // Only change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# --solutions--
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// solution required
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```