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

134
Visualizações
El nodo del árbol binario de rotación está dejando caer la mitad del árbol

Estoy tratando de construir un árbol AVL/Binario, pero no pude encontrar muchos ejemplos de código, solo teoría. Mi implementación, por alguna razón, tiene la función de rotación AVL perdiendo la mitad del árbol cuando el árbol es de 1 lado.

Aquí está mi código:

 function buildTree(dataSet){ let root = null function rotateRight(node){ return rotate(node,true) } function rotateLeft(node){ return rotate(node,false) } function rotate(node,right){ const inputNodeSide = right ? "left" : "right" const targetNodeSide = right ? "right" : "left" const targetNode = node[ inputNodeSide] const targetNodeChild = targetNode[targetNodeSide] targetNode[targetNodeSide] = node node[ inputNodeSide] = targetNodeChild return targetNode // as this is at the top } function createNode(data){ return { data, left : null, right : null, get balance(){ return workOutHeight(this.left) - workOutHeight(this.right) }, get height(){ return workOutHeight(this) }, } } // END createNode function workOutHeight(node){ if(null === node){ return -1 } return Math.max(workOutHeight(node.left), workOutHeight(node.right))+1 } function avl(node){ const balanced = node.balance if(2 === balanced){ if(0 > node.left.balance){ node.left = rotateLeft(node.left); } return rotateRight(node); } else if(-2 === balanced){ if(0 < node.right.balance){ node.right = rotateRight(node.right); } return rotateLeft(node); } return node } this.add = function(data, perent){ perent = perent || root; if(null === perent){ return root = createNode(data); } else if(data < perent.data){ if(null === perent.left){ return perent.left = createNode(data); } this.add(data,perent.left) avl(perent) } else if(data > perent.data){ if(null === perent.right){ return perent.right = createNode(data); } this.add(data,perent.right) avl(perent) } } // END addData this.tree = function(){ return JSON.parse(JSON.stringify(root)) } if(Array.isArray(dataSet)){ dataSet.forEach(val=>this.add(val)) } } // END buildTree console.log(new buildTree([2,6,9,4,7,0]).tree())

gracias por cualquier ayuda en esto.

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