[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();
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的更多相关文章
- 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. ...
- Remove Node in Binary Search Tree 解答
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 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 ...
- [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 ...
- 数据结构基础---Binary Search Tree
/// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and sea ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- RX库中的IDisposable对象
IDisposable是.net中的主动资源释放接口,它是在编程过程中经常使用到的一个接口,本文介绍一下微软在Rx.NET中提供的一系列常用的Disposable类,通过它们可以简化我们的程序代码,提 ...
- 京东SSO单点登陆实现分析
京东的sso流程: 初始访问状态: cookies: http请求: 1.在首页点击登陆,跳转至passport.360buy.com,给予验证cookie alc(可以试试在提交登陆信息前删除该 ...
- PHP 依赖注入(DI) 和 控制反转(IoC)
要想理解 PHP 依赖注入 和 控制反转 两个概念,就必须搞清楚如下的两个问题: DI —— Dependency Injection 依赖注入 IoC —— Inversion of Control ...
- 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(4)
chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...
- SMTP协议及POP3协议-邮件发送和接收原理(转)
本文转自https://blog.csdn.net/qq_15646957/article/details/52544099 感谢作者 一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 ...
- UnityShader之光照
.基本概念 1.光源:unity支持的光源有四种 1)平行光Directional Light 2)点光源Point Light 3)聚光灯光源Spot Light 4)区域光光源Area Light ...
- SharePoint Online 设置网站集
前言 本文介绍如何在Office 365中设置SharePoint Online网站集,当我们创建好SharePoint Online站点,开始使用之前,一定会有一些基本的设置,本文就为大家介绍这些基 ...
- Android安装和设置的方法
问题:gradle project sync failed 1.进入C:\Users\自己的用户名\.gradle\wrapper\dists\gradle-1.12-all\2apkk7d25mia ...
- PHP开发微信被动回复消息遇到的大坑
最近开始做CMS后台与微信公众号的整合,在做被动回复消息时,按照官方文档的XML格式回复消息,总是提示"该公众号提供的服务出现故障,请稍后再试".但是通过微信提供的接口调试工具看我 ...
- Java Callable接口与Future接口的两种使用方式
Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...