qq_53318532 2021-06-09 23:54 采纳率: 50%
浏览 25
已结题

由数组R创建 注释创建一棵树这一块不懂 可以详细的解释一下完整的代码 还有所有形参也不懂 n是什么

//头文件
//tree.h
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#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;
//读取文件内容到数组R中
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;k<MaxSons;k++)
      t->sons[k]=NULL;
    while(i<n)
    {
       if(strcmp(R[i].N,str)==0)
       {
           t->sons[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)    //若t结点至少有一个孩子
        {
           printf("(");
           for(i=0;i<MaxSons;i++)
           {
               DispTree(t->sons[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;i<MaxSons;i++)
        {
            if(t->sons[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;i<MaxSons;i++)
            {
                if(t->sons[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;i<MaxSons;i++)
    {
        if(p->sons[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;i<MaxSons;i++)
            {
                if(p->sons[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;i<MaxSons;i++)
            {
                if(p->sons[i]!=NULL)
                   sum+=LeafSumOfvalue(p->sons[i]);
                else
                    break;
            }
        }
    }
    return sum;
}

//源文件
//exp7.cpp
#include<stdio.h>
#include<stdlib.h>
#include"tree.h"
#define MaxSize 66
int main()
{
    int n=0;
    TSonNode *t;
    array R[MaxSize];
    FILE *fp;
    if((fp=fopen("table.txt","r"))==NULL)      //以只读方式打开table.txt文件
    {
        printf("error!cannot open the file!");
        exit(1);
    }
    printf("读取文件内容存入数组R中\n");
    ReadFile(R,fp,n);
    printf("输出数组R:\n");
    for(int i=0;i<n;i++)
        printf("%s %s\n",R[i].N,R[i].n);  //输出R数组查看是否读取正确
    printf("\n由数组R创建树T,");
    t=CreateTree(R[0].N,R,n);   //创建一颗树
    printf("由括号表示输出树T:\n");
    DispTree(t);
    char str[10];
    printf("\n请输入学院名:");
    scanf("%s",str);
    printf("\n%s的专业数:%d\n",str,ChildCount(FindNode(t,str)));
    printf("%s的班级数:%d\n",str,LeafCount(FindNode(t,str)));
    printf("\n请输入学院名:");
    scanf("%s",str);
    printf("\n%s的学生数:%d\n",str,LeafSumOfvalue(FindNode(t,str)));
    printf("销毁树!\n");
    DestroyTree(t);
    return 0;
}
 

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-06-10 04:47
    关注

    n是数组R的大小啊。

    函数注释不是比较清楚么。参数主要是这棵树。创建的时候是用于形成数的数组内容,删除的时候指定要删除的数据,在树中找到数据进行删除

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用