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

198
Visualizações
Intentando trabajar y hacer un método 'balanceFactor' para un BST. ¿No puede averiguar cómo calcular el factor de equilibrio... pasando el argumento incorrecto?

¿Creo que estoy usando 'esto' incorrectamente? No estoy seguro de cómo proceder. Aquí están los métodos que estoy usando.

 function BinarySearchTree(value) { this.value = value; this.right = null; this.left = null; } BinarySearchTree.prototype.add = function(value) { node = this; 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.leftHeight = function(root = this.value) { if (this.value == null) { return 0; } let queue = []; queue.push(this); let leftHeight = 0; while (1 == 1) { let nodeCount = queue.length; if (nodeCount == 0) { return leftHeight - 1; } leftHeight++; while (nodeCount > 0) { let newNode = queue.shift(); if (newNode.left != null) { queue.push(newNode.left); } nodeCount--; } } } BinarySearchTree.prototype.rightHeight = function(root = this.value) { if (this.value == null) { return 0; } let queue = []; queue.push(this); let rightHeight = 0; while (1 == 1) { let nodeCount = queue.length; if (nodeCount == 0) { return rightHeight - 1; } rightHeight++; while (nodeCount > 0) { let newNode = queue.shift(); if (newNode.right != null) { queue.push(newNode.right); } nodeCount--; } } } BinarySearchTree.prototype.balanceFactor = function(this) { console.log(this.leftHeight) return this.leftHeight - this.rightHeight; }

Y aquí está la entrada y las pruebas que estoy ejecutando...

 let binarySearchTree = new BinarySearchTree; binarySearchTree = new BinarySearchTree(5); binarySearchTree.left = new BinarySearchTree(2) binarySearchTree.right = new BinarySearchTree(3); binarySearchTree.right.right = new BinarySearchTree(1); console.log(binarySearchTree.rightHeight()); console.log(binarySearchTree.leftHeight()); console.log(binarySearchTree.balanceFactor())

Los últimos 3 registros de la consola generan 2, 1 y NaN. ¡Entonces mis métodos de altura izquierda y derecha funcionan correctamente! ¿Alguien puede decirme por qué mi método 'balanceFactor' no funciona?

¡Gracias y felices fiestas!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

leftHeight y rightHeight son funciones, por lo que debe llamarlas así

 BinarySearchTree.prototype.balanceFactor = function () { console.log(this.leftHeight()) return this.leftHeight() - this.rightHeight(); }
about 4 years ago · Santiago Trujillo 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