나무 아이 형제 노드
2473 단어 노트
#include <iostream>
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;i<=MAXSIZE;i++)
{
if(q[i]->data == 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)
{
cout<<tree->data<<" ";
if(tree->firstchild!=NULL)
print_cstree(tree->firstchild);
if(tree->nextsibling!=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<<endl;
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다: