Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
Estoy tratando de ejecutar una función de devolución de llamada en una entrada de matriz en un árbol de búsqueda binaria. Me siguen diciendo que mi función de devolución de llamada "no es una función"

Aquí está mi código para crear el árbol de búsqueda binaria, así como un método prototipo para agregar al árbol. También existe el método de pedido anticipado junto con algunas llamadas para ejecutar las funciones para probarlas...

 function BinarySearchTree(value) { this.value = value; this.right = null; this.left = null; } BinarySearchTree.prototype.add = function(value) { if (value < this.value && this.left) { this.left.add(value); } else if (value < this.value) { this.left = new BinarySearchTree(value); } if (value > this.value && this.right) { this.right.add(value); } else if (value > this.value) { this.right = new BinarySearchTree(value); } }; BinarySearchTree.prototype.depthFirstPre = function(callback) { callback(this.value); this.depthFirstPre(this.left); this.depthFirstPre(this.right); }; binarySearchTree = new BinarySearchTree(5); var array = []; var func = function(value){ array.push(value); }; binarySearchTree.add(2); binarySearchTree.add(3); binarySearchTree.add(7); binarySearchTree.add(6); binarySearchTree.depthFirstPre(func); console.log(array) -> *should output [ 5, 2, 3, 7, 6 ]*

Sigo recibiendo "la devolución de llamada no es una función" cuando intento ejecutar la función depthFirstPre y no sé por qué.

¡Gracias por cualquier ayuda, será muy apreciada!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Puede agregar una marca y tomar this.left o this.right para la llamada.

 function BinarySearchTree(value) { this.value = value; this.right = null; this.left = null; } BinarySearchTree.prototype.add = function(value) { if (value < this.value) { if (this.left) this.left.add(value); else this.left = new BinarySearchTree(value); } if (value > this.value) { if (this.right) this.right.add(value); else this.right = new BinarySearchTree(value); } }; BinarySearchTree.prototype.depthFirstPre = function(callback) { callback(this.value); if (this.left) this.left.depthFirstPre(callback); if (this.right) this.right.depthFirstPre(callback); }; BinarySearchTree.prototype.depthFirst = function() { return [ this.value, ...(this.left ? this.left.depthFirst() : []), ...(this.right ? this.right.depthFirst() : []) ]; }; var binarySearchTree = new BinarySearchTree(5), array = [], func = function(value) { array.push(value); }; binarySearchTree.add(2); binarySearchTree.add(3); binarySearchTree.add(7); binarySearchTree.add(6); binarySearchTree.depthFirstPre(func); console.log(...array); // [ 5, 2, 3, 7, 6 ] console.log(...binarySearchTree.depthFirst());

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda