forseize

顺序存储和链接存储的相互转化

顺序存储转化为链接存储:

BTreeNode create(char str, int pose, int size) //下标从0开始
{
char ch;
BTreeNode * t;
char* p=str;
ch = p[pose];

if(ch=='#'|| pose>=size) 
	return NULL; // 表示空结点
else 
{ 
	t=(BTreeNode *)malloc(sizeof(BTreeNode)); //非空则构造新结点
	t->data=ch; //新结点数据域即为读入字符
	t->lchild=create(p, 2*pose+1,size); //建立左子树
	t->rchild=create(p, 2*pose+2,size); //建立右子树
}
return(t);

}
链接存储转化为顺序存储:
char a[200];//顺序存储数组
int flag;//判断是否是空树
BiNode* Creat(BiNode bt,int k)
{
char ch;
cin>>ch;
if(ch=='#')
{
bt=NULL;
}
else
{
flag=1;
bt=new BiNode;
bt->data=ch;
a[k]=ch;
bt->lchild=Creat(bt->lchild,(k)
2);
bt->rchild=Creat(bt->rchild,(k)*2+1);
}
return bt;
}

posted on 2020-11-06 18:49  forseize  阅读(105)  评论(0编辑  收藏  举报

导航