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

227
Visualizações
AVL Tree implementation: Insert function - Reference get twisted

I got the bug when adding 13 to the tree, the left and right pointer of node 10 have reference back to the root, and create cycle reference.

I think it's because I understand Javascript syntax wrong.

code (open the console)

function rotLeft(node) {
  const parentNodeCopy = copyObj(node);
  const parentRightLeftChild =
    node.right.left !== null ? copyObj(node.right.left) : null;
  parentNodeCopy.right = parentRightLeftChild;

  node = node.right;
  node.left = parentNodeCopy;

  return node;
}

function rotRight(node) {
  const parentNodeCopy = copyObj(node);
  const parentLeftRightChild =
    node.left.right !== null ? copyObj(node.left.right) : null;
  parentNodeCopy.left = parentLeftRightChild;
  node = node.left;
  node.right = parentNodeCopy;

  return node;
}

function rebalance(node) {
  const bFact = threshold(node);

  if (bFact > 1) {
    if (threshold(node.left) < 0) node.left = rotLeft(node.left);
    node = rotRight(node);
  } else if (bFact < -1) {
    if (threshold(node.left) > 0) node.right = rotRight(node.right);
    node = rotLeft(node);
  }

  return node;
}

function insert(node, val) {
  if (node === null) return;

  if (val <= node.val) {
    if (node.left !== null) insert(node.left, val);
    else node.left = new TreeNode(val);
  } else {
    if (node.right !== null) insert(node.right, val);
    else node.right = new TreeNode(val);
  }

  return rebalance(node);
}

Any suggestion?

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

0

The problem is that you don't use the returned node reference from the recursive call of insert. insert may return a different node than the one it got as argument. By not assigning it back to node.left or node.right, the latter will keep a reference to the non-cloned node, giving you an inconsistent tree.

So change this:

if (node.left !== null) insert(node.left, val);

to this:

if (node.left !== null) node.left = insert(node.left, val);

Do the same with the mirrored case.

Other remarks

Unrelated to your question:

  1. It should not be necessary to create clones of nodes. Rotations can be implemented by just mutating the existing nodes.

  2. Retrieving the height dynamically each time you need it will kill the performance of this implementation. It is better to store the height as a property of a node and keep it updated. The best is to store the balance factor. With some nifty logic you can keep the balance factors up to date without having to query the heights of nodes. It is possible with just knowing the balance factor of the children, and which rotation is being performed.

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