Error[8]: Undefined offset: 64, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

上图所示的二叉树用顺序存储方式存为

[+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++]


以A结点为例:相当于一个一维数组啦

设A结点下标为i时

A的左子树下标为2*i +1,B就是 2*0+1 = 1

A的右子树下标为2*(i +1),C就是 2*(0+1) = 2

想必大家也发现了,这很容易造成内存的浪费

如上图只有A,B,D结点的话

[+++][+++][+++][+++][+++][+++][+++][+++]

下面是代码实现:

[+++]

#include 
#include 

//定义二叉树的最大结点数
#define MAX_SIZE 255

//下标为0的位置存储根结点
typedef char SeqBinTree[MAX_SIZE];//可以参考我写的文章 (c语言中的typedef) 

//初始化 
void InitTree(SeqBinTree tree);

//创建二叉树
void CreatSeqtree(SeqBinTree tree,int index);

//打印二叉树
void PrintSeqtree(SeqBinTree tree); 

int main(void) 
{
	SeqBinTree tree;
	InitTree(tree);
	CreatSeqtree(tree,0);
	PrintSeqtree(tree);
	return 0;
}

void InitTree(SeqBinTree tree)
{
	//将字符数组中的每个元素的设置为空
	 int i;
	 for(i=0;i

<===><===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 65, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

上图所示的二叉树用顺序存储方式存为

[+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++][+++]


以A结点为例:相当于一个一维数组啦

设A结点下标为i时

A的左子树下标为2*i +1,B就是 2*0+1 = 1

A的右子树下标为2*(i +1),C就是 2*(0+1) = 2

想必大家也发现了,这很容易造成内存的浪费

如上图只有A,B,D结点的话

[+++][+++][+++][+++][+++][+++][+++][+++]

下面是代码实现:

[+++]

#include 
#include 

//定义二叉树的最大结点数
#define MAX_SIZE 255

//下标为0的位置存储根结点
typedef char SeqBinTree[MAX_SIZE];//可以参考我写的文章 (c语言中的typedef) 

//初始化 
void InitTree(SeqBinTree tree);

//创建二叉树
void CreatSeqtree(SeqBinTree tree,int index);

//打印二叉树
void PrintSeqtree(SeqBinTree tree); 

int main(void) 
{
	SeqBinTree tree;
	InitTree(tree);
	CreatSeqtree(tree,0);
	PrintSeqtree(tree);
	return 0;
}

void InitTree(SeqBinTree tree)
{
	//将字符数组中的每个元素的设置为空
	 int i;
	 for(i=0;i

<===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
二叉树的顺序存储结构------_C_内存溢出

二叉树的顺序存储结构------

二叉树的顺序存储结构------,第1张

上图所示的二叉树用顺序存储方式存为

结点ABCDE0F
下标0123456


以A结点为例:相当于一个一维数组啦

设A结点下标为i时

A的左子树下标为2*i +1,B就是 2*0+1 = 1

A的右子树下标为2*(i +1),C就是 2*(0+1) = 2

想必大家也发现了,这很容易造成内存的浪费

如上图只有A,B,D结点的话

AB0D0000

下面是代码实现:

//定义二叉树的最大结点数
#define MAX_SIZE 255

//下标为0的位置存储根结点
typedef Element SqBinTree[MAX_SIZE];

SqBinTree tree;

#include 
#include 

//定义二叉树的最大结点数
#define MAX_SIZE 255

//下标为0的位置存储根结点
typedef char SeqBinTree[MAX_SIZE];//可以参考我写的文章 (c语言中的typedef) 

//初始化 
void InitTree(SeqBinTree tree);

//创建二叉树
void CreatSeqtree(SeqBinTree tree,int index);

//打印二叉树
void PrintSeqtree(SeqBinTree tree); 

int main(void) 
{
	SeqBinTree tree;
	InitTree(tree);
	CreatSeqtree(tree,0);
	PrintSeqtree(tree);
	return 0;
}

void InitTree(SeqBinTree tree)
{
	//将字符数组中的每个元素的设置为空
	 int i;
	 for(i=0;i

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/567734.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)