[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 ...
随机推荐
- PHP 服务端 和 APP 客户端 实现 RSA+AES 双向加密解密
目的:服务端和移动端双向加密解密 共有七个文件 其中包括三个类文件 lib_aes.php aes对称加密解密类 server_rsa_crypt.php 服务端RSA公钥私钥非对称加密解密类 cli ...
- SQL Server 2012 Always on Availability Groups安装
http://blog.csdn.net/kevinsqlserver/article/details/7886455
- 看opengl 写代码(4) 画一个圆
opengl 编程指南 P30 以下代码 是 用 直线 连起来 画一个圆. // circle.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...
- Revit Family API 创建参考平面
使用API来编辑族时,使用doc.FamilyCreate.NewReferencePlane();创建参考平面. ) { ]; } // canno ...
- LESS碎语
推荐在Brackets安装"LESS AutoCompile"插件,当保存less文件会自动生成或保存相应的css文件. 变量 以@开头声明变量,并且对变量进行分类,比如颜色变量. ...
- AutoMapper在MVC中的运用05-映射中的忽略、处理null、多种映射转换
本篇AutoMapper使用场景: ※ 动态实现接口方法或属性 ※ 目标中的属性如果比源多,可以忽略多出的属性 ※ 目标有virtual属性,可忽略 ※ 目标属性值为null的解决办法 ※ int转s ...
- 【k8s】搭建步骤
搭建步骤 基础概念:https://www.cnblogs.com/sxdcgaq8080/p/10640879.html ====================================== ...
- Kali Linux 与 BackTrack Linux
(一)BackTrack BackTrack是基于Ubuntu的自启动运行光盘,它包含了一套安全及计算机取证工具.它其实是依靠融合Auditor Security Linux和WHAX(先前的Who ...
- 安装veloeclipse插件报错解决方案
步骤: 1.把Eclipse安装目录下的artifacts.xml打开,搜索veloeclipse,把它相关的项删除: 2.Help 3. Install New Software 4.Work Wi ...
- Keras教程
In this step-by-step Keras tutorial, you’ll learn how to build a convolutional neural network in Pyt ...