Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点

BST的Search/Insert/Remove工程实践:从教科书到生产环境 二叉搜索树(BST)是经典动态查找结构,其核心操作Search、Insert、Remove表面遵循‘左小右大’规则,实则蕴含递归路径控制、结构性约束维持与指针安全重连等底层原理。在嵌入式、金融风控、日志索引等工程场景中,Search需返回父节点以支撑高效Insert/Remove;Insert必须应对重复值策略与有序数据退化风险;Remove则依赖后继节点替换机制保障BST性质不被破坏。技术价值在于以O(log n)平均复杂度实现动态有序管理,广泛应用于配置中心、实时规则引擎、时间序列索引等低延迟系统。本文 阅读详情

BST中找中序遍历的后继节点

两点注意:

1. 这个BST是否有parent指针:如果有,则直接用父指针往上找。如果没有,则从root开始往下找。

2. 要查找的点是否有右孩子:如果有,简单,直接找右子树的最小节点。如果没有,则找到比该节点大且相差最小的父节点

时间复杂度都是O(h), h是树高


In Binary Tree, Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inoorder traversal.
In Binary Search Tree, Inorder Successor of an input node can also be defined as the node with the smallest key greater than the key of input node. So, it is sometimes important to find next node in sorted order.

In the above diagram, inorder successor of 8 is 10, inorder successor of 10 is 12 and inorder successor of 14 is 20.

Method 1 (Uses Parent Pointer) 
In this method, we assume that every node has parent pointer.

The Algorithm is divided into two cases on the basis of right subtree of the input node being empty or not.

Input: node, root // node is the node whose Inorder successor is needed.
output: succ // succ is Inorder successor of node.

1) If right subtree of node is not NULL, then succ lies in right subtree. Do following.
Go to right subtree and return the node with minimum key value in right subtree.
2) If right sbtree of node is NULL, then succ is one of the ancestors. Do following.
Travel up using the parent pointer until you see a node which is left child of it’s parent. The parent of such a node is the succ.


Method 2 (Search from root) 
Parent pointer is NOT needed in this algorithm. The Algorithm is divided into two cases on the basis of right subtree of the input node being empty or not.

Input: node, root // node is the node whose Inorder successor is needed.
output: succ // succ is Inorder successor of node.

1) If right subtree of node is not NULL, then succ lies in right subtree. Do following.
Go to right subtree and return the node with minimum key value in right subtree.
2) If right sbtree of node is NULL, then start from root and us search like technique. Do following.
Travel down the tree, if a node’s data is greater than root’s data then go right side, otherwise go to left side.

Time Complexity: O(h) where h is height of tree.



package BinaryTreeSummary;

public class BSTInorderSuccessor {

	public static void main(String[] args) {
		Node root = null;
		root = insert(root, 20);
		root = insert(root, 8);
		root = insert(root, 22);
		root = insert(root, 4);
		root = insert(root, 12);
		root = insert(root, 10);
		root = insert(root, 14);

		Node temp = root.left.right.right;
		Node succ = inorderSuccessor(root, temp);
		succ = inorderSuccessor2(root, temp);
		if (succ != null) {
			System.out.println(temp.data + "'s successor is " + succ.data);
		} else {
			System.out.println("error");
		}
	}

	// 1)需要parent指针的做法
	// 找inorder successor 分右孩子是否存在的两种情况考虑
	// O(h) h: height of tree
	public static Node inorderSuccessor(Node root, Node node) {
		if (node.right != null) {		// 有右孩子,直接找右子树的最小节点
			return minValue(node.right);
		}

		// 否则利用父指针不断向上找,直到父节点的值大于当前节点的值
		// 或者该节点成为父节点的右孩子
		Node parent = node.parent;
//		while (parent != null && node == parent.right) {
		while (parent != null && node.data > parent.data) {
			node = parent;
			parent = parent.parent;
		}
		return parent;
	}
	
	
	// 2)不需要parent指针的做法
	// 过程其实就是个从root查找node节点的过程,同时保存旧的比node大的root节点,作为succ
	// O(h)
	public static Node inorderSuccessor2(Node root, Node node) {
		if (node.right != null) {		// 有右孩子,直接找右子树的最小节点
			return minValue(node.right);
		}
		
		Node succ = null;
		while(root != null) {
			if(root.data > node.data) {	// 继续找更小的
				succ = root;		// 后继节点必然比node要大,所以只能在这里保存
				root = root.left;
			}
			else if(root.data < node.data){		// 继续找更大的
				root = root.right;
			}
			else{		// root节点和node节点重复,停止
				break;
			}
		}
		return succ;
	}
	
	

	/*
	 * Given a non-empty binary search tree, return the minimum data value found
	 * in that tree. Note that the entire tree does not need to be searched.
	 */
	public static Node minValue(Node node) {
		Node cur = node;

		// 最小节点必定在最左下角
		while (cur.left != null) {
			cur = cur.left;
		}
		return cur;
	}

	/*
	 * Give a binary search tree and a number, inserts a new node with the given
	 * number in the correct place in the tree. Returns the new root pointer
	 * which the caller should then use (the standard trick to avoid using
	 * reference parameters).
	 * 
	 * 返回插入后节点的引用
	 */
	public static Node insert(Node node, int data) {
		if (node == null) {
			return new Node(data);
		} else { 			// node 存在
			Node temp;

			if (data <= node.data) {
				temp = insert(node.left, data);
				node.left = temp;
				temp.parent = node;
			} else {
				temp = insert(node.right, data);
				node.right = temp;
				temp.parent = node;
			}
			return node;
		}
	}

	static class Node {
		int data;
		Node left;
		Node right;
		Node parent;

		public Node(int data) {
			this.data = data;
		}
	}

}




http://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/



王珊数据库系统概论第5版视频教材 目录 隐藏 说明:本课程共包括1种电子书、84个高清视频(共68课时)、1种纸质书(赠品)。 网授课程 王珊《数据库系统概论》(第5版)网授精讲班【共68课时】 序号 名称 课时 1 第1章 绪 论(1) 00:32:25 2 第1章 绪 论(2) 00:17:03 3 第1章 绪 论(3) 00:50:19 4 第1章 绪 论(4) 00:20:37 5 第1章 绪 论(5) 00:24:47 6 第2章 关系数据库(1) 00:47:23 7 第2章 关系数据库(2) 00:14:48 8 第2章 关系 阅读详情

相关推荐

vivado下ECO操作-不重新综合实现修改属性并生成.bit

ECO即engineer change order,即工程变更指令,可以对设计做局部小改动并只对改动部分重新布局布线并生成.bit。在一个工程动辄编译几个小时情况下,可以在调试中节省大量时间。比如我们在调试中,需要改变时钟占空比或者相位,需要改变寄存器初值等等,此时用ECO暂时改动,待验证后在重新编译无疑是最有效率的方法。 实现ECO通常有两种方式,一是通过vivado界面完成,另一种方法就是tcl脚本。所有的ECO操作都可以通过tcl操作完成,有的操作用vivado界面无法完成,只能通过tcl。以下主要

4656

Inorder Successor in BST

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 在树中寻找一个节点在中序遍历下的后续节点,此类问题是带条件的遍历,设定某个条件,在遍历过程中符合给定条件时,将节点存入结果,最终返回结果即可。如采用递归遍历,结果可设为一个全局变量ans,如需辅...

m0_37587974的博客 297

半导体AMHS系统振动控制技术演进:从0.5G到超低振动的工艺革命

在半导体制造进入埃米级时代后,AMHS的振动控制能力将成为区分厂商技术等级的标尺。国内企业通过“弹性机械设计-混合主动控制-智能调度”的三重创新,正逐步突破0.2G阈值,支撑12英寸晶圆厂向3nm/2nm制程升级。未来,振动抑制不再仅是物流系统的性能参数,而是与光刻、刻蚀等核心工艺深度耦合的基础能力,这将重塑半导体设备产业的竞争格局。

m0_60674045的博客 1583

Inorder Successor in BST II

Given anodein a binary search tree, findthe in-order successor of that node in the BST. If that node has no in-order successor, returnnull. The successor of anodeis the node with the smallest key greater thannode.val. You will have direct access ...

leetcode 解题思路 446

ARINC429协议分析

如下表所示,ARINC429在发送一帧过程中,首先发送以START开始,以END结束,共发送32bits数据,其中0~7bits为Lable数据,9~8bits为SDI数据,10~28bits为数据,29~30bits为SSM,31bit为校验位End30292827262524校验位SSM2322212019181716DATA15141312111098SDI7654321Start。

5221

【DRAM存储器三十五】LPDDR4介绍--主要技术特性之VREF training,为什么要做VREF training?

详述DDR4、LPDDR4需要做vref training的原因和训练过程

highman110的博客 2914

二叉查找树 - 删除节点 详解(Java实现)

在浏览 二叉查找树(三)之 Java的实现 时,发现所有操作都很容易理解,只有删除看的很糊涂。原文作者在方法上也没有任何注释,因此理解起来很不容易。因此本文是在这篇的基础上,对删除操作进行详细的讲解,所以如果你对二叉查找树还不了解,先看前面这篇博客,如果你能直接看懂那篇博客中的删除方法,也不必再看本文。 虽然我自己也完全从头实现了一遍二叉查找树,但是为了和前面这篇博客衔接的更好,这里就直接拿这篇...

偶尔记一下 - mybatis.io 3万+

代谢组数据分析(二十六):LC-MS/MS代谢组学和脂质组学数据的分析流程

本教程概述了LC-MS/MS在代谢组学和脂质组学数据分析中的应用流程。首先介绍了样本的采集、提取和LC-MS/MS分析,随后详细阐述了质谱数据的预处理、峰值识别、特征注释,以及数据标准化和统计分析的重要性。特别强调了使用XCMS和CAMERA进行数据对齐和加合物识别,以及利用MultiABLER和LipidFinder进行数据过滤、插补和标准化处理。此外,还介绍了如何应用EigenMS算法进行数据标准化,以及如何使用Limma包进行差异分析。整个流程旨在提高数据质量,确保分析结果的准确性和可靠性,为后续的生

专注生信领域 1950

深入理解二叉搜索树(BST)

参考:https://blog.youkuaiyun.com/u013405574/article/details/51058133 https://blog.youkuaiyun.com/zyj8170/article/details/7045226 二叉搜索树的定义 二叉搜索树,也称有序二叉树,排序二叉树,是指一棵空树或者具有下列性质的二叉树: 1. 若任意节点的左子树不空,则左子树上所有结点...

yongwan5637的博客 404

【数据分析八步法】将模糊需求转化为可验证的分析问题

国内访问Huggingface官方源常遇网络问题,可通过切换国内镜像提升下载速度。三种配置方式:1)设置HF_ENDPOINT环境变量;2)在from_pretrained中直接指定镜像参数;3)修改本地Huggingface配置文件。常用镜像包括hf-mirror.com、阿里云ModelScope和清华镜像源。注意镜像同步延迟、私有模型限制,建议配合huggingface-cli工具使用,必要时取消代理设置。合理配置镜像源可显著改善Huggingface生态使用体验。

Mr数据杨 857

基于RAG(Retrieval Augmented Generation)架构的简单问答系统的Python实现示例

以下是一个基于RAG(Retrieval Augmented Generation)架构的简单问答系统的Python实现示例。这个系统使用langchain库进行文档处理、检索,以及与大语言模型交互。

max500600的博客 741

Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup

原文: Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent. 译文: 给定二叉查找树的一个结点, 写一个算法查找它的“下一个”结点(

2284

YOLOv11 模型轻量化全流程:从剪枝到量化,打造移动端“极速检测利器”

将浮点模型量化为INT8模型,减少内存占用并加速推理。# 加载剪枝后的模型# 转换为TorchScript# 静态量化# 保存量化模型量化后模型推理速度可再提升20%,而mAP损失小于1%。

m0_64661770的博客 464

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 node in the BST. If the given node has no in-order successor in the tree, returnnull. 分析:

shiralwz的专栏 366

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode 510. Inorder Successor in BST II - 二叉搜索树(Binary Search Tree)系列题14

​ 题目给定一个二叉搜索树的某一个节点node,要求找出节点node的中序遍历的后继Successor,所谓后继就是树所有值大于node节点值的节点中值最小的那个节点。如果后继不存在就返回空节点。这题跟285. Inorder Successor in BST不同是,没有给定树的根节点,但是节点多了一个指针指向父节点。刷过285. Inorder Successor in BST中的第二种解法就会发现这题其实更简单。

hgq522的博客 709

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[LintCode] 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 node in the BST. If the given node has no in-order successor in the tree, returnnull. ...

188

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[Lintcode]Inorder Successor in Binary Search Tree(DFS)

题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p->val的最小数,否则就遍历p的右子树,找到最左边的节点即可 代码 class Solution { public: /* * @param root: The root...

anquan1533的博客 161

LeetCode 285. Inorder Successor in BST - 二叉搜索树(Binary Search Tree)系列题8

题目给定一个二叉搜索树的根节点root和树中的某一个节点p,要求出节点p的中序遍历的后继Successor,所谓后继就是树所有值大于p节点值的节点中值最小的那个节点。如果后继不存在就返回空节点。

hgq522的博客 433

LeetCode 285. Inorder Successor in BST

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.Note: If the given node has no in-order successor in the tree, return null.思路: 1. 如下图,如果要找4的下一个inorder

xinqrs01的博客 338

二叉树查找树中序后继 · Inorder Successor in Binary Search Tree

[抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的。 [一句话思路]: 比root大时直接扔右边递归,比root小时 考虑是左边递归还是就是root [输入量]:空:正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画...

weixin_30929295的博客 503

LintCode : Inorder Successor in Binary Search Tree

Very interesting problem! http://www.lintcode.com/zh-cn/problem/inorder-successor-in-binary-search-tree/ Description: Given a binary search tree (See Definition) and a node in it, find the in-order...

weixin_30622107的博客 118

lintcode448- Inorder Successor in Binary Search Tree- medium

Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST. If the given node has no in-order successor in the tree, returnnull. Notice I...

weixin_30485799的博客 129
上一篇: LCS 最长公共子序列
下一篇: 算是继往开来
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值