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

194
Visualizações
Is there a better way to write this if-else block?

I am writing some code for a Binary Search Tree where each node has a left and a right child pointer. In my delete function I have the following code.

if (!current.left && !current.right) {
  if (parent.left === current) {
    parent.left = null;
  } else {
    parent.right = null;
  }
} else {
  if (parent.left === current) {
    parent.left = current.left ? current.left : current.right;
  } else {
    parent.right = current.left ? current.left : current.right;
  }
}

Twice I have some block with the following syntax:

if (someCondition) {
  parent.left = x;
} else {
  parent.right = x;
}

Is there a cleaner way to write this (maybe a one-liner)? I am not sure if there is a ternary operator syntax I can use here because I have parent.left = x in the if block and parent.right = x in the else block. I am not keen on all of these if-else blocks used in this function.

Here is the entire code snippet.

const getInOrderSuccessor = (root, node) => {
  let successorParent = null;
  let successor = null;
  let previous = null;
  let current = root;

  while (current) {
    if (node.value < current.value) {
      successorParent = previous;
      successor = current;
      previous = current;
      current = current.left;
    } else {
      previous = current;
      current = current.right;
    }
  }
  return [successor, successorParent];
};

const deleteNode = (root, value) => {
  let current = root;
  let parent = null;
  while (current) {
    if (value === current.value) break;
    parent = current;
    current = value < current.value ? current.left : current.right;
  }

  // If 2 child, deal with that first
  if (current.left && current.right) {
    const [successor, successorParent] = getInOrderSuccessor(root, current);
    current.value = successor.value;
    current = successor;
    parent = successorParent;
  }

  if (!current.left && !current.right) {
    if (parent.left === current) {
      parent.left = null;
    } else {
      parent.right = null;
    }
  } else {
    if (parent.left === current) {
      parent.left = current.left ? current.left : current.right;
    } else {
      parent.right = current.left ? current.left : current.right;
    }
  }
};
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Instead of using the parent.left = x and parent.right = x you could store the property name in a variable and use parent[direction] = x instead.

For your specific scenario it also helps that the parent.left === current check is made in both the if and else-block. This means we can move it outside the conditional.

const direction = parent.left === current ? "left" : "right";
if (!current.left && !current.right) {
  parent[direction] = null;
} else {
  parent[direction] = current.left ? current.left : current.right;
}

You can further simplify this by reworking your logic.

if (!a && !b) {
  variable = null;
} else {
  variable = a ? a : b;
}

Can be changed into:

if (a) {
  variable = a;
} else if (b) {
  variable = b;
} else {
  variable = null;
}

Which can also be written as:

variable = a || b || null;

Resulting in the following solution:

const direction = parent.left === current ? "left" : "right";
parent[direction] = current.left || current.right || null;
about 4 years ago · Juan Pablo Isaza Relatório

0

Please try this one.

whenever you have something like z = y?y:x you should replace it with || operator like this z = y || x And also you can write common conditions.

if (parent.left === current) {
  parent.left = (!current.left && !current.right) ? null : (current.left || current.right);
} else {
  parent.right = !(!current.left && !current.right) ? null : (current.left || current.right);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Here's an option that satisfies your criteria, though style-wise I like what you've written better than this :-)

function chooseWhichToMakeNull(object, condition) {
  const field = condition ? 'left' : 'right';
  object[field] = null;
}

const obj = { left: 5, right: 10 };
chooseWhichToMakeNull(obj, true);
console.log(obj);

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