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

Time:10-13

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

CodePudding user response:

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

CodePudding user response:

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;

CodePudding user response:

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

  • Related