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

153
Visualizações
Having problems finding the height of my first Binary Search Tree in JS

Here is my constructor function as well as my 'add' method...

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);
    }
  };

Here is the 'getNodeHeight' method I am trying to make...

BinarySearchTree.prototype.getNodeHeight = function(node) {
    if (node.value === null) {
      return -1;
    }
    return Math.max(this.getNodeHeight(node.left), this.getNodeHeight(node.right)) + 1;
  }

And here are the test cases I am running...

binarySearchTree = new BinarySearchTree(5);
binarySearchTree.left = new BinarySearchTree(3);
binarySearchTree.left.left = new BinarySearchTree(1);
binarySearchTree.getNodeHeight(this);

Every time I log the last one to the console I get "Cannot read properties of undefined (reading 'value')". I believe I may be using the 'this' keyword incorrectly... but I've tried messing with it and can't figure it out!

Any tips, tricks, or help would be greatly appreciated... Thank you for your time!

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

0

There are 2 issues, one that was pointed out by @Austin regarding the check on whether the input value is null.

The other issue is that there is no default value for getNodeHeight. For this I'm assuming that you're expecting the default behavior to find the height of the whole tree. Passing in this to getNodeHeight is passing in the context where you are creating the BinarySearchTree - not the instance.

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.getNodeHeight = function(node = this) {
  if (node === null) {
    return -1;
  }
  return Math.max(this.getNodeHeight(node.left), this.getNodeHeight(node.right)) + 1;
}

const binarySearchTree = new BinarySearchTree(5);
binarySearchTree.left = new BinarySearchTree(3);
binarySearchTree.left.left = new BinarySearchTree(1);
console.log(binarySearchTree.getNodeHeight());

about 4 years ago · Santiago Trujillo Relatório

0

The problem is in the first line of your getNodeHeight function when it's called on node.right (which is null). null doesn't have a value property. The same error will occur if you evaluate

null.value

Fix this by changing

if (node.value === null) ...

to

if (!node) ...

On further investigation, the use of the this keyword in binarySearchTree.getNodeHeight(this) is likely returning a reference to your Window object instead of the binarySearchTree. Try calling the method by way of

BinarySearchTree.prototype.getNodeHeight(binarySearchTree)

Good luck!

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