回 帖 发 新 帖 刷新版面

主题:[讨论]求教个问题

void disptree(BTree  BT)
    {
      BTree stack[MaxSize],p;
      int level[MaxSize][2],top,n,i,width=4;
      if (BT!=NULL)
      {
    printf("家谱凹入表示法:\n");
        top=1;
    stack[top]=BT;   /*根结点入栈*/
    level[top][0]=width;
    while (top>0)
        {
          p=stack[top];  /*退栈并凹入显示该结点值*/
      n=level[top][0];
      for (i=1;i<=n;i++)  /*其中n为显示场宽,字符以右对齐显示*/
          printf(" ");
      printf("%6s",p->name);
      for (i=n+1;i<=MaxWidth-6;i+=2)
          printf("-");
      printf("\n");
      top--;
      if (p->right!=NULL)
      {               /*将右子树根结点入栈*/
    top++;
    stack[top]=p->right;
    level[top][0]=n+width;    /*显示场宽增width*/
    level[top][1]=2;
      }
      if (p->left!=NULL)
      {               /*将左子树根结点入栈*/
    top++;
        stack[top]=p->left;
    level[top][0]=n+width;     /*显示场宽增width*/
    level[top][1]=1;
      }
    }
  }
}
  BTree findfather(BTree p,char xm[])      /*寻找指定的父亲结点*/
   {
  BTree bt;  if(p==null) return(null);
  else
  {
    bt=findfather(p->left,xm);
    if(bt!=null)  return(bt);
    else return(findfather(p->right,xm));
  }
}
BTree creatree()           /*建立家谱二叉树*/
{
  int contin=1,first=1;
  char xm[10];
  BTree btree,s,t,p;
  printf("\n Create a binary tree:\n");
  while(contin)
  {
   if (first==1)
   {
     btree=(BTree)malloc(sizeof(struct node));  /*生成结点btree 作为根结点*/
     printf("please input a name; \n");
     gets(btree->name);
     gets(btree->name);
     btree->right=null;
     s=(BTree)malloc(sizeof(struct node));     /*生成结点s作为根的妻子结点*/
     printf("please input the wife's name: ");
     gets(s->name);
     s->left=s->right=null;
     btree->left=s;                          /*s设置成btree的左孩子*/
     first=0;
    }
    else
    {
      printf("father: ");
      gets(xm);
      if (strcmp(xm," ")==0)
       contin=0;
      else
      {
    p=findfather(btree,xm);
        if(p!=null)
        {
          p=p->left;
          if(p==null)                             /*p没有妻子,所以没有孩子*/
            printf("there is no son!\n");
          else
          {
            while (p->right!=null)
             p=p->right;
            s=(BTree)malloc(sizeof(struct node));
            s->right=null;
            p->right=s;
            printf("\t son:");
            gets(s->name):
            printf("\t son's wife:");
            gets(xm);
            if(strcmp(xm,"")!=0)
            {
             t=(BTree)malloc(sizeof(struct node));
              strcpy(t->name,xm);
              t->left=t->right=null;
              s->left=t;
              else s->left=null;
              }
             }
             else printf("Not exist! \n");
            }
           }
          }
           return(btree);
         }
       void findson(BTree bt)       /*查找指定父亲的所有儿子*/
       {
        char xm[10];
        BTree p;
        printf("\n find sons:\n");
        gets(xm);
        gets(xm);
        p=findfather(bt,xm);
        if (p==null)
          printf("%s is not exist!\n",xm);
        else
        {
         p=p->left;
         p=p->right;
         if(p==null)
          printf("%s has no son.\n",xm);
         else
         {
           printf("here are the sons of %s: \n");
           while(p!=null)
           {
             printf("%8s",p->name);
             p=p->right;}
         }
        }
       }
 怎么该才能运行啊    在Win-tc里运行
然后再修改
我改不出来哦

回复列表 (共1个回复)

沙发

怎么没有主函数啊

我来回复

您尚未登录,请登录后再回复。点此登录或注册