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

116
Visualizações
Is is possible to implement a depth first search on a binary tree without using recursion?

I wrote the small class below for playing with a binary tree. Is it possible to do depth first search with out recursion?

It looks like you might need a queue to remember where you are in the traversal, but I'm not sure.

Is there a way to do it with out adding another data structure?

class Node {
  constructor(data) {
    this.left = null;
    this.right = null;
    this.data = data;
  }
}

class BinaryTree {

  constructor(data) {
    this.root = new Node(data);
    this.makeTree();
  }

  makeTree(){
    this.insert(15);
    this.insert(5);
    this.insert(7);
    this.insert(3);
    this.insert(20);
    this.insert(13);
  }

  insert(data) {
    let iter = this.root;
    let node = new Node(data);
    while(iter) {
      if(iter.left && (node.data < iter.data)){
        iter = iter.left;
      }
      if(!iter.left && (node.data < iter.data)){
        iter.left = node;
        return;
      }
      if(iter.right && (node.data >= iter.data)){
        iter = iter.right;
      }
      if(!iter.right && (node.data >= iter.data)){
        iter.right = node;
        return;
      }
    }
  }

  df() {
    function dfInner (node) {
      if(node === null) {
        return node;
      }

      // pre order
      dfInner(node.left);

      // in order
      dfInner(node.right);

      // post order

    }
    dfInner(this.root);
  }

}

const tree = new BinaryTree(10);
tree.df();
about 4 years ago · Juan Pablo Isaza
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