[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,16,17,20,25,27
The successor is the one right next to the target:
// target 8 --> succossor is 10
So, given the tree and target node, to find its successor.
Require knowledge how recurise call work, mainly it should reach the leaf node, then print it from bottom to top. For the node which has both right and left side, it visits left first, then itself, then right side.
function findCurrent(root, val) {
let current = root;
let found = null;
while (current !== null) {
if (current.val === val) {
found = current;
break;
} else if (current.val < val) {
current = current.right;
} else {
current = current.left;
}
} return found;
} function findMin(root) {
// min should be on the left side
while (root.left != null) {
root = root.left;
} return root;
} function findSuccsor(root, val) {
const current = findCurrent(root, val);
if (current == null) {
return null;
}
// case 1: Node has right subtree
// Find min on the right part of the node
// the min should be on the left most
if (current.right != null) {
return findMin(current.right);
}
// case 2: Node has no right subtree
// --> A. target node can be on the left side like 8
// --> B. target node can be on the right side like 12
else {
let parent = root;
let found = null;
while (parent !== current) {
// A: if target is smaller than parent, means on the left side
// then we keep going down to find target, its parent should be
// the successor
if (current.val < parent.val) {
found = parent;
parent = parent.left;
}
// B: it target is larger than parent, means on the right side
// then it means the parent node should already been visited
// so we should not set successor in this case
else {
parent = parent.right;
}
} return found;
}
} 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(, tree.root);
const n2 = tree.addRight(, tree.root); const n3 = tree.addLeft(, n1);
tree.addLeft(, n3); const n4 = tree.addRight(, n1);
tree.addLeft(, n4); const n5 = tree.addLeft(, n2);
const n6 = tree.addLeft(, n5);
const n7 = tree.addRight(, n2);
tree.addRight(, n7);
// inorder
// 6,8,10,11,12,15,16,17,20,25,27
// successor is the one next to the target
// target 8 --> succossor is 10
console.log(findSuccsor(tree.root, ));
[Algorithm] Inorder Successor in a binary search tree的更多相关文章
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...
- [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 nod ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Binary Search Tree In-Order Traversal Iterative Solution
Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- IAR EWARM 字体设置
如果只想简单的设置,可进行如下设置 Tools->IDE Options->Editor->Colors and Fonts->Editor Font->Font 但是这 ...
- SSH 证书登录(实例详解)
SSH 证书登录(实例详解) 客户端通过私钥登录 ssh 服务器 CentOS 7 SSH 使用证书登录 使用私钥 ssh 登陆 CentOS
- Win10年度更新开发必备:VS2015 Update 2正式版下载汇总
========================================================================== 微软在03月30日发布了Visual Studio ...
- GIS中的数据库.gdb与.mdb的区别
gdb是文件地理数据库,mdb是个人地理数据库,都是数据库文件类型. 个人地理数据库,是以access数据库为基础的个人将数据库格式mdb,可以存储不超过2G的文件,只适合Windows系统下: 文件 ...
- java多态--算法实现就是多态
算法:是实现集合接口的对象里的方法执行的一些有用的计算,例如:搜索和排序. 这些算法被称为多态,那是因为相同的方法可以在相似的接口上有着不同的实现. 集合接口 集合框架定义了一些接口.本节提供了每个接 ...
- ifeq endif
ifeq ($(PLATFORM_VERSION),4.4)$(info "________________________4.4"); LOCAL_CFLAGS += -DPLU ...
- python测试开发django-19.admin后台自定义显示
前言 django的admin后台默认显示的内容很少,只显示了表的相关信息,查看字段内容需点开详情才能查看,不是很直观. 在admin.py文件里面是可以自定义相关的展示内容的,也可以添加搜索框,快速 ...
- Linux学习10-CentOS搭建nginx负载均衡环境
前言 当自己的web网站访问的人越来越多,一台服务器无法满足现有的业务时,此时会想到多加几台服务器来实现负载均衡. 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,怎样将同一个域名的访问分散 ...
- spring 5.0.1.RELEASE官方任然不支持velocity(平台升级)
官方说明: Dear Spring community, It is my pleasure to announce that Spring Framework 5.0.1 is available ...
- MySQL查询字符串长度最长的记录
select `字段`, length(`字段`) from 表名 where length(`字段`) = ( select max(length(`字段`)) from 表名 )http://s ...