Ch4.5: find the (in-order tranversely) successor of a node in BST.

AI预测彩票,使用chatgpt和lstm神经网络(文末附源码) AI预测彩票,使用chatgpt和lstm神经网络(文末有彩蛋) 阅读详情

There are 3 methods:

1. simply in order tranverse and get the ascending array, every next element is the successor of previous element.

2. It is Hawstein's method: There are 2 conditions: 

1st: have right child, go find the left least grand child.

2nd don't. go up to find the closest ancestor.


Here is whole code:

// Ch4.4: 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.
// based on Hawstein's solution

#include 
#include 
#include 

using namespace std;

class BinNode{
public:
    int key;
    BinNode *parent, *lChild, *rChild;
    BinNode(){}
	BinNode(int t): key(t), parent(NULL), lChild(NULL), rChild(NULL){}
};

class BTree{
private:
	BinNode *m_root;
public:
	BTree(): m_root(NULL){}
	BTree(BinNode *t): m_root(t){}

	BinNode* getRoot() const{return m_root;}
	
	BinNode* balanceTree(BinNode* pnode, BinNode *parent, int *arr, int low, int high){
		if(low>high || arr == NULL)
			return NULL;
		int mid = (low+high)>>1;
		pnode = new BinNode(arr[mid]);
		pnode->parent = parent;
		pnode->lChild = balanceTree(pnode->lChild, pnode, arr, low, mid-1);
		pnode->rChild = balanceTree(pnode->rChild, pnode, arr, mid+1, high);

		return pnode;
	}

	void PrintPre(BinNode *pnode)
	{
		if(pnode == NULL)
			return;
 
		cout << pnode->key << " ";
		PrintPre(pnode->lChild);
		PrintPre(pnode->rChild);		
	}

    BinNode* minimal(BinNode* pnode){
        if(pnode==NULL) return NULL;
        while(pnode->lChild){
            pnode = pnode->lChild;
        }
        return pnode;
    }

    BinNode* successor(BinNode* qnode){
        if(qnode==NULL) return NULL;
        if(qnode->rChild) return minimal(qnode->rChild);
        BinNode *r = qnode->parent;
        while(r && r->rChild==qnode){
            qnode = r;
            r = r->parent;
        }
        return r;
    }
};

int main(){
	int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    }; //{1, 2, 5, 7, 10, 13, 15, 21, 22};
	int low = 0, high = sizeof(arr)/sizeof(int)-1;
    cout <<"Balanced tree in array format\n";
	BTree *bt = new BTree();
	BinNode *proot = bt->balanceTree(bt->getRoot(), NULL, arr, low, high);
    bt = new BTree(proot);
    bt->PrintPre(bt->getRoot());
    cout << "pnode is:  "<< proot->key<successor(proot))->key<

In order to verify, need to be careful, it is based on BST. So every simulation should have a BST structure. Random Tree will fail in this method.

华为Ascend_toolkit在x86ubuntu安装失败报错。[WARNING] driver package is not installed. If you install in docker 这个报错发生在交叉编译配置Ascend_toolkit环境时,安装Ascend_toolkit安装包的时候发生。[Y/N]Y。 阅读详情

相关推荐

2D激光slam四种算法建图效果对比

2d激光slam四种算法建图比较

m0_73791170的博客 8452

Cracking the coding interview--Q4.5

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

cfanr 1280

【实战指南】基于YOLOv11的肺结节检测系统开发与优化(LUNA2016数据集)

本文提供了基于YOLOv11模型和LUNA2016数据集开发肺结节检测系统的完整实战指南。详细介绍了从环境搭建、数据预处理、模型训练与优化,到构建Web应用系统的全流程,旨在帮助开发者快速掌握医疗影像AI项目的核心开发技能。

weixin_29031057的博客 141

Successor

线段树 map离散化,按照能力值排序建立更新节点,区间查询(单点更新) for (int i=1;i<=n-1;i++) { scanf("%d%d%d",&fa,&peo[i].loty,&peo[i].abty); peo[i].id=i; son[fa].push_back(i); D[peo[i].loty]=i; ...

qq_36172410的博客 321

【硬件开源实战】STM32G070RBT6开发板从设计到避坑全解析

本文全面解析了STM32G070RBT6开发板从硬件设计到避坑的实战经验。详细介绍了电源设计、时钟电路、PCB布局等关键环节,分享了成本控制、焊接测试、软件开发和外设驱动的实用技巧,帮助开发者高效完成项目开发。

weixin_26897585的博客 247

calibre QRC提取寄生参数

typical将上面的文件QCI_flow里面的文件 在Cp 到当前 ,其实应该是某几个文件,但是我给忘记了 就全cp过来了 ,无影响,不过也希望大佬指正。

2202_75711937的博客 1530

4.6 Successor

For this problem, first we need to know the in-order traverse: left -> root ->right. Then we can analyze the problem by simply draw a BT: Therefore, we can find there are total three different

MoM 372

Qt —— QWebEngineView加载谷歌离线地图(包含离线地图瓦片下载制作、附谷歌地图开发文档)

  关注 微信公众号搜索"Qt_io_"或"Qt开发者中心"了解更多关于Qt、C++开发知识.。 笔者 - jxd

1万+

求一棵二叉树中任意节点的后继节点(后继节点是指在中序遍历中紧随其后的节点)...

   问题定义:         RT,基本上标题已经说得很清楚了。Write an algorithm to find the 'next' node (eg. in-order successor) of a given node in a binary search tree where each node has a link to its parent. 思路:        ...

liuchang198512的博客 622

RTDETR最强结构图 | 包含模型整体结构图 | 全模块展开图 | AiFi模块展开图

RT-DETR 最全结构图

Coding... 9602

CAN总线介绍及硬件设计

1. CAN是什么 CAN 是 Controller Area Network 的缩写(以下称为 CAN),是 ISO*1 国际标准化的串行通信协议。 在当前的汽车产业中,出于对安全性、舒适性、方便性、低公害、低成本的要求,各种各样的电子控制系统 被开发了出来。由于这些系统之间通信所用的数据类型及对可靠性的要求不尽相同,由多条总线构成的情况很 多,线束的数量也随之增加。为适应“减少线束的数量”、“......

sinat_15677011的博客 9994

4.5-求BST的中序后继节点

Write an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its parent. #include #include using namespace std;

oooooops 746

使用YubiKey进行全盘加密、加密启动分区以及利用UEFI安全启动的详细指南

在本教程中,我们将使用YubiKey来增强电脑的安全性,通过加密整个硬盘,确保只有持有正确YubiKey的人才能访问。UEFI安全启动是一种在UEFI固件级别实施的安全标准,确保计算机仅启动签名的启动加载程序。为了符合您的要求,我将以此方式分配内容,确保每部分都有详尽的说明和代码。现在,您已经成功地使用YubiKey设置了全盘加密,并使用UEFI安全启动为系统增加了额外的安全层。注意:为了简洁和清晰,本文中的代码可能不是最优的或最完整的实现。注意:为了简洁和清晰,本文中的代码可能不是最优的或最完整的实现。

qq_38334677的博客 1006

crack the code interview 4.5

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. #include using namespace std; struct T

yfdyyy的专栏 663

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

BST中找中序遍历的后继节点 两点注意: 1. 这个BST是否有parent指针:如果有,则直接用父指针往上找。如果没有,则从root开始往下找。 2. 要查找的点是否有右孩子:如果有,简单,直接找右子树的最小节点。如果没有,则找到比该节点大且相差最小的父节点 时间复杂度都是O(h), h是树高 In Binary Tree, Inorder successor of a n...

系统 运维 390

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Cracking the coding interview(原版)

转自:Hawstein的Cracking the coding interview--问题与解答

holybin的专栏 5852

老版QQ原生表情gif动态

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

九联UNT400G刷机救砖与精简提速全攻略:从短接线刷到ADB优化

电视盒子用久之后卡顿、广告多,常常不是硬件不行,而是预装应用和后台服务拖累了系统。针对运营商定制盒子,刷机是重新获得流畅体验的核心手段,而理解晶晨S905L2芯片的引导机制与线刷原理,是安全救砖的前提。通过USB Burning Tool连接电脑短接进入烧录模式,可以彻底擦除原厂系统;刷入匹配固件后,再借助ADB卸载无用的预装应用、限制后台进程,能进一步释放内存提升响应速度。九联UNT400G这类盒子,只要选对方案、按步骤操作,就能从卡顿的运营商固件中解脱出来,实现从救砖到精简提速的完整改造。

weixin_33937778的博客 362

cc150做题计划

计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。 2月底面试前完成200题C++。 这里记录一下ctci学习的参考资料。 1. hawstein的blog 2. https://github.com/ChengTian/CtCI 3. absolute C++ 5th 4. Primer C++ 5. learncpp:http

纯黑老白的专栏 2909

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes: it means: (from leetcode blog: 点击打开链接) _______3______ / \

纯黑老白的专栏 1330

Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)

这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。 题目在这:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.

纯黑老白的专栏 1295

Ch2-2: return the nth to the last node data of a singly linked list

In this problem, I need to find the nth to the last node data of a singly linked list.  In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an

纯黑老白的专栏 1290

1/22/2014: 纪念第一个C++ programmer电面--F家

经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。 Anyway,谢谢郭志给的refer,不然连电面的机会都没有。 开头是C++基础题: 1. Fib的2种写法:recursive和DP 2. Struct和Class的区别;在C

纯黑老白的专栏 1274

Ch1-7: clear the row i and column j iff a[i][j]==0

in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE. Then the send loop make the TURE row/col

纯黑老白的专栏 1207

复习一下dfs和bfs 用java

dfs 和 bfs都是用来找从source 到 target的path的算法。 dfs用的是recursion,bfs则用queue

纯黑老白的专栏 1191

Ch3-1: use a single array to implement three stacks.

Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici

纯黑老白的专栏 1097

ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one

// Ch4.7: You have two very large binary trees: T1, with millions of nodes, and T2, // with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 #include #include #includ

纯黑老白的专栏 1030
上一篇: Ch4.4: array to Balanced tree and print each level
下一篇: ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值