--- id: 587d8257367417b2b2512c7e title: Use Depth First Search in a Binary Search Tree challengeType: 1 videoUrl: '' localeTitle: Use a primeira pesquisa de profundidade em uma árvore de pesquisa binária --- ## Description
Sabemos como pesquisar uma árvore de pesquisa binária por um valor específico. Mas e se quisermos apenas explorar a árvore inteira? Ou se nós não temos uma árvore ordenada e precisamos apenas procurar por um valor? Aqui introduziremos alguns métodos de travessia de árvores que podem ser usados ​​para explorar estruturas de dados de árvores. A primeira é a pesquisa em profundidade. Na pesquisa em profundidade, uma determinada subárvore é explorada o mais profundamente possível antes que a pesquisa continue em outra subárvore. Existem três maneiras pelas quais isso pode ser feito: Em ordem: Comece a pesquisa no nó mais à esquerda e termine no nó mais à direita. Pré-encomenda: explore todas as raízes antes das folhas. Pós-ordem: explore todas as folhas antes das raízes. Como você pode imaginar, você pode escolher diferentes métodos de busca, dependendo do tipo de dados que sua árvore está armazenando e do que você está procurando. Para uma árvore de pesquisa binária, uma passagem in-order retorna os nós na ordem classificada. Instruções: Aqui nós iremos criar estes três métodos de busca em nossa árvore de busca binária. A pesquisa em profundidade é uma operação inerentemente recursiva que continua a explorar outras subárvores, desde que os nós filhos estejam presentes. Depois de entender esse conceito básico, você pode simplesmente reorganizar a ordem na qual você explora os nós e subárvores para produzir qualquer uma das três pesquisas acima. Por exemplo, na pesquisa de pós-ordem, gostaríamos de recorrer a um nó de folha antes de começarmos a retornar qualquer um dos nós, enquanto que na pesquisa de pré-ordem, gostaríamos de retornar os nós primeiro e continuar recursando. descendo a árvore. Defina os métodos inorder , preorder e postorder na nossa árvore. Cada um desses métodos deve retornar uma matriz de itens que representam o percurso da árvore. Certifique-se de retornar os valores inteiros em cada nó da matriz, não os próprios nós. Finalmente, retorne null se a árvore estiver vazia.
## Instructions
## Tests
```yml tests: - text: A estrutura de dados BinarySearchTree existe. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() }; return (typeof test == "object")})(), "The BinarySearchTree data structure exists.");' - text: A árvore de pesquisa binária tem um método chamado inorder . testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; return (typeof test.inorder == "function")})(), "The binary search tree has a method called inorder.");' - text: A árvore de pesquisa binária tem um método chamado preorder . testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; return (typeof test.preorder == "function")})(), "The binary search tree has a method called preorder.");' - text: A árvore de pesquisa binária tem um método chamado postorder . testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; return (typeof test.postorder == "function")})(), "The binary search tree has a method called postorder.");' - text: O método inorder retorna uma matriz dos valores do nó que resultam de um percurso inorder. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.inorder !== "function") { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.inorder().join("") == "012345678910"); })(), "The inorder method returns an array of the node values that result from an inorder traversal.");' - text: O método preorder retorna uma matriz dos valores do nó que resultam de uma passagem de pré-ordem. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.preorder !== "function") { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.preorder().join("") == "710325469810"); })(), "The preorder method returns an array of the node values that result from a preorder traversal.");' - text: O método postorder retorna uma matriz dos valores do nó que resultam de uma passagem de pós-ordem. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.postorder !== "function") { return false; }; test.add(7); test.add(1); test.add(9); test.add(0); test.add(3); test.add(8); test.add(10); test.add(2); test.add(5); test.add(4); test.add(6); return (test.postorder().join("") == "024653181097"); })(), "The postorder method returns an array of the node values that result from a postorder traversal.");' - text: O método inorder retorna null para uma árvore vazia. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.inorder !== "function") { return false; }; return (test.inorder() == null); })(), "The inorder method returns null for an empty tree.");' - text: O método de preorder retorna null para uma árvore vazia. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.preorder !== "function") { return false; }; return (test.preorder() == null); })(), "The preorder method returns null for an empty tree.");' - text: O método postorder retorna null para uma árvore vazia. testString: 'assert((function() { var test = false; if (typeof BinarySearchTree !== "undefined") { test = new BinarySearchTree() } else { return false; }; if (typeof test.postorder !== "function") { return false; }; return (test.postorder() == null); })(), "The postorder method returns null for an empty tree.");' ```
## Challenge Seed
```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; // change code below this line // change code above this line } ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```