二叉树的基本操作

2023-05-16

二叉树
先序和中序确定二叉树
在这里插入图片描述

后序以及中序确定二叉树
在这里插入图片描述

// 指针版本
struct node{
	typename data;
	node* lchild;
	node* rchild;
}
// 二叉树建树前根节点不存在,一般设为NULL
node* root = NULL;

// 生成一个新节点, v为结点权值
node* newNode(int v) {
	node* Node = new node;
	Node->data = v;
	Node->lchild = Node->rchild = NULL;
	return Node;
}

//二叉树的查找修改
void search(node* root, int x, int newdata) {
	if (root == NULL) return ;
	if (root->val == x) root->val = newdata;
	search(root->lchild, x, newdata);
	search(root->rchild, x, newdata);
}

//二叉树结点的插入位置就是数据域在二叉树中查找失败的位置
// 这里使用&, 在函数中直接修改root原变量
void insert(node* &root, int x) {
	if (root == NULL) {
		root = newNode(x);
		return ;
	}
	if (由二叉树的性质, x应插入在左子树) {
		insert(root->lchild, x);
	} else {
		insert(root->rchild, x);
	}
}

// 二叉树的创建
node* Create(int data[], int n) {
	node* root = NULL;
	for (int i = 0; i < n; i ++) {
		insert(root, data[i]);
	}
	return root;
}

// 二叉树的遍历

// 先序遍历
void preorder(node* root) {
	void preorder(node* root) {
		
	}
}
// 中序遍历(先遍历根在遍历左右子树,因此可以通过根节点在中序遍历的位置区分出左子树和右子树)
void inorder(node* root) {
	if (root == NULL) return ;
	inorder(root->lchild);
	printf("%d\n", root->val);
	inorder(root->rchild);

}
// 后序遍历(序列最后一个元素为根节点)
void postorder(node* root) {
	if (root == NULL) return ;
	postorder(root->lchild);
	postorder(root->rchild);
	printf("%d\n", root->val);
}
// 层序遍历
// 1.将根节点root入队
// 2.取出队首元素, 访问它。
// 如果该结点有左孩子, 将左孩子入队
// 如果该结点有右孩子, 将右孩子入队
// 返回2,直至队列为空
void LayerOrder(node* root) {
	queue<node*> q;
	q.push(root);
	while (!q.empty()) {
		node* now = q.front();
		q.pop();
		printf("%d ", now->val);
		if (now->lchild != NULL) q.push(now->lchild);
		if (now->rchild != NULL) q.push(now->rchild);
	}
}

// 根据先序和中序构建一个二叉树
node* createPreIn(int preL, int preR, int inL, int inR) {
	if (preL > preR) return NULL;
	node* root = new node;
	root->val = pre[preL];
	int k;
	for (k = inL; k <= inR; k ++) // 找到根节点在中序遍历的位置
		if (in[k] == pre[preL]) break;
	
	int numLeft = k - inL; // 左子树结点
	// 左子树的区间在先序遍历序列为[preLeft+1, preLeft+numLeft], 中序区间为[inL, k - 1]
	root->lchild = createPreIn(preL + 1, preL + numLeft, inL, k - 1);
	// 右子树的区间在先序遍历序列为[preL + numLeft + 1, preR], 中序区间为[k + 1, inR]
	root->rchild = createPreIn(preL + numLeft + 1, preL, k + 1, inR);

	return root;
}

// 根据后序和中序构建一个二叉树
node* createPostIn(int PostL, int PostR, int inL, int inR) {
	if (PostL > PostR) return NULL;
	node* root = new node;
	root->val = post[PostR];
	int k;
	for (k = inL; k <= inR; k ++)
		if (in[k] == posst[PostR]) break;
	int numLeft = k - inL;

	root->lchild = createPostIn(PostL, PostL + numLeft - 1, inL, k - 1);
	root->rchild = createPostIn(PostL + numLeft, PostR - 1, k + 1, inR);
}
// 数组版本
struct node {
	typename data;
	int lchild;
	int rchild;
} Node[N];

int index = 0;
int newNode(int v) {
	Node[index].data = v;
	Node[index].lchild = -1;
	Node[index].rchild = -1;
	return index ++;
}

void search(int root, int x, int newdata) {
	if (root = -1) return;
	if (Node[root].data == x) Node[root].data = newdata;
	search(Node[root].lchild, x, newdata);
	search(Node[root].rchild, x, newdata);
}

void insert(int &root, int x) {
	if (root == -1) {
		root = newNode(x);
		return ;
	}
	if (由于二叉树的性质x应该插在左子树)
		insert(Node[root].lchild, x);
	else
		insert(Node[root].rchild, x);
}

int Create(int data[], int n) {
	int root = 1;
	for (int i = 0;i < n; i ++)
		insert(root, data[i]);
	return root;
}

void preorder(int root) {
	if (root == -1) return ;
	printf("%d ", Node[root].data);
	preorder(Node[root].lchild);
	preorder(Node[root].rchild);
}

void inorder(int root) {
	if (root == -1) return ;
	inorder(Node[root].lchild);
	printf("%d ", Node[root].data);
	inorder(Node[root].rchild);
}

void postorder(int root) {
	if (root == -1) return ;
	
	preorder(Node[root].lchild);
	preorder(Node[root].rchild);
	printf("%d ", Node[root].data);
}

void LayerOrder(int root) {
	queue<int> q;
	q.push(root);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		printf("%d ", Node[now].data);
		if (Node[now].lchild != -1) q.push(Node[now].lchild);
		if (Node[now].rchild != -1) q.push(Node[now].rchild);
	}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

二叉树的基本操作 的相关文章

  • 关于VR的历史及发展

    寒假我看了关于一些虚拟现实的东西 xff0c 并在网上查获了一些资料 xff0c 作出以下归纳总结 xff1a 虚拟现实 xff0c 无法绕开它的历史 xff0c 最早可以追溯到公元前427年的古希腊时代 xff0c 当时的哲学家柏拉图在提
  • linux安装jdk环境(多种方式)

    linux系统通用安装 通过tar gz压缩包安装 此方法适用于绝大部分的linux系统 下载tar gz的压缩包 xff0c 这里使用官网下载 进入 xff1a http www oracle com technetwork java j
  • 线程的生产者和消费者模式

    多个线程同时运行时 xff0c 会产生线程并发可使用同步操作确保数据的安全性 xff0c 如果需要各线程之间交互 xff0c 可是使用线程等待和唤醒模式 xff0c 在这里常用的等待唤醒中经典的模式为 生产者和消费者模式 生产者和消费者由两
  • 获取操作日志记录(springboot+AOP)

    下面是我在公司写操作日志记录的时候的代码使用的aop自定义注解的方式 xff0c 这里记录一下代码希望可以给大家带来一些帮助 xff0c 顺便自己也巩固一下 方便随时取用 Aop注解实现日志的话其实不难 xff0c 我认为只要理解下面三个点
  • ThinkPHP5 SQL注入(select方法)

    ThinkPHP5 SQL注入 xff08 select方法 xff09 漏洞概要初始配置漏洞利用漏洞分析漏洞修复攻击总结 漏洞概要 本次漏洞存在于 Mysql 类的 parseWhereItem 方法中 xff0c 由于程序没有对数据进行
  • Windows下通过PowerShell终端直接制作tar.gz压缩包

    因为工作需要 xff0c 笔者经常要在windows系统里上传多个文件到linux环境上 centos linux下默认支持tar gz压缩格式 xff0c 因此一般都是通过制作tar gz压缩包来传的 原来在Windows下制作tar g
  • 一日一技:Ocelot网关使用IdentityServer4认证

    概述 Ocelot是一个用 NET Core实现的开源API网关技术 IdentityServer4是一个基于OpenID Connect和OAuth2 0的针对ASP NET Core的框架 xff0c 以中间件的形式存在 OAuth是一
  • Android Studio模拟器启动后不停闪烁(未解决)

    问题描述 xff1a Android Studio模拟器启动后不停闪烁 解决方法 xff1a 右侧点击Device Manager打开设备管理 xff0c 点击修改标志 将Graphics 图样 换成Software 软件 xff0c 点击
  • 复杂网络建模的实现(哈工大深圳复杂网络建模课程Project)

    任务 xff1a 1 xff0c 三张不同的网络 xff1a 已知某人的名称 已知某人的家乡 已知某人的方言 分析这三者各自的网络性能 xff08 节点度数分布 平均最短路径长度 集聚系数 xff09 以及动态行为 xff08 在刻意攻击
  • 免登陆Oracle官网下载JDK

    Oracle官网下载JDK 方法一 免登录下载 进入官网 xff0c 选择需要下载的JDK版本 xff0c 这里以JDK8为例 点击下载 xff0c 勾选同意 在正常情况下 xff0c 点击 Download jdk 8u333 windo
  • linux rancher 清理docker容器磁盘空间

    目录说明 var lib docker containers xff1a 是 Docker 在 Linux 系统上默认存储容器信息的目录 在该目录下 xff0c 每个运行的 Docker 容器都有一个单独的目录 xff0c 以容器 ID 命
  • 如何在官网下载COCO数据集

    官网地址 xff1a https cocodataset org download 1 选择下载的数据 xff0c 右键 xff0c 获取下载地址 2 将 http 改为 https 示例获得的下载地址为 xff1a http images
  • 两个互相引用对象的垃圾回收

    部分转自 xff1a 深入理解java虚拟机 一书 判断对象是否存活 1 引用计数算法 给对象添加一个引用计数器 xff0c 每当有一个地方引用它时 xff0c 计数器值就加1 当引用失效时 xff0c 计数器值就减1 任何时刻计数器为0的
  • ssm整合时,通过jdbc.properties文件无法连接mysql问题

    最近在重温ssm框架 在搭建基础的项目进行单元测试时 xff0c 发现无法连接mysql数据库 通过各种查资料终于发现了原因 原始jdbc properties文件 由于username这个属性会被系统的username变量覆盖 xff0c
  • Mysql数据库之左连接left join 右连接right join 内连接inner join

    最近 xff0c 公司的用户达到了700 43 万 xff0c 意味着数据库已经达到700 43 万 xff0c 聊聊傻傻分不清的连接查询吧 xff01 前提 数据库中一共有三个表 class book phone 而且每个数据库表中都有1
  • VMware虚拟机软件,配置Linux Ubuntu操作系统环境,及安装问题总结大全

    文章目录 1 xff1a 前言2 xff1a 基本认识3 下载环境4 xff1a VM虚拟机的安装5 xff1a ubuntu的下载6 xff1a 把ubuntu安装在VM虚拟机上7 VMware Tools工具8 Source insig
  • linux常用命令(Beginner note)

    命令 ls 列出所有文件及文件夹 ls 路径 xff1a 列出所给路径下的所有文件及文件夹 选项 xff1a xff08 可组合使用 xff0c 也可简写组合形式 xff0c 例 xff1a alh xff0c 无先后顺序 xff09 a
  • 利用JS-SDK微信分享接口调用(后端.NET)

    一直都想研究一下JS SDK微信分享的接口调用 xff0c 由于最近工作需要 xff0c 研究了一下 xff0c 目前只是实现了部分接口的调用 xff1b 其他接口调用也是类似的 xff1b 在开发之前 xff0c 需要提前准备一个微信公众
  • Linux文件查找find

    1 find查找概述 为什么要有文件查找 xff0c 因为很多时候我们可能会忘了某个文件所在的位置 xff0c 此时就需要通过find来查找 find命令可以根据不同的条件来进行查找文件 xff0c 例如 xff1a 文件名称 文件大小 文

随机推荐

  • Linux文件打包与压缩

    1 文件打包与压缩 1 什么是文件压缩 将多个文件或目录合并成为一个特殊的文件 比如 搬家 脑补画面 img 2 为什么要对文件进行压缩 xff1f 当我们在传输大量的文件时 xff0c 通常都会选择将该文件进行压缩 xff0c 然后在进行
  • 集中式版本管理SVN与分布式版本管理Git的区别

    集中式版本控制系统SVN CVS 先说集中式版本控制系统 xff0c 版本库是集中存放在中央服务器的 xff0c 而大家工作的时候 xff0c 用的都是自己的电脑 xff0c 所以要先从中央服务器取得最新的版本 xff0c 然后开始工作 x
  • chatgpt Linux 定时任务 清理rancher pod启动服务的日志文件 脚本

    Linux 定时任务执行命令 假设我们想要每隔X分钟 每隔X天 每天X点执行一个脚本文件 xff0c 可以使用 Linux 自带的 cron 工具来创建定时任务 清理步骤 您可以使用 Linux 自带的 cron 工具来创建定时任务 xff
  • Http协议的几种常见状态码

    在开发好了网站后 xff0c 用户通过URL对资源进行操作 xff0c 服务器端要告诉用户交互的结果 xff0c 比如新增资源是成功还是失败了 一个较好的办法就是遵循HTTP协议 xff0c 使用请求响应的HTTP状态码 xff08 Sta
  • 推荐画UML图以及流程图的在线网站Site

    记得当年学UML课程的时候 xff0c 当你还在为了安装Rose而发愁的时候 xff0c 人家都把作业给交了 xff0c 并且现在大多数UML课程都会让学生使用Rational Rose做画图练习 近来 xff0c 做毕业设计需要提供各种流
  • 浙大PTA平台上的题目题解

    记载一些题目的代码 xff0c 之后想要在b站讲题目 xff0c 到时候会把录的视频上传b站 不是大佬 xff0c 是蒟蒻 xff0c 大佬勿喷 xff0c 仅供参考 xff0c 欢迎大家star xff0c qwq 浙大版 C语言程序设计
  • git 入门教程

    想要将一个项目托管到github xff0c 需要进入项目所在文件夹进行git init命令初始化 Git提交代码的基本流程 xff1a 创建或修改 本地文件使用 git add 命令 xff0c 将创建或修改的文件添加到本地的暂存区 xf
  • 博客搬家

    谢谢大家对我的blog的支持 现在本科毕业 xff0c 准备读研 方向大概是机器学习这一块 又是一个新的开始 我想将我读研学习Python以及机器学习 深度学习 以及数据分析处理 大数据等学习教程放在新的blog上 xff1a blog 欢
  • Python多线程笔记(Python_MultiThread)

    4 MultiThreading 多线程 使用 xff1a a 什么是多线程 xff1f 简单明了 xff0c 让计算机在同一时间内同时运行多个程序 xff0c 并且每个程序的计算互不干扰 xff0c 我们称这样的操作为多线程运算 b ad
  • Python多进程笔记(Python_MultiProcess)

    1 MutiProcessing 多进程 使用 xff1a a 什么是多进程 xff1f 在上面我们使用多线程去分别处理不同的事情 xff0c 看起来 xff0c 多线程处理并不比单线程循环处理的效率看起来那么的高 多进程是在利用我们电脑C
  • Python自带的GUI(Tkinter)教程

    1 Python Tkinter xff08 GUI图形界面 xff09 xff1a a What s Tkinter Tkinter 是什么 xff1f Tkinter是Python自带的一个GUI库 xff0c 他可以将我们在comma
  • Python科学计算包NumPy教程

    在我的Github上有一份代码与教程结合的jupyter Notebook文件 xff0c 大家可以clone下来看一看 下面会用实例的方式给出一些examples xff1a Tutorial教程 官方中文文档 span class to
  • 区块链入门的几个概念

    区块链入门的几个简单概念 1 What s Block Chain 区块链是一门软件技术 xff0c 从本质上来看就像是一个分布式的DataBase xff0c 是一个去中心化 xff0c 分布式技术 由于是分布式的 xff0c 所以区块链
  • Mysql GROUP_CONCAT与CONCAT_WS配合使用单选、多选拼接

    举例1 可以使用IF函数将单选和多选的值分别拼接 xff0c 并在最后的结果中使用CONCAT WS函数将它们合并 xff1a idcolors11223344 551 2 362 3 4 5 我们想要的结果为1 2 3 4 5 下面开始测
  • A Survey on Concept Drift Adaptation Note

    A Survey on Concept Drift Adaptation Abstract Concept drift primarily refers to an online supervised learning scenario w
  • Learning under Concept Drift:A Review

    Learning under Concept Drift A Review Abstract Concept drift describes unforeseeable changes in the underlying distribut
  • Note for Understanding Neural Networks Through Deep Visualization

    Note for Understanding Neural Networks Through Deep Visualization Abstract 近年来 xff0c 在训练大型深度神经网络方面取得了巨大进展 xff0c 其中包括训练卷积
  • ML-Leaks Note

    ML Leaks Model and Data IndependentMembership Inference Attacks and Defenses onMachine Learning Models xff08 机器学习模型上与模型和
  • C++中读取字符串的方式

    这里写自定义目录标题 C 43 43 读取字符串的两种方式1 getline 读取行的输入2 get 读取行的输入 C 43 43 读取字符串的两种方式 1 getline 读取行的输入 getline函数读取整行 xff0c 它使用通过回
  • 二叉树的基本操作

    二叉树 先序和中序确定二叉树 后序以及中序确定二叉树 span class token comment 指针版本 span span class token keyword struct span node span class token