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

94
Visualizações
depthFirstTraversel with binary search tree

I try to get the results of depthFirstTraverselfor with binary search tree. But I get a Blanco output.

So I have this:


class BST {
  constructor(value) {
    this.left = null;
    this.right = null;
    this.value = value;
  }

  insert(value) {
    if (value <= this.value) {
      if (!this.left) this.left = new BST(value);
      else this.left.insert(value);
    } else if (value > this.value) {
      if (!this.right) this.right = new BST(value);
      else {
        this.right.insert(value);
       
      }
    }
  }


  depthFirstTraversel = (iteratorFunc) => {    
    if (this.left) this.left.depthFirstTraversel(iteratorFunc);  
    if (this.right) this.right.depthFirstTraversel(iteratorFunc);
  };
}

function log(value) {
  console.log(value);
}

const bst = new BST(50);
bst.insert(30);
bst.insert(70);
bst.insert(100);
bst.insert(60);
bst.insert(59);
bst.insert(20);
bst.insert(45);
bst.insert(35);
bst.insert(85);
bst.insert(105);
bst.insert(10);




bst.depthFirstTraversel(log);

So what I expect is a ascending order of the numbers: 10 20 30..etc

But I get a Blanco page back in google chrome dev tools

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

0

Your depthFirstTraversel doesn't try to output anything, all it does is traverse. You keep passing the log function as a parameter, for some reason, but you never call it.

Here's a corrected version (I removed passing the log function as parameter, because it can just be called directly.)

class BST {
  constructor(value) {
    this.left = null;
    this.right = null;
    this.value = value;
  }

  insert(value) {
    if (value <= this.value) {
      if (!this.left) this.left = new BST(value);
      else this.left.insert(value);
    } else if (value > this.value) {
      if (!this.right) this.right = new BST(value);
      else {
        this.right.insert(value);
       
      }
    }
  }


  depthFirstTraversel = () => {    
    if (this.left) this.left.depthFirstTraversel();  
    log(this.value);
    if (this.right) this.right.depthFirstTraversel();
  };
}

function log(value) {
  console.log(value);
}

const bst = new BST(50);
bst.insert(30);
bst.insert(70);
bst.insert(100);
bst.insert(60);
bst.insert(59);
bst.insert(20);
bst.insert(45);
bst.insert(35);
bst.insert(85);
bst.insert(105);
bst.insert(10);




bst.depthFirstTraversel(log);

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