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();
tree.root = root;
const n1 = tree.addLeft(, root);
const n2 = tree.addRight(, root);
const n3 = tree.addLeft(, n1);
const n4 = tree.addRight(, n1);
tree.addLeft(, n3);
tree.addRight(, n4);
const n5 = tree.addLeft(, n2);
tree.addRight(, n5);
const n6 = tree.addRight(, n2);
const n7 = tree.addRight(, n6);
tree.addLeft(, 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, ); console.log(JSON.stringify(tree.root.left, null, ));

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

  1. Lintcode: Remove Node in Binary Search Tree

    iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...

  2. Remove Node in Binary Search Tree 解答

    从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...

  3. 【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 v ...

  4. [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,1 ...

  5. 数据结构基础---Binary Search Tree

    /// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and sea ...

  6. [Algorithm] Check if a binary tree is binary search tree or not

    What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...

  7. Lintcode: 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 t ...

  8. LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder

    1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...

  9. 85. Insert Node in a Binary Search Tree【easy】

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

随机推荐

  1. SGU 101 Domino (输出欧拉路径)

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...

  2. SourceTree的简单使用

    原文网址:http://blog.csdn.net/u011439289/article/details/42126507 今天开始参与公司项目的代码编写,公司内部采用的是gitlib,所以用到了So ...

  3. 虫趣:BAD POOL CALLER (par1: 0x20)

    [作者:张佩] [原文:http://www.yiiyee.cn/Blog/0x19-1/] 内核在管理内存的时候,为了提高内存使用效率,对于小片内存的申请(小于一个PAGE大小),都是通过内存池来操 ...

  4. java 输入一个字符串,打印出该字符串中字符的所有排列

    import java.util.Scanner; public class Demo001 { public static void main(String[] args) { String str ...

  5. Revit API选择三维视图上一点

    start [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class cmdPickP ...

  6. WinForm多线程实现HTTP网络检测工具

    一.背景描述与课程介绍 明人不说暗话,跟着阿笨一起玩WinForm.本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高 ...

  7. MyEclipse10安装Log4E插件

    一. Log4E插件下载 下载地址:http://log4e.jayefem.de/content/view/3/2/ 二.安装Log4E插件 将下载下来的压缩包解压缩,如下图所示: 解压缩生成的[d ...

  8. WCF Routing服务,负载均衡

    WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...

  9. 在简历中使用STAR法则

    一.什么是STAR法则? The STAR (Situation, Task, Action, Result) format is a job interview technique used by ...

  10. 整理:iOS 短信与电话事件的获取

    整理:iOS 短信与电话事件的获取   background information: Core Telephony iOS 4.0 的官方 API 裡頭,多了一個叫做 Core Telephony  ...