技术标签: 二叉树 遍历二叉树 顺序二叉树 链式二叉树 数据结构
const int MAX_TREE_SIZE = 100;
typedef int SeqBTree[MAX_TREE_SIZE + 1];//0位置存储结点个数
数组依次递增对应按层构建二叉树
凹入表示法输出
void PrintSeqBTree(const SeqBTree t,int i)//凹入表示法打印以i为根节点的完全二叉树
{
int k = log(t[0]) / log(2) + 1;//层数
if (i > t[0]) return;
int m = log(i) / log(2);
while (m--) cout << ' ';
cout << t[i];
m = k - (int)(log(i) / log(2) + 1);
while (m--) cout << '-';
cout << endl;
PrintSeqBTree(t, i * 2);
PrintSeqBTree(t, i * 2 + 1);
}
#include<iostream>
#include<math.h>
using namespace std;
const int MAX_TREE_SIZE = 100;
typedef int SeqBTree[MAX_TREE_SIZE + 1];//0位置存储结点个数
void PrintSeqBTree(const SeqBTree t,int i)//凹入表示法打印以i为根节点的完全二叉树
{
int k = log(t[0]) / log(2) + 1;//层数
if (i > t[0]) return;
int m = log(i) / log(2);
while (m--) cout << ' ';
cout << t[i];
m = k - (int)(log(i) / log(2) + 1);
while (m--) cout << '-';
cout << endl;
PrintSeqBTree(t, i * 2);
PrintSeqBTree(t, i * 2 + 1);
}
int main()
{
int i;
SeqBTree bt;
cout << "请输入二叉树的结点数:";
cin >> bt[0];
cout << "请依次输入二叉树的各个结点:" << endl;
for (i = 1; i <= bt[0]; i++) cin >> bt[i];
PrintSeqBTree(bt,1);
return 0;
}
typedef struct BTNode {
char data;
BTNode* lChild;
BTNode* rChild;
}BTNode, * BTTree;
BTTree CreatBTTreeByPreAndInfixOrder(char* pre, char* in,int n)//根据先序和中序遍历序列建立二叉链表
{
if (n <= 0) return NULL;
int i;
BTTree t = (BTTree)malloc(sizeof(BTNode));
t->data = pre[0];
for (i = 0; in[i] != pre[0]; i++);
t->lChild = CreatBTTreeByPreAndInfixOrder(pre + 1, in, i);
t->rChild = CreatBTTreeByPreAndInfixOrder(pre + i + 1, in + i + 1, n - i - 1);
return t;
}
BTTree CreatBTTreeByInfixAndPostOrder(char* in, char* post, int n)//根据中序和后序遍历序列建立二叉链表
{
if (n <= 0) return NULL;
int i;
BTTree t = (BTTree)malloc(sizeof(BTNode));
t->data = post[n - 1];
for (i = 0; in[i] != post[n - 1]; i++);
t->lChild = CreatBTTreeByInfixAndPostOrder(in, post, i);
t->rChild = CreatBTTreeByInfixAndPostOrder(in + i + 1, post + i, n - i - 1);
return t;
}
void PrintBTTreeByPreorder(BTTree t)//打印先序遍历二叉链表
{
if (t)
{
cout << t->data;
PrintBTTreeByPreorder(t->lChild);
PrintBTTreeByPreorder(t->rChild);
}
}
void PrintBTTreeByInfixorder(BTTree t)//打印中序遍历二叉链表
{
if (t)
{
PrintBTTreeByInfixorder(t->lChild);
cout << t->data;
PrintBTTreeByInfixorder(t->rChild);
}
}
void PrintBTTreeByPostorder(BTTree t)//打印后序遍历二叉链表
{
if (t)
{
PrintBTTreeByPostorder(t->lChild);
PrintBTTreeByPostorder(t->rChild);
cout << t->data;
}
}
#include<iostream>
#include<stdlib.h>
#include<String.h>
using namespace std;
typedef struct BTNode {
char data;
BTNode* lChild;
BTNode* rChild;
}BTNode, * BTTree;
void PrintBTTreeByPreorder(BTTree t)//打印先序遍历二叉链表
{
if (t)
{
cout << t->data;
PrintBTTreeByPreorder(t->lChild);
PrintBTTreeByPreorder(t->rChild);
}
}
void PrintBTTreeByInfixorder(BTTree t)//打印中序遍历二叉链表
{
if (t)
{
PrintBTTreeByInfixorder(t->lChild);
cout << t->data;
PrintBTTreeByInfixorder(t->rChild);
}
}
void PrintBTTreeByPostorder(BTTree t)//打印后序遍历二叉链表
{
if (t)
{
PrintBTTreeByPostorder(t->lChild);
PrintBTTreeByPostorder(t->rChild);
cout << t->data;
}
}
BTTree CreatBTTreeByPreAndInfixOrder(char* pre, char* in,int n)//根据先序和中序遍历序列建立二叉链表
{
if (n <= 0) return NULL;
int i;
BTTree t = (BTTree)malloc(sizeof(BTNode));
t->data = pre[0];
for (i = 0; in[i] != pre[0]; i++);
t->lChild = CreatBTTreeByPreAndInfixOrder(pre + 1, in, i);
t->rChild = CreatBTTreeByPreAndInfixOrder(pre + i + 1, in + i + 1, n - i - 1);
return t;
}
BTTree CreatBTTreeByInfixAndPostOrder(char* in, char* post, int n)//根据中序和后序遍历序列建立二叉链表
{
if (n <= 0) return NULL;
int i;
BTTree t = (BTTree)malloc(sizeof(BTNode));
t->data = post[n - 1];
for (i = 0; in[i] != post[n - 1]; i++);
t->lChild = CreatBTTreeByInfixAndPostOrder(in, post, i);
t->rChild = CreatBTTreeByInfixAndPostOrder(in + i + 1, post + i, n - i - 1);
return t;
}
int main()
{
char s1[20] = "BFDGAC", s2[20] = "FGDBCA";
BTTree t = CreatBTTreeByInfixAndPostOrder(s1, s2, 6);
PrintBTTreeByPreorder(t);
return 0;
}
文章浏览阅读692次,点赞2次,收藏3次。规划采购管理是记录项目采购决策、明确采购方法,及识别潜在卖方的过程。本过程的主要作用 是,确定是否从项目外部获取货物和服务,如果是,则还要确定将在什么时间、以什么方式获取什 么货物和服务。货物和服务可从执行组织的其他部门采购,或者从外部渠道采购。本过程仅开展一 次或仅在项目的预定义点开展。图 12-2 描述本过程的输入、工具与技术和输出。图 12-3 是本过程的 数据流向图。..._pmp项目管理实施
文章浏览阅读5.7k次,点赞4次,收藏15次。linux bash脚本Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com The date command is found in the Bash shell, which is the default shell in most Linux distributions and..._linux terminal显示时间
文章浏览阅读3.6k次。npm报错处理_npm安装报错
文章浏览阅读2.5k次。问题本文主要针对的问题是在Unity中对Button类进行Onclick事件绑定的时候出现的函数参数错误进行分析解决,具体例子如下: Button[] button = GetComponentsInChildren<Button>(); int buttonCnt = 3; for (int i = 0; i < buttonCnt; i++) { button[i].SetActive(true); Debug.Log("i: " + i);_unity 按钮onclick参数类型
文章浏览阅读6.5k次,点赞5次,收藏43次。Halcon矩阵(Matrix)详细说明创建(Creation)create_matrixcopy_matrixrepeat_matrix访问(Access)算法(Arithmetic)分解(Decomposition)特征值(Eigenvalues)特性(Features)文件操作(File)新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注_get_full_matrix
文章浏览阅读2.5k次。欧氏距离(Euclidean Distance)欧式距离是最经典的一种距离算法,适用于求解两点之间直线的距离,适用于各个向量标准统一的情况,如各种药品的使用量、商品的售销量等。 欧氏距离也是最易于理解的一种距离计算方法,源自欧氏空间中两点间的距离公式。 二维空间上两点a(x1,y1)a(x_1,y_1)与b(x2,y2)b(x_2,y_2)之间的欧式距离: d12=(x1−x2)2+(y1−y_两条线之间的欧式距离怎么算
文章浏览阅读337次。TPM(Trusted Platform Module)安全芯片,是指符合TPM(可信赖平台模块)标准的安全芯片。标准由TCG(可信赖计算组织,Trusted Computing Group)提出,目前最新版本为2.0。符合TPM的芯片首先必须具有产生加解密密钥的功能,此外还必须能够进行高速的资料加密和解密,以及充当保护BIOS和操作系统不被修改的辅助处理器。TPM的可信基础来源于可信根,可信根(..._tpm 总线监听
文章浏览阅读6.7w次。也就更容易获得更高的浏览量。,我们称为午高峰,这个时间段主要是针对一二线城市的上班族,因为玩抖音的一二线城市的人比较多,所以这个时间段他们基本都是下班的时间段,刷抖音的人也很多。,我们成为晚高峰,这个时间段的人基本都忙完工作在休息了,这个时间段可以说是一天中抖音流量最高的时间段,是高峰中的高峰。,这一个时间段的人大都是刚刚睡醒,躺在床上刷一刷抖音,或者在上班的路上没事看看抖音,坐公交的路上刷着抖音。,我们称之为午高峰,这个时间段是人们的午休时间,这个时间段刷抖音的人也很多,吃完饭午休,拿着手机刷刷抖音。_几点发抖音浏览量最高
文章浏览阅读6.1k次,点赞7次,收藏41次。图数据可视化_R语言ggplot2包和tidybayes包绘制小提琴图进阶概述:绘制小提琴图时按数据分布的密度填充不同透明度的颜色(渐变填充)。使用工具:R语言中的ggplot2和tidybayes工具包本文使用的数据及计算方式与之前的博文一致:数据可视化——R语言ggplot2包绘制精美的小提琴图(并箱线图或误差条图组合)。本文采用tidybayes包中stat_eye()绘制小提琴图,通过设置aes(alpha = stat(f)可实现渐变填充。由于stat_eye()会默认采用中位数及分位数作_分半小提琴图
文章浏览阅读1.4k次。江苏省专转本计算机_计算机硬件系统是执行软件程序的物质基础,其中能执行程序指令的是( )
文章浏览阅读4.4k次。在Robocode中,坦克分为3个部件: 身体(Body)、炮塔(Gun)、雷达(Radar)。 因此,在Robot类(还记得吗,它是任何坦克的父类)中,有对这些部件操作的方法。要查看Robocode提供的API,可以在robocode目录下的javadoc下找到,也可以在Robocode程序的帮助菜单中找到: 对于Body来说,Robot类提供了4个方法:_robocode炮和机身的运动分离
文章浏览阅读77次。Problem DescriptionA number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first...