From fc472b9ce1fb5f4e5feaa7d29872c364e5e3d490 Mon Sep 17 00:00:00 2001 From: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> Date: Sat, 13 Jan 2024 15:43:06 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=AD=A6=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> --- ...0\346\215\256\347\273\237\350\256\241.cpp" | 203 ++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 "2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" diff --git "a/2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" "b/2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" new file mode 100644 index 00000000..3d8e5bac --- /dev/null +++ "b/2224020152/\347\254\254\344\270\203\345\215\225\345\205\203/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" @@ -0,0 +1,203 @@ +#include +#include +#define MaxSize 66 +#include +#include +#define MaxSons 3 +typedef char ElemType; +typedef struct node +{ + ElemType data[15]; + struct node *sons[MaxSons]; +}TSonNode; +typedef struct +{ + char N[15]; + char n[15]; +}array; + +void ReadFile(array R[],FILE *fp,int &n) +{ + while((fscanf(fp,"%s",R[n].N))!=EOF&&(fscanf(fp,"%s",R[n].n))!=EOF) + n++; +} + +TSonNode *CreateTree(char str[],array R[],int n) +{ + TSonNode *t; + int k,i=0,j=0; + t=(TSonNode *)malloc(sizeof(TSonNode)); + strcpy(t->data,str); + for(k=0;ksons[k]=NULL; + while(isons[j]=CreateTree(R[i].n,R,n); + j++; + } + i++; + } + return t; +} + +void DispTree(TSonNode *t) +{ + int i=0; + if(t==NULL) + printf("此树为空树!\n"); + else + { + printf("%s",t->data); + if(t->sons[i]!=NULL) + { + printf("("); + for(i=0;isons[i]); + if(t->sons[i+1]!=NULL) + printf(","); + else + break; + } + printf(")"); + } + } +} + +void DestroyTree(TSonNode *t) +{ + if(t==NULL) + printf("此树为空树!\n"); + else + { + for(int i=0;isons[i]!=NULL) + DestroyTree(t->sons[i]); + else + break; + } + free(t); + } +} + +TSonNode *FindNode(TSonNode *t,char str[]) +{ + TSonNode *p; + if(t==NULL) + return NULL; + else + { + if(strcmp(t->data,str)==0) + return t; + else + { + for(int i=0;isons[i]!=NULL) + { + p=FindNode(t->sons[i],str); + if(p!=NULL) + return p; + } + } + return NULL; + } + } +} + +int ChildCount(TSonNode *p) +{ + int count=0; + for(int i=0;isons[i]!=NULL) + count++; + else + break; + } + return count; +} + +int LeafCount(TSonNode *p) +{ + int count=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + count++; + else + { + for(int i=0;isons[i]!=NULL) + count=count+LeafCount(p->sons[i]); + else + break; + } + } + } + return count; +} + +int LeafSumOfvalue(TSonNode *p) +{ + int sum=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + return atoi(p->data); + else + { + for(int i=0;isons[i]!=NULL) + sum+=LeafSumOfvalue(p->sons[i]); + else + break; + } + } + } + return sum; +} + + +int main() +{ + int n=0; + TSonNode *t; + array R[MaxSize]; + FILE *fp; + if((fp=fopen("abc1.txt","r"))==NULL) + { + printf("error!cannot open the file!"); + exit(1); + } + printf("读取文件内容存入数组R中\n"); + ReadFile(R,fp,n); + printf("输出数组R:\n"); + for(int i=0;i