[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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- HDU 1075 What Are You Talking About (strings)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- delphi udp文件传输
客户端: unit UnitClient; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...
- 用delphi实现完美屏幕截图
可以截取layered窗口(包括透明窗口)的代码: procedure CaptureScreen(AFileName: string);const CAPTUREBLT = $40000000;v ...
- NSNotificationCenter消息注册与撤销
苹果的消息机制是个非常好用的东西,当需要在类的各个实例之间传递消息或者写一些事件驱动的程序时,绝对是个不错的工具.但是使用时一不小心就会造成引用已经被dealloc的对象的错误,引起程序崩溃.于是,在 ...
- 如何获得ios7系统中的蓝色
最简洁的办法,直接使用下列代码: [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0] 最彻底的办法: OS7Colors是U ...
- Win7 ArcGIS 10.5 安装错误 A service pack is required on this operatiing system
前段时间有朋友反馈在Win7专业版下安装ArcGIS 10.5, 无法安装,弹出错误: A service pack is required on this operatiing system,如图: ...
- CSS id 和 class 选择器
如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. id 选择器 id 选择器可以为标有特定 id 的 HTML 元 ...
- android 读取 raw 中的文件。
file.getParentFile().mkdir(); file.createNewFile(); InputStream inputStream = context.getResources() ...
- sharepoint 2013 网站集解锁
前言 最近碰到这样的一个问题,就是SharePoint 站点备份(Backup-SPSite)的时候,速度特别慢,然后网站变成只读状态(备份过程中只读属于正常现象).但是,自己手欠把备份命令的Powe ...
- Java分布式系统高并发解决方案
对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并发问题是绝大部分的程序员头疼的问题, 但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们一起来研 ...