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

Is there a better way to write this if-else block?

我正在为二叉搜索树编写一些代码,其中每个节点都有一个 left 和一个 right 子指针。在我的 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;
  }
}

我有两次使用以下语法的块:

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

有没有更简洁的方式来写这个(也许是一行)?我不确定这里是否可以使用三元运算符语法,因为我在 if 块中有 parent.left = x,在 else 块中有 parent.right = x。我不喜欢此函数中使用的所有这些 if-else 块。

这是完整的代码片段。

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

请试试这个。

每当你有像 z = y?y:x 这样的东西时,你应该用这样的 || 运算符替换它 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);
}

这里有一个选项可以满足您的标准,不过从风格上来说,我更喜欢您写的比这个好:-)

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

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

除了使用 parent.left = xparent.right = x,您还可以将 属性 名称存储在变量中并使用 parent[direction] = x

对于您的特定情况,在 if 和 else 块中进行 parent.left === current 检查也很有帮助。这意味着我们可以将它移到条件之外。

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

您可以通过修改您的逻辑来进一步简化它。

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

可以改成:

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

也可以写成:

variable = a || b || null;

导致以下解决方案:

const direction = parent.left === current ? "left" : "right";
parent[direction] = current.left || current.right || null;