[Algorithm] Delete a node from Binary Search Tree

The solution for the problem can be divided into three cases:

case 1: if the delete node is leaf node, then we can simply remove it

case 2: if the delete node is has single side or substree

case 3: it has two children, then to keep it as a valid binary search tree, we can copy the min value from right subtree to delete node, to convert the problem into case 2

function Node(val) {
  return {
    val,
    left: null,
    right: null
  };
}

function Tree() {
  return {
    root: null,
    addLeft(val, root) {
      const node = Node(val);
      root.left = node;
      return root.left;
    },
    addRight(val, root) {
      const node = Node(val);
      root.right = node;
      return root.right;
    }
  };
}

const tree = new Tree();
const root = Node(12);
tree.root = root;
const n1 = tree.addLeft(5, root);
const n2 = tree.addRight(15, root);
const n3 = tree.addLeft(3, n1);
const n4 = tree.addRight(7, n1);
tree.addLeft(1, n3);
tree.addRight(9, n4);
const n5 = tree.addLeft(13, n2);
tree.addRight(14, n5);
const n6 = tree.addRight(17, n2);
const n7 = tree.addRight(20, n6);
tree.addLeft(18, n7);

/**
 *
            12
          /             5      15
       /  \    /       3    7   13  17
    /      \    \     1         9    14  20
                    /
                   18

        Delete 15
    First copy 17 to 15
            12
          /             5      17
       /  \    /       3    7   13  17
    /      \    \     1         9    14  20
                    /
                   18
     Then remove original 17
            12
          /             5       17
       /  \    /        3    7   13    20
    /      \   \   /
  1         9  14  18

 */

function deleteNode(root, val) {
  // base case, if leaf node, return
  if (root === null) {
    return root;
  } else if (val > root.val) {
    // if delete value is larger than root, search on right side of tree
    root.right = deleteNode(root.right, val);
  } else if (val < root.val) {
    // if delete value is smaller than root, search on left side of tree
    root.left = deleteNode(root.left, val);
  } else {
    // if found the delete value and it is leaf node
    if (root.left === null && root.right === null) {
      // set leaf node to null
      root = null;
    }
    // if found the delete node and its right side has children
    // set root to null and link to its right node
    else if (root.left === null && root.right !== null) {
      let temp = root.right;
      root = null;
      root = temp;
    }
    // if found the delete node and its left side and children
    // set root to null and link to its left node
    else if (root.left !== null && root.right === null) {
      let temp = root.left;
      root = null;
      root = temp;
    }
    // the found node has children on both sides
    // then pick the min value from rgiht side (or max value from left side)
    // copy to current node
    // reset it right side (or left side)
    else {
      const temp = root.right; // get the min on the right side or max on the left side
      root.val = temp.val;
      root.right = deleteNode(root.right, temp.val);
    }

    return root;
  }
}

deleteNode(tree.root, 15);

console.log(JSON.stringify(tree.root.left, null, 2));

原文地址:https://www.cnblogs.com/Answer1215/p/10637315.html

时间: 2024-10-17 00:10:06

[Algorithm] Delete a node from Binary Search Tree的相关文章

[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still

【Lintcode】087.Remove Node in Binary Search Tree

题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after rem

Remove Node in Binary Search Tree

Question:  Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree a

Remove Node in Binary Search Tree 解答

从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树(因为第一个比删除节点大的节点可能出现在右子树,不会出现在左子树). 这里用Target表示删除节点,Parent表示删除节点的母节点. 1. 没有右子树 Parent.left / Parent.right = Target.left 2. 有右子树 1) 找到第一个比删除节点大的节点Node 2

[Algorithm] Inorder Successor in a binary search tree

For the given tree, in order traverse is: visit left side root visit right side // 6,8,10,11,12,15,16,17,20,25,27 The successor is the one right next to the target: // target 8 --> succossor is 10 So, given the tree and target node, to find its succe

LF.53.Delete In Binary Search Tree

Delete the target key K in the given binary search tree if the binary search tree contains K. Return the root of the binary search tree. Find your own way to delete the node from the binary search tree, after deletion the binary search tree's propert

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

[Lintcode] Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow, after Insert node 6, the tree

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod