Ch4.3: transform an ordered array into a balanced Tree

OpenEarthMap 与 xBD 数据集合并 整合好的数据:链接: https://pan.baidu.com/s/1uJ_Zg6n-s3u1D49OW52D7Q?pwd=2vwx 提取码: 2vwx完整整合步骤与代码如下。 阅读详情

In this problem, it is like the classical recursion example: draw the ruler as the sign of algorithms 4th textbook.

In order to draw a balanced tree from an array, we need to keep left subtree.height similar or equal to right subtree.height.

First need to find the root--->we can choose the middle number of the array. then recursively go to left construction and right construction.

Here is the main algorithm: 

note: the first parameter seems no use of the head, but it is a good one. By using call-by-reference of the head pointer, we don't need to declare head as  a global variable, and it can be explicitly shown in the recursion part to tell the order of recursion: left tree first and then right one.

void min_BST(BinNode* &head, BinNode *parent, int a[], int start, int end){
	if(start<=end){
		int mid=(start+end)>>1;
		node[cnt].data = a[mid];
		node[cnt].parent = parent;
		head = &node[cnt++];
		min_BST(head->lChild, head, a, start, mid-1);
		min_BST(head->rChild, head, a, mid+1, end);
	}
}

Here is the whole code.

// Ch4-3: create a balanced minimal BST from an ordered array 

#include 
#include 
using namespace std;

const int maxn=100;

int jerry = 0;

struct BinNode{
    int data;
    BinNode *parent, *lChild, *rChild;
};
BinNode node[maxn];
int cnt;

//BinNode *head = NULL; if use ver2, need to declare head as global var
void init(){
	memset(node, '\0', sizeof(node));
	cnt=0;
}

void min_BST(BinNode* &head, BinNode *parent, int a[], int start, int end){
	if(start<=end){
		int mid=(start+end)>>1;
		node[cnt].data = a[mid];
		node[cnt].parent = parent;
		head = &node[cnt++];
		min_BST(head->lChild, head, a, start, mid-1);
		min_BST(head->rChild, head, a, mid+1, end);
	}
}
// if "BinNode* &head" is not inside of method, then it must be a global variable
// so as to be changed every time, also, it will be less clear in recursion:
// which is first? lChild or rChild. So I prefer to have "BinNode* &head" inside 
// of the method, need to make a call by reference that we can modify the value
/*
void min_BST_ver2(BinNode *parent, int a[], int start, int end){
    if(start <= end){
        int mid = (start + end)>>1;
        node[cnt].data = a[mid];
        node[cnt].parent = parent;
        head = &node[cnt++];
        min_BST_ver2(head, a, start, mid-1);
        min_BST_ver2(head, a, mid+1, end);
    }
}
*/

int height(BinNode* head){
    if(head==NULL) 
        return 0;
    else{
		return max(height(head->lChild), height(head->rChild))+1;
	}
}

int main(){
	init();
	int a[] = {
		0,1,2,3
	};
    
    
	BinNode *head = NULL;  //if use ver2, need to declare head as global var
	min_BST(head, NULL, a, 0, 3);
    
    cout<<"\nheight: "<data<lChild)<<"  " <lChild << "dizhi\n";
    cout<lChild->data<rChild->data<rChild->rChild->data<// Ch4.3: Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).

// based on Hawstein's solution
// and modified into class version according to zyli github

#include 
#include 
#include 

using namespace std;

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

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

	BinNode* getRoot() const{return m_root;}
	// node the 1st parameter cannot be "BinNode* &pnode"
	BinNode* balanceTree(BinNode* pnode, BinNode *parent, T *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);		
	}
};


int main(){
    cout << "hello"< *bt = new BTree();
	BinNode *proot = bt->balanceTree(bt->getRoot(), NULL, arr, low, high);
    bt = new BTree(proot);
    bt->PrintPre(bt->getRoot());
}

3、结合物理学与深度学习的连续时间动力学模型 本文探讨了如何结合物理学原理与深度学习技术,构建连续时间动力学模型,以提升机器人系统对复杂动态环境的建模与控制能力。重点介绍了深度拉格朗日网络(DeLaN)和深度哈密顿神经网络(HNN)的理论基础及其在模拟与物理系统中的实验表现,并展示了其在逆向模型、前向模型以及能量控制任务上的优越性能。文章还分析了当前方法的局限性,并提出了未来研究方向,如接触处理、广义坐标学习及优化损失函数等。 阅读详情

相关推荐

前端零代码丨UIOTOS + NodeRed:无代码搭建工控上位机的绝配组合!

用户类型价值硬件产品商软硬一体方案,无代码GUI上位机成新卖点软件服务商售前原型演示利器,交付成本降低至1/3平台开发商融合无代码技术,同质化竞争中脱颖而出🚀 总结UIOTOS的前端零代码设计(页面嵌套、属性继承、节点连线)与NodeRed的后端流程自动化,堪称工控与IoT开发的黄金组合。无论是串口通信、SCADA监控还是业务管理,均能实现“无代码交付”,真正让前端不求人,硬件交互触手可及!探索更多案例:UIOTOS官网在线体验。

2502_91192798的博客 757

【剑指Offer】数组中的逆序对

题目 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。 思路 直接上了个力扣的代码,自己写了较详细的注释。 题解:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/solution/shu-zu-zhong-de-ni-xu-dui-by...

hanmin822的博客 236

Windows Server双机热备实战:用IIS+ARR+NLB搭建高可用Web服务(附避坑指南)

本文详细介绍了在Windows Server环境下,利用IIS、ARR(Application Request Routing)和NLB(Network Load Balancing)构建高可用Web服务的实战方案。通过双机热备架构,实现从网络层到应用层的全面冗余与智能流量分发,并提供了关键的配置步骤、常见避坑指南及性能调优建议,确保企业级Web服务的连续性与可靠性。

weixin_29210759的博客 388

数组

一、数组的基本概念: 数组是一种线性表数据结构。它用一组连续的内存空间,来存储一组具有相同类型的数据。数组支持随机访问,根据下标随机访问的时间复杂度为O(1)。 java中数组和对象一样,存储在堆中,由java虚拟机直接生成。数组可以通过下标直接访问,但是不允许有空洞,即数组中的数据必须是连续的,不能有null值。 二、无序数组不允许重复 package com.xwiam.algori...

wuweiwoshishei的博客 262

[ESP32]:基于esp-modbus实现serial主机

[ESP32]:基于esp-modbus实现serial主机

LINKKEEE的博客 2571

Proteus 8.17安装部署全指南:避坑、汉化与STM32库配置

Proteus是一款广泛用于单片机仿真与电路设计的EDA工具,其核心价值在于支持嵌入式系统原型验证与教学实践。Proteus 8.17版本重构了许可证机制(LIC Engine)、依赖环境(VC++ 2019/NET 4.8)及路径加载逻辑,导致传统安装方法失效。掌握正确安装部署流程,是保障原理图绘制、VSM仿真(如51/STM32/ESP32)及外设波形观测(OLED、SPI、ADC)稳定运行的前提。本文聚焦Windows平台下零失败安装路径选择、权限配置、汉化实施与高频器件(如STM32F103)元件库

weixin_33377602的博客 404

PHP 的数组 (array) 是宇宙中最强大的数据结构之一:既是列表,又是字典,还是栈/队列。。

特性表现评价通用性列表 + 字典 + 栈 + 队列⭐⭐⭐⭐⭐ (无敌)有序性天然保持插入顺序⭐⭐⭐⭐⭐ (稀缺特性)易用性语法糖丰富,函数库庞大⭐⭐⭐⭐⭐ (开发者友好)内存效率较高开销 (元数据多)⭐⭐ (相对较弱)极致性能头部操作慢,非专用数值存储⭐⭐⭐ (够用,但非极致)终极心法PHP 数组是“工程实用主义”的巅峰之作。它牺牲了极致的内存紧凑性和部分算法复杂度,换取了无与伦比的开发效率和灵活性。array能搞定一切。

长风破浪会有时的博客 145

空间节省算法在Rowhammer防御中的应用与优化

空间节省算法(Space-Saving)是一种高效的流式数据处理算法,主要用于在有限内存条件下实时识别数据流中的高频项。其核心原理是通过维护固定大小的计数器表,动态跟踪最活跃的元素,适用于处理无限数据流场景。该算法在内存安全领域具有重要价值,特别是在Rowhammer攻击防御中,能够有效检测异常高频访问的DRAM行地址。通过结合计数器衰减策略(如DSAC)和混合架构设计,空间节省算法可以在保证低硬件开销的同时,提供高精度的攻击检测能力。这些技术已成功应用于现代DDR5内存控制器,展现出优异的工程实践价值。

weixin_31139479的博客 606

面试题:m个长度为n的ordered array,求top k 个 数字

package com.sinaWeibo.interview; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www....

weixin_30576827的博客 308

FMCW雷达基本原理(通俗易懂)篇2

FMCW雷达(Frequency Modulated Continuous Wave Radar)是一种使用频率调制连续波的雷达系统。它使用一个发射器发射一个连续波,但是这个连续波的频率会在一定的范围内不断变化。当这个连续波遇到一个目标物体并被反射回来时,接收器会检测到这个反射波的频率变化,并计算出目标物体的距离和速度等信息。FMCW雷达的原理可以理解为:1.发射器发射一个连续波,这个波的频率会从一个固定的起始频率开始,逐渐变化至一个固定的终止频率。

研二小学鸡的博客 7695

华为OD新系统机试真题 - 失灵的键盘(C/C++/Py/Java/Js/Go)

华为OD新系统机试真题 【失灵的键盘】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD4月15号机试真题【失灵的键盘】

qq_45776114的博客 1075

Java数据结构与算法(1) - ch02有序表(OrderedArray)

有序表需要掌握的插入方法,删除方法和二分法查找方法。 插入方法: 从前往后找到比要插入的值大的数组项,将该数组项及之后的项均后移一位(从最后一项起依次后移),最后将要插入的值插入当前数组项。 删除方法: 从前往后找到要删除的项,将该数组项之后的项均前移一位(从该数组项后一项起依次往前移); 二分法查找: 通过将数组数据项范围不断对半分割来查找特定的数据项。 示例代码: package...

weixin_34416649的博客 181

【LAMMPS学习】五、LAMMPS命令(1) LAMMPS 输入脚本

本部分描述了 LAMMPS 输入脚本的格式以及其中的命令如何用于定义 LAMMPS 模拟。主要包括以下内容。

荷塘阅色 2646

insertion sort with binary search applied on ordered array

insertion sort with binary search applied on ordered array =&gt; o(nlogn)

zjfzjf2012的博客 355

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

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

复习recursion

1. 如果这样 private void lop(int x, int size, double[] jerry){ if(x==size-1){ jerry[x] = -1; return; } if(jerry[x+1]==0){ jerry[x+1] = jerry[x]- 1; //if so, lop(x+1, size, jerry); }

纯黑老白的专栏 1009

Ch2-5: find the beginning of loop in a circular linked list---two solutions

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer poin

纯黑老白的专栏 986

Ch3-2: implement min() for a stack with O(1)

Here will show the thinking flow from bad to good. idea 1: const int MAX_INT = ~(1<<31);//2147483647 struct node{ int val, min; }; class StackWithMin{ public: StackWithMin(int size){

纯黑老白的专栏 956
上一篇: Ch4-2: find if 2 given vertices are connected in a directed map,BFS + JUST fix the DFS recursion
下一篇: Ch4.4: array to Balanced tree and print each level
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值