C++高手来看看:树的孩子兄弟表示法,这个程序里面的TXT文件该怎么建立啊!#include using namespace std;typedef struct CSNode{char data;struct CSNode * firstchild ,* nextsibling ;}* CSTree;//#define MAXSIZE 10CSTree q[MAXSIZE];
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/28 09:39:19
![C++高手来看看:树的孩子兄弟表示法,这个程序里面的TXT文件该怎么建立啊!#include using namespace std;typedef struct CSNode{char data;struct CSNode * firstchild ,* nextsibling ;}* CSTree;//#define MAXSIZE 10CSTree q[MAXSIZE];](/uploads/image/z/7146826-34-6.jpg?t=C%2B%2B%E9%AB%98%E6%89%8B%E6%9D%A5%E7%9C%8B%E7%9C%8B%EF%BC%9A%E6%A0%91%E7%9A%84%E5%AD%A9%E5%AD%90%E5%85%84%E5%BC%9F%E8%A1%A8%E7%A4%BA%E6%B3%95%2C%E8%BF%99%E4%B8%AA%E7%A8%8B%E5%BA%8F%E9%87%8C%E9%9D%A2%E7%9A%84TXT%E6%96%87%E4%BB%B6%E8%AF%A5%E6%80%8E%E4%B9%88%E5%BB%BA%E7%AB%8B%E5%95%8A%21%23include+using+namespace+std%3Btypedef+struct+CSNode%7Bchar+data%3Bstruct+CSNode+%2A+firstchild+%2C%2A+nextsibling+%3B%7D%2A+CSTree%3B%2F%2F%23define+MAXSIZE+10CSTree+q%5BMAXSIZE%5D%3B)
C++高手来看看:树的孩子兄弟表示法,这个程序里面的TXT文件该怎么建立啊!#include using namespace std;typedef struct CSNode{char data;struct CSNode * firstchild ,* nextsibling ;}* CSTree;//#define MAXSIZE 10CSTree q[MAXSIZE];
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
C++高手来看看:树的孩子兄弟表示法,这个程序里面的TXT文件该怎么建立啊!#include using namespace std;typedef struct CSNode{char data;struct CSNode * firstchild ,* nextsibling ;}* CSTree;//#define MAXSIZE 10CSTree q[MAXSIZE];
根据题意应该是两位数表示 一个结点信息(父、孩子),如开始为'#'则为首结点.
r孩子abc
a孩子de
b孩子无
c孩子f
f孩子ghk
表示内容如下:
文件名:树的孩子兄弟表示法.txt
#rrarbrcadaecffgfhfk##