• 代码没有报错但是运行的时候没反应是怎么回事


    求指教
    代码没有报错,但是运行的时候就打印了一段文字,之后怎么输入都没有反应了……

    以下是代码要实现的功能:

    img

    以下是我的代码:

    #include 
    #include 
    
    #define OVERFLOW 0
    #define MAXSIZE 100
    
    typedef struct BiTNode
    {
        char data;
        struct BiTNode *lchild,*rchild;
    }BiTNode,*BiTree;
    
    void CreateBT(BiTree &T)
    {
        char c; 
        c=getchar();
            if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
                exit(OVERFLOW);
            T->data=c;
            if(c=='#')
            goto L1;
            if(!(T->lchild=(BiTNode*)malloc(sizeof(BiTNode))))
                exit(OVERFLOW);
            CreateBT(T->lchild);
            if(!(T->rchild=(BiTNode*)malloc(sizeof(BiTNode))))
                exit(OVERFLOW);
            CreateBT(T->rchild);
            L1:;
    }
    
    void ExchangeBT(BiTree T)
    {
        BiTree t=NULL;
        t=T->lchild;
        T->lchild=T->rchild;
        T->rchild=t;
        ExchangeBT(T->lchild);
        ExchangeBT(T->rchild);
    }
    
    BiTree SearchBT(BiTree T,char e)
    {
        BiTree t;
        t->data=NULL;
        t->lchild=NULL;
        t->rchild=NULL;
        if(T->data==e)
            return T;
        else if(T->data=='#')
        {
            printf("您查找的结点不存在");
            return t; 
        }
        else
        {
            SearchBT(T->lchild,e);
            SearchBT(T->rchild,e);
        }
    }
    void CountBT(BiTree T,int &d0,int &d1,int &d2)
    {
        if(T->data!='*')
        {   if(T->lchild->data=='*'&&T->rchild->data=='*')
                d0++;
            else if(T->lchild->data!='*'&&T->rchild->data!='*')
            {
                d2++;
                CountBT(T->lchild,d0,d1,d2);
                CountBT(T->rchild,d0,d1,d2);
            }
            else
                d1++;
                if(T->lchild->data=='*')
                    CountBT(T->rchild,d0,d1,d2);
                else
                    CountBT(T->lchild,d0,d1,d2);
        }
    }
    
    void DispBT(BiTree T,int level)
    {
        int i;
        i=level+1;
        DispBT(T->rchild,i);
        while(level!=0)
        {
            printf("*");
            level--;
        }
        printf("%c",T->data);
        DispBT(T->lchild,i);
    }
    
    int main()
    {
        int d0,d1,d2,m;
        char n;
        d0=0;
        d1=0;
        d2=0;
        m=0;
        BiTree T=NULL;
        BiTree t=NULL;
        printf("请按照先序遍历的顺序输入字符串(以“#”结尾):");
        CreateBT(T);
        DispBT(T,m);
        ExchangeBT(T);
        DispBT(T,m);
        printf("请输入您要查找的结点的数据域值:");
        scanf("%c",&n);
        t=SearchBT(T,n);
        if(t->data!=NULL)
        {
            CountBT(t,d0,d1,d2);
            printf("该结点的子树中叶子结点数为%d,度为1的结点数为%d,度为2的结点数为%d",d0,d1,d2);
        }
        return 0;
    
    

    展开全部

    • 嵌入式小企鹅 2024-10-21 16:22
      关注
      //在你的基础上更改的,你自己再试一下
      #include 
      #include 
      
      #define OVERFLOW 0
      
      typedef struct BiTNode {
          char data;
          struct BiTNode *lchild, *rchild;
      } BiTNode, *BiTree;
      
      void CreateBT(BiTree *T) {
          char c;
          scanf(" %c", &c); // 加空格跳过空白字符
          if (c == '#') {
              *T = NULL;
          } else {
              *T = (BiTNode *)malloc(sizeof(BiTNode));
              if (!(*T)) exit(OVERFLOW);
              (*T)->data = c;
              CreateBT(&(*T)->lchild);
              CreateBT(&(*T)->rchild);
          }
      }
      
      void ExchangeBT(BiTree T) {
          if (T) {
              BiTree t = T->lchild;
              T->lchild = T->rchild;
              T->rchild = t;
              ExchangeBT(T->lchild);
              ExchangeBT(T->rchild);
          }
      }
      
      BiTree SearchBT(BiTree T, char e) {
          if (T == NULL) return NULL;
          if (T->data == e) return T;
          BiTree found = SearchBT(T->lchild, e);
          if (found) return found;
          return SearchBT(T->rchild, e);
      }
      
      void CountBT(BiTree T, int *d0, int *d1, int *d2) {
          if (T == NULL) return;
          if (T->data == '#') {
              (*d0)++;
              return;
          }
          int left = (T->lchild != NULL && T->lchild->data != '#');
          int right = (T->rchild != NULL && T->rchild->data != '#');
          if (left && !right) {
              (*d1)++;
          } else if (!left && right) {
              (*d1)++;
          } else if (!left && !right) {
              (*d0)++;
          } else {
              (*d2)++;
          }
          CountBT(T->lchild, d0, d1, d2);
          CountBT(T->rchild, d0, d1, d2);
      }
      
      void DispBT(BiTree T, int level) {
          if (T) {
              DispBT(T->rchild, level + 1);
              for (int i = 0; i < level; i++) printf(" ");
              printf("%c", T->data);
              DispBT(T->lchild, level + 1);
          }
      }
      
      int main() {
          int d0 = 0, d1 = 0, d2 = 0;
          char n;
          BiTree T = NULL;
          printf("请按照先序遍历的顺序输入字符串(以“#”结尾):");
          CreateBT(&T);
          printf("原始二叉树:\n");
          DispBT(T, 0);
          printf("\n交换后的二叉树:\n");
          ExchangeBT(T);
          DispBT(T, 0);
          printf("\n请输入您要查找的结点的数据域值:");
          scanf(" %c", &n); // 加空格跳过空白字符
          BiTree t = SearchBT(T, n);
          if (t != NULL && t->data != '#') {
              printf("找到节点 %c\n", n);
              CountBT(t, &d0, &d1, &d2);
              printf("该结点的子树中叶子结点数为%d,度为1的结点数为%d,度为2的结点数为%d\n", d0, d1, d2);
          } else {
              printf("未找到节点 %c 或节点为叶子节点\n", n);
          }
          return 0;
      }
      

      展开全部

    • 相关阅读:
      III.二分算法
      vue3 + Element-plus + Echarts 5.2 切换不更新、导出PDF不显示 解决方案
      Image (mathematics)
      ElementUI之CUD+表单验证
      ASEMI整流桥S25VB100,S25VB100参数,S25VB100应用
      Leetcode 1431. Kids With the Greatest Number of Candies
      Java IDEA java.lang.IllegalStateException: Failed to introspect Class报错原因和解决办法
      SSm+Java+VUE基于微信小程序的学习资料销售平台#毕业设计
      网络编程-套接字相关基础知识
      CenterFusion: Center-based Radar and Camera Fusion for 3D Object Detection 解读
    • 原文地址:https://ask.csdn.net/questions/8154241