题目
C++高手来看看:树的孩子兄弟表示法,这个程序里面的TXT文件该怎么建立啊!
#include
using namespace std;
typedef struct CSNode
{
char data;
struct CSNode * firstchild ,* nextsibling ;
}* CSTree;
//
#define MAXSIZE 10
CSTree q[MAXSIZE];
int count=0;
//初始化
void init_cstree(CSTree &tree)
{
tree->firstchild = NULL;
tree->nextsibling = NULL;
}
//创建树
void creat_cstree(CSTree &T)
{
FILE *fin=fopen("树的孩子兄弟表示法.txt","r");
char fa=' ',ch=' ';
for( fscanf(fin,"%c%c",&fa,&ch); ch!='#'; fscanf(fin,"%c%c",&fa,&ch) )
{
CSTree p=(CSTree)malloc(sizeof(CSTree));
init_cstree(p);
p->data=ch;
q[++count]=p;
if('#' == fa)
T=p;
else
{
CSTree s = (CSTree)malloc(sizeof(CSTree));
int i;
for(i=1;idata == fa)
{
s=q[i];
break;
}
}
if(!(s->firstchild) ) //如果该双亲结点还没有接孩子节点
s->firstchild=p;
else //如果该双亲结点已经接了孩子节点
{
CSTree temp=s->firstchild;
while(NULL = temp->nextsibling)
{
temp=temp->nextsibling;
}
temp->nextsibling=p;
}
}
}
fclose(fin);
}
//前序遍历
void print_cstree(CSTree &tree)
{
coutnextsibling!=NULL)
print_cstree(tree->nextsibling);
}
//输出树中所有从根到叶子结点的路径的算法
//void allpath_tree()
int main()
{
CSTree cstree;
cstree=(CSTree)malloc(sizeof(CSTree));
init_cstree(cstree);
creat_cstree(cstree);
//输出树
print_cstree(cstree);
cout
#include
using namespace std;
typedef struct CSNode
{
char data;
struct CSNode * firstchild ,* nextsibling ;
}* CSTree;
//
#define MAXSIZE 10
CSTree q[MAXSIZE];
int count=0;
//初始化
void init_cstree(CSTree &tree)
{
tree->firstchild = NULL;
tree->nextsibling = NULL;
}
//创建树
void creat_cstree(CSTree &T)
{
FILE *fin=fopen("树的孩子兄弟表示法.txt","r");
char fa=' ',ch=' ';
for( fscanf(fin,"%c%c",&fa,&ch); ch!='#'; fscanf(fin,"%c%c",&fa,&ch) )
{
CSTree p=(CSTree)malloc(sizeof(CSTree));
init_cstree(p);
p->data=ch;
q[++count]=p;
if('#' == fa)
T=p;
else
{
CSTree s = (CSTree)malloc(sizeof(CSTree));
int i;
for(i=1;idata == fa)
{
s=q[i];
break;
}
}
if(!(s->firstchild) ) //如果该双亲结点还没有接孩子节点
s->firstchild=p;
else //如果该双亲结点已经接了孩子节点
{
CSTree temp=s->firstchild;
while(NULL = temp->nextsibling)
{
temp=temp->nextsibling;
}
temp->nextsibling=p;
}
}
}
fclose(fin);
}
//前序遍历
void print_cstree(CSTree &tree)
{
coutnextsibling!=NULL)
print_cstree(tree->nextsibling);
}
//输出树中所有从根到叶子结点的路径的算法
//void allpath_tree()
int main()
{
CSTree cstree;
cstree=(CSTree)malloc(sizeof(CSTree));
init_cstree(cstree);
creat_cstree(cstree);
//输出树
print_cstree(cstree);
cout
提问时间:2021-03-20
答案
根据题意应该是两位数表示 一个结点信息(父、孩子),如开始为'#'则为首结点.
r孩子abc
a孩子de
b孩子无
c孩子f
f孩子ghk
表示内容如下:
文件名:树的孩子兄弟表示法.txt
#rrarbrcadaecffgfhfk##
r孩子abc
a孩子de
b孩子无
c孩子f
f孩子ghk
表示内容如下:
文件名:树的孩子兄弟表示法.txt
#rrarbrcadaecffgfhfk##
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
热门考点
- 1世界上最早的字典叫什么名?
- 2如何配制0.1mol/l的蔗糖溶液10ml
- 328 父亲的菜园
- 4化合价中是-1价高还是-2价高
- 5填介词She said she had a really hard time ___science this semester.为什么?
- 6一艘船从A顺流开到B需6小时,从B逆流开到A需8小时.一天,船从早晨6点从A开到B后,发现有一个救生圈在途中掉到了水里,便立刻返回,一个小时后找到救生圈.请问:能否求出救生圈掉入水中的时间,若不能,请
- 7如图所示,光滑水平面上放置质量分别为m和2m的四个木块,其中两个质量为m的木块间用一不可伸长的轻绳相连,木块间的最大静摩擦力是μmg.现用水平拉力F拉其中一个质量为2m的木块,使四
- 8雨前茶和雨后茶的区别
- 9苯甲醛和乙醛分别在什么条件下发生羟醛缩合和分子间脱水
- 10东经170度,北纬40度,是什么半球