简体   繁体   English

有没有更好的方法来编写这个 if-else 块?

[英]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.我写了二叉搜索树,其中每个节点有一些代码, leftright子指针。 In my delete function I have the following code.在我的delete功能中,我有以下代码。

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.我不知道如果有一个三元操作的语法,因为我有我可以在这里使用parent.left = xifparent.right = xelse块。 I am not keen on all of these if-else blocks used in this function.我并不热衷于这个函数中使用的所有这些if-else块。

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

Please try this one.请试试这个。

whenever you have something like z = y?y:x you should replace it with ||每当你有类似z = y?y:x你应该用||替换它operator like this z = y || x像这样的运算符z = y || x z = y || x And also you can write common conditions. z = y || x你也可以写常见的条件。

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

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.您可以将属性名称存储在变量中并使用parent[direction] = x代替,而不是使用parent.left = xparent.right = x

For your specific scenario it also helps that the parent.left === current check is made in both the if and else-block.对于您的特定场景,在 if 和 else 块中进行parent.left === current检查也有帮助。 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;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM