[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 ...
随机推荐
- CSDN博客的积分计算方法和博客排名规律
开通博客一段时间了,近期莫名其妙得获得"持之以恒"的勋章,看着日益增长的积分,既兴奋又好奇.本人对CSDN博客积分的计算方法非常疑惑,也不知当中怎么回事,好奇度娘一番,并结合CSD ...
- 《大话设计模式》C#/C++版pdf/源码下载
大话设计模式(带目录完整版)[中文PDF+源代码].zip 下载地址:http://pan.baidu.com/s/1giQP4大话设计模式C++.pdf下载地址:http://pan.baidu.c ...
- C# CSGL
转.修改自ShareIdeas文章C# 基于CSGL opengl OpenGL是一个功能强大的开放图形库(Open Graphics Library).其前身是SGI公司为其图形工作站开发的IRIS ...
- Linux学习11-CentOS如何设置java环境变量
前言 之前用yum安装的java,现在想添加环境变量,yum安装的java路径在哪呢?如何找到安装的路径,把jdk添加到环境变量. 本篇详细讲解linux系统设置java环境变量 找到jdk路径 之前 ...
- Convolutional Neural Networks at Constrained Time Cost(精读)
一.文献名字和作者 Convolutional Neural Networks at Constrained Time Cost,CVPR 2015 二.阅读时间 2015年6月30 ...
- POST 和 PUT 方法区别
Http定义了与 服务器的交互方法,其中除了一般我们用的最多的GET,POST 其实还有PUT和DELETE 根据RFC2616标准(现行的HTTP/1.1)其实还有OPTIONS,GET,H ...
- ios成长之每日一遍(day 1)
Hello world开始. 这里不讨论如何创建项目导入项目.由于趁上班时间打酱油所以也不谈细节, 只谈具体项目的实现与关键流程的解析, 只供本人实际程况使用.不喜请移驾. 首先来谈谈 AppDele ...
- 《HTML5与CSS3基础教程(第8版)》
<HTML5与CSS3基础教程(第8版)> 基本信息 原书名:HTML and CSS:visual quickstart guide 作者: (美)Elizabeth Castro ...
- [Web 前端] CSS篇之3. 如何保持浮层水平垂直居中
原文链接](http://www.cnblogs.com/yaliu/p/5190957.html) 浮层水平垂直居中方法 (一)利用绝对定位与transform <div class=&quo ...
- Caffe的solver参数介绍
版权声明:转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/59109447 1. Parameters solver.p ...