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. 使用p3p跨域设置Cookie

    有些时候不能将url上的参数传来传去,比如与调用某开放平台上的接口,这时候可能需要借助Cookie来进行处理了,但这里可能又涉及到跨域的问题. 如果浏览器开启了对Cookie的支持,按照Cookie ...

  2. stat,fstate,lstat函数

    #include <sys/stat.h> int stat (const char *restrict pathname,struct stat* restrict buf) int f ...

  3. 如何:声明、实例化和使用委托(C# 编程指南)

    委托的声明如下所示: C#   public delegate void Del<T>(T item); public void Notify(int i) { } C#   Del< ...

  4. 基于设备树的controller学习(1)

    作者 彭东林pengdonglin137@163.com 平台 TQ2440Linux-4.10.17 概述 在设备树中我们经常见到诸如"#clock-cells"."# ...

  5. ASP.NET MVC中检测浏览器版本并提示下载更新

    如果网站使用html5.css3.自适应等新特性,可能有些浏览器版本不支持.这时候,需要提醒浏览者更新浏览器的版本到最新. 本篇用到的插件为:http://jreject.turnwheel.com/ ...

  6. could not load inserted library: /usr/lib/libgmalloc.dylib

    本人也刚开始学习iphone开发,最近维护一个程序时,不知道怎么弄的,不能build到真机了,到模拟器没有什么问题,出现的错误是 :---->>>could not load ins ...

  7. 三张图让你高速明确activity与fragment生命周期的异同点

    第一张图:activity的生命周期 第二张图:fragment的生命周期 第三张图:activity与fragment生命周期对照 补充:假设你还是不明确,请翻译一下你不理解的相应单词. ----- ...

  8. ASP.NET MVC:三个被隐藏的扩展性“钻石”(转载)

    原文地址:http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx/. ASP.N ...

  9. python测试开发django-11.模型models详解

    前言 Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库 只需要在settings ...

  10. 便利的初始化view以及设置tag值

    便利的初始化view以及设置tag值 效果 源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 SetRect // // Access ...