当前位置: > C++高手来看看:树的孩子兄弟表示法,这个程序里面的TXT文件该怎么建立啊!...
题目
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

提问时间:2021-03-20

答案
根据题意应该是两位数表示 一个结点信息(父、孩子),如开始为'#'则为首结点.
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的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.