建立一棵含有n个结点的二叉树,采用二叉链表存储;
输出前序、中序、后序、、层序遍历该二叉树的遍历结果。
定义二叉树的数据类型——二叉树结点结构体BiNode。建立二叉链表可以采用扩展二叉树的一个遍历序列,例如前序序列,将扩展二叉树的前序序列由键盘输入,建立该二叉树的二叉链表存储。
简单起见,本实验假定二叉树的数据元素为char型
用模板类改写

创建一个拓展二叉树,我们用#来代指虚结点:

前序遍历的实现:

中序遍历的实现:

后序遍历的实现:

层序遍历的实现:
墨衍会员
·
AI 创作全网分发
墨衍智能分发
本文由作者通过墨衍一键同步至各平台
1分发平台
900+累计阅读
958平均单平台
已同步平台
优快云
微信公众号
微博
知乎
掘金
百家号
博客园
抖音
小红书
你的文章也可以这样分发
墨衍会员 ¥399起/年,写一次发全网
我也要 →
码龄7年
建立一棵含有n个结点的二叉树,采用二叉链表存储;
输出前序、中序、后序、、层序遍历该二叉树的遍历结果。
定义二叉树的数据类型——二叉树结点结构体BiNode。建立二叉链表可以采用扩展二叉树的一个遍历序列,例如前序序列,将扩展二叉树的前序序列由键盘输入,建立该二叉树的二叉链表存储。
简单起见,本实验假定二叉树的数据元素为char型
用模板类改写

创建一个拓展二叉树,我们用#来代指虚结点:

前序遍历的实现:

中序遍历的实现:

后序遍历的实现:

层序遍历的实现:
C++实现二叉树(二叉链表)前序,中序,后序,层序遍历
若不为空,则将二叉树的根节点入队,然后开始队列不为空的判断,在此判断(循环)中:输出此结点,并对他的左右子树进行判断,左右子树不为空则进入队列,为空则直接跳过,最后再将遍历过的结点出队,这样一次判断(循环)便完成了。设二叉树中的结点均为一个字符,假设扩展二叉树的前序遍历序列有键盘输入,root为指向跟结点的指针,二叉链表的建立过程是:首先输入根节点,若输入的是一个“#”字符,则表明该二叉树为空树,也就是root=NULL;的语句顺序是:输出根节点,访问左子树,访问右子树;遍历,结果为:DGBAECF;
二叉树遍历(C++版本)
访问结点所做的操作依赖于具体的应用问题。遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。前·中·后序遍历:前序遍历亦称先序遍历)——访问根结点的操作发生在遍历其左右子树之前。中序遍历访问根结点的操作发生在遍历其左右子树之中(间)。后序遍历访问根结点的操作发生在遍历其左右子树之后。由于被访问的结点必是某子树的根,。NLRLNR和LRN分别又称为先根遍历、中根遍历和后根遍历。相应地,前序、中序和后序遍历都属于。
C++实现二叉树的创建以及四种遍历方式(递归与非递归)
C++实现二叉树的创建以及四种遍历方式(递归与非递归) 语言这个东西不用真的会忘, 我记得前前后后C++的基本语法我也看了好几遍了,一直没有动手写过什么东西,所以一遍遍的看,一遍遍的忘… … 正好最近在看数据结构,想着自己用C++来实现一下,一方面是熟悉整个逻辑过程,加深对数据结构的理解,另一方面,也熟悉一下C++。 下图是我们要创建的二叉树,其中的#代表空字符; 为了验证我们程序的正确性,现给出此二叉树的四种遍历结果: 前序遍历(Preorder traversal): A B C D E F
二叉树三种遍历(动态图+代码深入理解)
文章目录一、图示理解(图片是一位前辈所留,在此感谢)1、先序遍历2、中序遍历3、后序遍历4、层序遍历二、深入理解三种遍历让我们来理解一下绕着外围跑一整圈的真正含义是:遍历所有结点时,都先往左孩子走,再往右孩子走。下面做一个实例吧三、代码实现加以理解以下是C语言全部代码实现下面是同样的例子用c++实现,大家可以参考一下 一、图示理解(图片是一位前辈所留,在此感谢) 1、先序遍历 先序遍历可以想象成,...
二叉树遍历算法:前序、中序、后序与层序
二叉树是计算机科学中经常使用的一种数据结构,它的每个节点最多有两个子节点,通常被称作左子节点和右子节点。这种结构的特点使得它非常适合于实现二叉搜索树、堆和哈希树等复杂的数据结构。二叉树的定义可以归纳为以下几点:一个二叉树的根节点只有一个,这是树的最顶层节点。每个节点最多有两个子节点,分别是左子节点和右子节点。左子节点和右子节点可以不存在(空树情况下)。每个子树也是二叉树。中序遍历是二叉树遍历方法中的一种,它按照“左-根-右”的顺序访问树中的节点。
避坑指南:STM32H7不同DMA控制器(MDMA/DMA2D/BDMA)到底该用哪个?
本文深入解析STM32H7系列芯片的DMA控制器选型策略,涵盖MDMA、DMA2D、BDMA等不同控制器的架构原理与性能对比。通过实际项目案例,提供高速TCM数据搬运、GUI加速、低功耗传输等场景的选型决策树,并分享性能优化与调试技巧,帮助工程师避开常见陷阱,提升系统效率。
使用cubeMX生成USB HID设备
本文主要介绍如何使用stm32开发板和cubeMX生成HID设备,供参考。
LAMMPS模拟新合金没势函数?手把手教你用`pair_style hybrid`混合势函数(附FeCMnSiTi案例)
本文详细介绍了在LAMMPS中如何使用`pair_style hybrid`混合势函数模拟新型高熵合金(如FeCMnSiTi),解决缺乏现成势函数的问题。通过实战案例,涵盖参数查找、命令编写、错误排查等关键步骤,特别适合处理多组元体系的复杂相互作用。文章还提供了EAM势函数与LJ势的组合技巧,帮助研究者准确模拟合金性能。
Hermes深入理解及源码解析(二):Hermes的记忆机制
源码位置:tools/memory_tool.py存储内容为两个文件,放在 ~/.hermes/memories/:MEMORY.md:Agent 自己的笔记(环境事实、项目约定、工具坑、学到的东西)—— 上限 2200 字符USER.md:关于用户的画像(角色、偏好、沟通风格)—— 上限 1375 字符作为一个独立的工具注册在系统中(为了让智能体正确使用它,System Prompt()中明确下达了禁令存储目标存储位置数据类型持久偏好/事实MEMORY.md我是谁、我喜欢什么、环境配置。
大模型推理中Prefill与Decode、KV Cache三者说明
Prefill 阶段对输入 Prompt 并行编码,一次性构建KV Cache,属于计算密集型任务。Decode 阶段逐 token 迭代生成,复用KV Cache避免重复计算,属于显存带宽密集型任务。KV Cache是自回归推理的核心基石,用显存换时间,实现两阶段衔接与高效生成。PD 分离通过任务解耦、异构硬件、异步调度、独立批处理,解决传统一体化推理的资源冲突与利用率低下问题,是当前云端大模型推理服务的主流工程优化方向。
AIGC算法工程师 面试八股文
1、简述DDPM的算法原理 2、什么是重参数化技巧?Diffusion Models和VAE中的重参数化技巧是如何使用的? VAE中的重参数化技巧 Diffusion Models中的重参数化技巧 3、什么是马尔可夫过程?DDPM中的马尔可夫链是如何定义的? 马尔可夫过程 DDPM马尔可夫链 4、为什么DDPM前向过程中前期加噪少,后期加噪多? 5、VAE和Diffusion Models中的变分推断是什么? 6、Negative Prompt实现的原理是什么?
YOLOv8改进 | 注意力篇 | YOLOv8引入EMAttention(EMA)注意力
具体来说,除了对全局信息进行编码以重新校准每个并行分支中的通道权重之外,还通过跨维度交互来进一步聚合两个并行分支的输出特征,以捕获像素级的成对关系。3. 跨空间学习:除了在每个并行子网络中建立局部的跨通道交互外,EMA模块还通过跨空间学习方法融合了两个并行子网络的输出特征图。到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv8改进有效涨点专栏,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~
Agisoft Metashape 基于RTK技术的无人机航测免像控技术流程
利用无人机进行航空摄影测量,其使用方便、快速获得成果,已经是测绘行业外业生产的主要方式。RTK定位系统的出现,其厘米级的定位系统,使得航测无人机在作业时,能够准确获取拍照时的GPS位置,能够大量减少像控点,即使不使用像控点也能满足规范对精度的要求。可以有效解决特殊地形(如山川、峡谷、河流、沼泽、水库等)外业人员很难布设像控点的情况。
lwip-2.0.3
lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。
TMS_WebGMaps_1.9.8.0_Full_Source
TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
ssm框架搭建
这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载
2463
5934

被折叠的 条评论
为什么被折叠?



