【数据结构】实验报告11 将二叉树后序线索化并进行后序遍历

线索二叉树(前中后序线索化/遍历/画线索) 考研数据结构笔记。线索二叉树(前中后序线索化/遍历/画线索) 阅读详情

一、实验目的和要求

目的:设计并实现基于后序线索二叉树的后序遍历的非递归算法。
要求:
(1)创建二叉树。
(2)转换为后序线索二叉树。
(3)实现后序遍历的非递归算法。
(4)其它要求同课后作业-01要求。

二、实验环境

软件环境:visual stdio 2017
硬件环境:①CPU:Intel(R)Core(TM)i7-8565U CPU @1.80Ghz
②内存:8.0GB

三、实验内容

设计并实现基于后序线索二叉树的后序遍历的非递归算法。

四、实验过程

4.1 任务定义和问题分析

本次实验主要解决的任务可以分解为:建立一颗二叉树,将二叉树进行后序线索化,对线索化后的二叉树进行后序遍历。

4.2 数据结构的选择和概要设计

数据结构采取链式结构建立树,每个结点增加部分辅助空间以服务于二叉树的线索化。
通过编写后序线索化函数将树中叶子结点空闲的左儿子指针和右儿子指针指向逻辑上的前驱后继。

4.3 详细设计

每个二叉树结点新增1个指针,2个bool型标记。指针为父亲指针,指向结点的父亲。两个bool型标记表示该节点的左/右儿子指针是否指向逻辑上的前驱后继还是子节点。

template<typename T>
struct BTNode
{
	T data;
	bool ltag;
	bool rtag;
	BTNode<T>* leftson;
	BTNode<T>* rightson;
	BTNode<T>* father;
};

将二叉树封装为类:

template<typename T>
class BT
{
public:
	BT();
	~BT();
	void build();                //建树
	void erase();                //删除
	void ToPoClueBT();              //转换为后序线索二叉树
	void PoTraversal();             //后序线索遍历
protected:
	void build(BTNode<T>* & father, BTNode<T>* & root);
	void erasebyRecursion(BTNode<T>* & root);
	void ToPoClueBTAssist(BTNode<T>*&root, string& str);   //转换函数的辅助函数
private:
	BTNode<T>* root;
	bool isclued;            //标记是否被线索化
	bool isbuilt;            //标记是否已建树
};

建树采取输入扩展先序序列递归建树的方法。

template<typename T>
void BT<T>::build()
{
	if (isbuilt)
	{
		cout << "The binary Tree has been built , if you want to rebuild , please erase it ! " << endl;
		return;
	}
	this->isbuilt = true;
	BTNode<T>* null = NULL;
	build(null, this->root);
}

template<typename T>
void BT<T>::build(BTNode<T>* & father,BTNode<T>* & root)
{
	char ch = cin.get();
	if (ch == '.')
	{
		root = NULL;
		return;
	}
	root = new BTNode<T>;
	root->father = father;
	root->data = ch;
	root->ltag = false;
	root->rtag = false;
	build(root, root->leftson);
	build(root, root->rightson);
}

对于二叉树的后序线索化,采用后序递归遍历的方法找到子节点为空的结点,将其空指针指向前驱/后继。有以下几种情况:
①左孩子结点为空:

  • 若结点有右孩子,则该节点的左孩子指针指向其右孩子,同时标记值ltag更新为true.
  • 若结点无右孩子,且该结点的父结点有左孩子且不为自己,则该结点的左孩子指针指向其父节点的左孩子,同时标记值ltag更新为true.
  • 若结点无右孩子,且该结点的父节点无左孩子,则再继续探寻父节点的父节点,探寻方式与判断条件与前述相同(我是利用了一个string值随着作为函数参数传入判断是当前结点是父节点的左孩子还是右孩子),直到所探寻的父节点有左孩子结束探寻。该结点的左孩子指针指向探寻到的父节点的左孩子,同时标记值ltag更新为true.
  • 【期末复习的时候发现之前写的这篇博客在解析上存在疏漏,但是代码还是真确的】 缺少的情况为结点无右孩子且是父节点的左儿子。在这种情况下,迭代(用词可能不正确,就是不断用父节点的父节点来更新“父节点”)父节点直至某个“父节点”是其父节点的右儿子且这个“父节点”的父节点的左儿子存在,则结点的前驱为这个“父节点的左儿子”。【这种情况在代码上可以和前两种合并,具体体现详见后述代码】
  • 不满足上述者,则该结点为整棵树的后序线索的最前的前驱。
    ②右孩子结点为空:
  • 若结点为其父节点的右孩子,则该结点右孩子指针指向其父节点,同时标记值rtag更新为true
  • 若结点为其父节点的左孩子,且该结点的父节点无右孩子,则该结点右孩子指针指向其父节点,同时标记值rtag更新为true
  • 若结点为其父节点的左孩子,且该结点的父节点有右孩子,则该结点右孩子指针指向其父节点的右子树的最左的左叶子结点。
template<typename T>
void BT<T>::ToPoClueBT()
{
	if (isclued)
	{
		cout << "The binary Tree has been clued , your instruction is to be canceled ! " << endl;
		return;
	}
	this->isclued = true;
	string empty = "";
	ToPoClueBTAssist(this->root, empty);
}

 template<typename T>
void BT<T>::ToPoClueBTAssist(BTNode<T>* & root, string& str)
{
	if (root->leftson != NULL) ToPoClueBTAssist(root->leftson, str += '0');
	if (root->rightson != NULL)    ToPoClueBTAssist(root->rightson, str += '1');
	if (root->leftson == NULL)
	{
		if (root->rightson != NULL)
		{
			root->ltag = true;
			root->leftson = root->rightson;
		}
		else
		{
			BTNode<T>* node = root;
			for (int i = str.size() - 1; i > 0; i--)
			{
				if (str[i] == '1')
				{
					root->ltag = true;
					if (node->father->leftson != NULL)
					{
						root->leftson = node->father->leftson;
						break;
					}
				}
				node = node->father; 
			}
		}
	}
	if (root->rightson == NULL)
	{
		root->rtag = true;
		if (str[str.size() - 1] = '1') root->rightson = root->father;
		else
		{
			BTNode<T>* Node = root->father->rightson;
			if (Node == NULL) root->rightson = root->father;
			else
			{
				while (Node->leftson != NULL) { Node = Node->leftson; }
				root->rightson = Node;
			}
		}
	}
}

对于后序线索二叉树的后序遍历,初始化node=根结点,建立循环,用字符串记录各结点data值,每次记录后便将node更新为前驱,更新的时候利用ltag与rtag进行判断逻辑上的前驱的位置。最后将字符串反向输出。

template<typename T>
void BT<T>::PoTraversal()
{
	string OppositeResult, Result;
	BTNode<T>* node = this->root;
	while (node != NULL)
	{
		OppositeResult += node->data;
		if (node->ltag) node = node->leftson;
		else 
		{
			if (node->rightson != NULL && !node->rtag) node = node->rightson;
			else node = node->leftson;
		}
	}
	for (int i = OppositeResult.size() - 1; i >= 0; i--)  Result += OppositeResult[i];
	cout << "The result of postorder clue binary tree traversal is:" << Result << endl;
}

五、测试及结果分析

5.1 实验数据

测试如图所示的二叉树
在这里插入图片描述

5.2 结果及分析

结果正确 ,且在写算法时各种测试的数据皆能正确输出。
在这里插入图片描述

六、实验收获

通过本次实验,我掌握如何将二叉树线索化的方法,同时本次实验也加深了我对数据结构的理解,自己的编码水平也得到了一定的提升。

七、源代码

#include<iostream>
#include<string>
using namespace std;

template<typename T>
struct BTNode
{
	T data;
	bool ltag;
	bool rtag;
	BTNode<T>* leftson;
	BTNode<T>* rightson;
	BTNode<T>* father;
};

template<typename T>
class BT
{
public:
	BT();
	~BT();
	void build();                //建树
	void erase();                //删除
	void ToPoClueBT();              //转换为后序线索二叉树
	void PoTraversal();             //后序线索遍历
protected:
	void build(BTNode<T>* & father, BTNode<T>* & root);
	void erasebyRecursion(BTNode<T>* & root);
	void ToPoClueBTAssist(BTNode<T>*&root, string& str);   //转换函数的辅助函数
private:
	BTNode<T>* root;
	bool isclued;            //标记是否被线索化
	bool isbuilt;            //标记是否已建树
};

template<typename T>
BT<T>::BT()
{
	root = new BTNode<T>;
	isclued = false;
	isbuilt = false;
}

template<typename T>
BT<T>::~BT()
{
	this->erase();
}

\
template<typename T>
void BT<T>::erase()
{
	if (isbuilt)
	{
		BTNode<T>* node = new BTNode<T>;
		while (root != NULL)
		{
			node = root;
			if (root->ltag) root = root->leftson;
			else
			{
				if (root->rightson != NULL && !node->rtag) root = root->rightson;
				else root = root->leftson;
			}
			delete node;
		}
	}
	else  this->erasebyRecursion(this->root);
	isbuilt = 0;
	isclued = 0;
	root = NULL;
}

template<typename T>
void BT<T>::erasebyRecursion(BTNode<T>* &node)
{
	if (node == NULL) return;
	erasebyRecursion(node->leftson);
	erasebyRecursion(node->rightson);
	delete node;
}

template<typename T>
void BT<T>::build()
{
	if (isbuilt)
	{
		cout << "The binary Tree has been built , if you want to rebuild , please erase it ! " << endl;
		return;
	}
	this->isbuilt = true;
	BTNode<T>* null = NULL;
	build(null, this->root);
}

template<typename T>
void BT<T>::build(BTNode<T>* & father,BTNode<T>* & root)
{
	char ch = cin.get();
	if (ch == '.')
	{
		root = NULL;
		return;
	}
	root = new BTNode<T>;
	root->father = father;
	root->data = ch;
	root->ltag = false;
	root->rtag = false;
	build(root, root->leftson);
	build(root, root->rightson);
}

template<typename T>
void BT<T>::ToPoClueBTAssist(BTNode<T>* & root, string& str)
{
	if (root->leftson != NULL) ToPoClueBTAssist(root->leftson, str += '0');
	if (root->rightson != NULL)    ToPoClueBTAssist(root->rightson, str += '1');
	if (root->leftson == NULL)
	{
		if (root->rightson != NULL)
		{
			root->ltag = true;
			root->leftson = root->rightson;
		}
		else
		{
			BTNode<T>* node = root;
			for (int i = str.size() - 1; i > 0; i--)
			{
				if (str[i] == '1')
				{
					root->ltag = true;
					if (node->father->leftson != NULL)
					{
						root->leftson = node->father->leftson;
						break;
					}
				}
				node = node->father; 
			}
		}
	}
	if (root->rightson == NULL)
	{
		root->rtag = true;
		if (str[str.size() - 1] = '1') root->rightson = root->father;
		else
		{
			BTNode<T>* Node = root->father->rightson;
			if (Node == NULL) root->rightson = root->father;
			else
			{
				while (Node->leftson != NULL) { Node = Node->leftson; }
				root->rightson = Node;
			}
		}
	}
}

template<typename T>
void BT<T>::ToPoClueBT()
{
	if (isclued)
	{
		cout << "The binary Tree has been clued , your instruction is to be canceled ! " << endl;
		return;
	}
	this->isclued = true;
	string empty = "";
	ToPoClueBTAssist(this->root, empty);
}

template<typename T>
void BT<T>::PoTraversal()
{
	string OppositeResult, Result;
	BTNode<T>* node = this->root;
	while (node != NULL)
	{
		OppositeResult += node->data;
		if (node->ltag) node = node->leftson;
		else 
		{
			if (node->rightson != NULL && !node->rtag) node = node->rightson;
			else node = node->leftson;
		}
	}
	for (int i = OppositeResult.size() - 1; i >= 0; i--)  Result += OppositeResult[i];
	cout << "The result of postorder clue binary tree traversal is:" << Result << endl;
}

int main()
{
	BT<char> tree;
	cout << "Please enter the expanded tree preorder list :";
	tree.build();
	tree.ToPoClueBT();
	tree.PoTraversal();
	return 0;
}
树和二叉树-第6章-《数据结构题集》习题解析-严蔚敏吴伟民版 习题集解析部分 第6章 树和二叉树 ——《数据结构题集》-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ 《数据结构-C语言版》(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接☛☛☛ 《数据结构》课本源码合辑        习题集全解析  链接☛☛☛ 《数据结构题集》习题解析合辑         相关测试数据下载  链接☛ 数据包  ... 阅读详情

相关推荐

千兆网口电路设计中的EMC防护与TVS选型实战

本文深入探讨了千兆网口电路设计中面临的EMC挑战,并提供了TVS二极管选型的实战指南。文章分析了ESD、浪涌和EFT等主要威胁,重点阐述了如何根据工作电压、钳位电压和关键的低结电容等参数为PHY芯片选择匹配的TVS器件,以确保在严苛电磁环境下实现可靠的信号完整性与防护。

m3n5b7v8c9x的博客 530

数据结构 实验报告11

一、实验目的和要求 目的:熟悉后序线索二叉树并实现后序遍历 要求: (1)创建二叉树。 (2)转换为后序线索二叉树。 (3)实现后序遍历的非递归算法。 二、实验环境 编译器:Vscode +DevC++ 系统:Windows10 CPU:i5-8265U@1.60GHz 三、实验内容 (1)创建二叉树。 (2)转换为后序线索二叉树。 (3)实现后序遍历的非递归算法。 四、实验过程 4.1 任务定义和问题分析 1.创建二叉树 沿用之前使用二叉树创建模板;(若兼容后...

zRNA__的博客 1738

大模型部署之二--vllm+docker部署

本文介绍了在openEuler系统上快速本地部署Qwen3-8B、DeepSeek-R1、Qwen2.5deng 大模型的完整流程。通过modelscope管理模型,采用vllm+docker模式实现部署。主要步骤包括:1)安装conda环境;2)配置docker;3)下载模型;4)编写docker compose配置文件;5)启动服务。特别针对2卡4090服务器提供了参数配置建议,并给出常用命令和参考资料。文档还提示不同系统和平台可能存在差异,为开发者提供了本地部署大型语言模型的实用指南。

weixin_39279863的博客 1133

《数据结构》实验报告五:二叉树

《数据结构》实验报告:二叉树基本概念 + 结点总数 + 叶子结点数 + 二叉树深度 + 中序非递归遍历 算法实现

Amentos的博客 1万+

STM32F103 CAN通信避坑指南:从标准库配置到收发测试的完整流程

本文详细介绍了STM32F103 CAN通信的实现流程,从标准库配置到收发测试,帮助开发者避开常见陷阱。内容涵盖硬件连接、波特率计算、过滤器配置及错误处理等关键环节,提供实用的代码示例和调试技巧,确保CAN通信的稳定性和可靠性。

weixin_30627341的博客 324

告别拼音!用VSCode+Dosbox-X搞定汇编语言课程设计里的中文姓名显示

本文详细介绍了如何利用VSCode和Dosbox-X解决汇编语言课程设计中中文姓名显示的问题。通过配置开发环境、理解GB2312编码及正确使用汇编指令,学生可以告别拼音显示,实现中文字符的正确输出。文章还提供了完整的代码示例和常见问题排查方法,帮助读者快速掌握这一实用技能。

weixin_30872337的博客 424

二叉树先序、中序、后序的递归和非递归实现

1 二叉树的创建 今天看了一下二叉树,突然发现书上讲了它的遍历方式,却没有讲如何创建二叉树,如果不先创建的话,我感觉即使写出了遍历的代码,也没法遍历啊。真的是觉得很奇怪!这里附上我写的代码,首先我这棵树我是用数组先存的,按照完全二叉树存的,没有节点的地方值为0 结构如下: int[] arr = { 1, 2, 3, 4, 5, 0, 0, 0, 0, 6, 7 }; int index =...

dream_follower的博客 1094

信号与系统——卷积、相关运算与FFT关系以及工程应用(三)

卷积运算、相关运算

qq_35028634的博客 6611

线索二叉树的前序、中序、后续遍历

线索化二叉树

kittimzhe的博客 1710

PyRoki:机器人运动学优化库入门教程

PyRoki:模块化机器人运动学优化工具包 本文介绍了PyRoki,一个模块化、可扩展且跨平台的机器人运动学优化工具包。PyRoki通过统一变量和成本函数接口,简化了逆运动学、轨迹优化和运动重定向等任务的实现,支持CPU、GPU和TPU加速。实验表明,PyRoki在批量逆运动学任务中比现有GPU加速库cuRobo快1.4-1.7倍,且收敛误差更低。其模块化设计支持快速集成新成本函数,并通过交互式可视化工具优化参数配置。PyRoki开源发布,为机器人运动优化提供了灵活高效的解决方案。

机器人控制与仿真应用 1291

【基础-多选】HarmonyOS鸿蒙应用开发

1、HarmonyOS提供了基础的应用加固安全能力,包括混淆、加密和代码签名能力。正确2、可以通过ohpm uninstall 指令下载指定的三方库。错误3、支持模块化开发是指一个应用通常会包含多种功能,将不同的功能特性按模块来划分和管理是一种良好的设计方式。在开发过程中,我们可以将每个功能模块作为一个独立的Module进行开发,Module中可以包含源代码、资源文件、第三方库、配置文件等,每一个Module可以独立编译,实现特定的功能。这种模块化、松耦合的应用管理方式有助于应用的开发、维护与扩展。正确。

江拥羡橙的博客 1万+

先序,中序,后序线索二叉树

 //后序线索,这种方法不容易想到 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 6 using namespace std; 7 8 struct TREE{ 9 int...

aipiannian6725的博客 309

BLDC控制实验:方波、霍尔、开环、定速、正转

本文记录博主学习BLDC控制软件的过程。 文章目录1 概述2 软件架构及工具链2.1 软件架构2.2 工具链3 底层代码生成3.1 时钟配置3.2 按键输入配置 1 概述 本文是博主第一次做BLDC控制实验,目的仅仅是写代码让BLDC转起来,而暂时不考虑其他复杂的需求。 控制的方法是比较简单的六步换相法。利用电机内部的霍尔传感器,通过参考电机厂家给出的二二导通换相表,生成方波来控制电机。第一次实验为了简化需求,只考虑开环、定速、正转的情况: 开环:不考虑速度反馈,直接给PWM让电机转起来; 定速:不考虑速

u013288925的博客 1万+

使用C语言实现前,中,后序线索化二叉树

使用C语言实现前,中,后序线索化二叉树

Keep_Trying_Go的博客 1115

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

后序线索化二叉树

1 void Tree::_postTree(Node * root, Node *&pre) { 2 if (root == NULL) { // 空值不操作 3 return; 4 } 5 else { 6 _postTree(root->lchild, pre); // 递...

weixin_30307921的博客 537

深度学习工具包 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');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【数据结构C++之看懂就这一篇】二叉树(动画遍历)

二叉树的详细内容

追当今朝天骄,忆顾往昔豪杰!!! 1666

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

后序线索二叉树—源码

//后序线索二叉树 //二叉树--链式存储 #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 struct ThreadNode{ //数据域 int value; //左孩子 struct ThreadNode *lChild; //右孩子 struct ThreadNode *rChild; //1--前驱结点 0--左孩子 int ltag; //1--.

qq_42824207的博客 292

考研二战_数据结构_day11_线索二叉树

线索树的代码属于想起来很复杂,但是实现起来就非常简单的东西。主要是在实现过程中需要注意不要转圈、以及是否需要处理最后一个结点。

m0_49412669的博客 667

数据结构 | 【树与二叉树】考研相关结论与习题

1、一棵完全二叉树上有1001个结点,其中叶结点的个数是() A.250 B.500 C.254 D.501 解:法1: 结点总数为1001,奇数,故n1=0。由n=n0+n1+n2;n2=n0-1得:n=n0+n1+n0-1,即1001=2*n0-1,得 n0=1002/2=501. 法2: 完全二叉树的最后一个结点编号为1001,由于其为奇数,因此其双亲结点编号为(1001-1)/2=500(若为偶数则直接除以2),双亲之后的结点都是叶结点,因此总叶结点n0=最后一个编号..

"You are worthy! You can do it!" 7418

万字长文详解数据结构:树 | 第6章 | Java版大话数据结构 | 二叉树 | 哈夫曼树 | 二叉树遍历 | 构造二叉树 | LeetCode练习 | Java

线性结构是1️⃣对1️⃣的关系,如果处理1️⃣对多的情况,则需要使用树形结构🌲树形结构包括:二叉树、AVLAVLAVL树、B树、红黑树等,而二叉树是学其它树的基础,学好了二叉树才能在后续更难的树时得心应手🫱在介绍二叉树之前同时还需要整体的介绍一下树这个数据结构**定义:**🌳树是nn≥0n(n≥0)nn≥0个结点的有限集。当n0n = 0n0时,称为空树。在任意一棵非空树中应满足:有且仅有一个特定的称为根(Root)的结点;当n1n>1n1。

神马都会亿点点的毛毛张 1258

数据结构--树与二叉树练习题

设一棵二叉树的先序序列:ABDFCEGH,中序序列:BFDAGEHC. (1)画出这棵二叉树。 (2)画出这棵二叉树的后序线索树。 (3)将这棵二叉树转换成对应的树(或森林) 假设用于通信的电文仅由8个字母组成,字母在电文中出现的频率分别为0.07、0.19、0.02、0.06、0.32、0.03、0.21和0.10。 (1)试为这8个字母设计哈夫曼编码。 (2)试设计另一种由二进制表示的等长编码方案。 (3)对于上述实例,比较两种方案的优缺点。 【408真题】如果一棵非空k(k≥2)叉树T中每..

丹丹的后花园 1万+

《数据结构》_PTA_数据结构作业5:树和二叉树

判断题: 1-1 某二叉树的前序和中序遍历序列正好一样,则该二叉树中的任何结点一定都无右孩子。 F 1-2 存在一棵总共有2016个结点的二叉树,其中有16个结点只有一个孩子。 F 1-3 哈夫曼树中一定没有度为 1 的结点。 T 1-4 一棵非空二叉树,若先序遍历与后序遍历的序列相反,则该二叉树只有一个叶子结点。 T 1-5 某二叉树的前序和中序遍历序列正好一样,则该二叉树中的任何结点一定都无左孩子。 T 1-6 已知一棵二叉树的先序遍历结果是ABC, 则CAB不可能是中序遍历结果。 T 1-7 一

国家一级致癌物 8242

数据结构——5.3 二叉树的遍历和线索二叉树

2.n个结点的线索二叉树具有2n个链域指针,除了根节点外,每个结点都被一个指针指向,因此用掉了n-1个指针,还剩下n+1个指针用作线索。(2)可以确定结点间的祖先关系,如先序XY,后序YX则X为Y的双亲,例如:先序aebdc、后序bcdea,则根节点a的孩子只有e。(6)次根结点(移除根节点后变成根节点的结点,即原根节点的孩子结点)在不含根节点的序列中也在序列首或序列尾。(2)若已知入栈次序和入栈元素个数n,则出栈的序列个数有:个,即可能的二叉树个数。(1)前序序列作为入栈的次序,中序序列作为出栈的次序。

honev.blog.youkuaiyun.com 981

2020-11-17 中序线索二叉树

中序线索二叉树 先序与后序线索化还没实现过 #include<iostream> #include<stack> using namespace std; #define TElemType double typedef enum{Link,Thread} PointerTag; typedef struct BiThrNode { TElemType data; struct BiThrNode* lchild,* rchild; PointerTag LT

weixin_45888287的博客 190

数据结构---第五章树与二叉树---二叉树的遍历和线索二叉树---选择题

数据结构---树与二叉树---二叉树的遍历和线索二叉树---选择题

programmer9的博客 2729
上一篇: 【数据结构】 顺序、链式存储结构的森林递归遍历、层次遍历求高度
下一篇: Qt实现哈夫曼编码解压缩软件详解
RealWeakCoder
博客等级 码龄7年 236粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值