回 帖 发 新 帖 刷新版面

主题:求救--关于二叉树的程序有点问题帮忙一下

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define flag 0
typedef struct Tnode
{
    char data;
    struct Tnode *Lchild;
    struct Tnode *Rchild;
}Btree;
Btree *Great_Btree()
{
    char x;
    Btree *T;
    
    scanf("%c",&x);getchar();
    if(x==flag)
        T=NULL;
    else
    {
        T=(Btree*)malloc(sizeof(Btree));
        T->data=x;
        printf("\n请输入%c结点的左小孩:",T->data);
        T->Lchild=Great_Btree();
        printf("\n请输入%c结点的右小孩:",T->data);
        T->Rchild=Great_Btree();
    }
    return T;
}
void Preorder_Btree(Btree *T)
{
    if(T==NULL)
        return;
    else
    {
        printf("%c ",T->data);
        if(T->Lchild!=NULL)
            Preorder_Btree(T->Lchild);
        if(T->Rchild!=NULL)
            Preorder_Btree(T->Rchild);
    }
}
void Inorder_Btree(Btree *T)
{
    if(T==NULL)
        return;
    else
    {
        if(T->Lchild!=NULL)
            Inorder_Btree(T->Lchild);
        printf("%d ",T->data);
        if(T->Rchild!=NULL)
            Inorder_Btree(T->Rchild);
    }
}
void Postorder_Btree(Btree *T)
{
    if(T==NULL)
        return;
    else
    {
        if(T->Lchild!=NULL)
            Postorder_Btree(T->Lchild);
        if(T->Rchild!=NULL)
            Postorder_Btree(T->Rchild);
        printf("%c ",T->data);
    }
}
int height_Btree(Btree *T)
{
    int lh,rh;
    if(T==NULL)
        return 0;
    else
    {
        lh=height_Btree(T->Lchild);
        rh=height_Btree(T->Rchild);
    }
    if(lh>rh)
        return lh+1;
    else
        return rh+1;
}
int num=0;
void leavenum_Btree(Btree *T)
{
    if(T==NULL)
        return ;
    else
    {
      if(T->Lchild==NULL&&T->Rchild==NULL)
          num++;
      leavenum_Btree(T->Lchild);
      leavenum_Btree(T->Rchild);
    }

}
void main()
{
    Btree *T;
    int k,flag1;
    do{
        printf("\n\n\n");
        printf("\t\t\t        二叉树子系统\n");
        printf("\t\t\t******************************\n");
        printf("\t\t\t**    1--建  二  叉 树      **\n");
        printf("\t\t\t**    2--前序遍历二叉树     **\n");
        printf("\t\t\t**    3--中序遍历二叉树     **\n");
        printf("\t\t\t**    4--后序遍历二叉树     **\n");
        printf("\t\t\t**    5--求二叉树的高度     **\n");
        printf("\t\t\t**    6--求二叉树叶子数     **\n");
        printf("\t\t\t**    0--返          回     **\n");
        printf("\t\t\t******************************\n");
        printf("\t\t\t   请输入菜单(0-6):");
        scanf("%d",&k);
        switch(k)
        {
        case 1:
            printf("\n请输入树的根结点:");
            T=Great_Btree();
            printf("\n树已建立! 请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 2:
            printf("\n前序遍历二叉树得的结果为:\n");
            Preorder_Btree(T);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 3:
            printf("\n中序遍历二叉树得的结果为:\n");
            Inorder_Btree(T);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 4:
            printf("\n后序遍历二叉树得的结果为:\n");
            Preorder_Btree(T);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 5:
            flag1=height_Btree(T);
            printf("\n该二叉树的高度为:%d",flag1);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 6:
            leavenum_Btree(T);
            printf("\n该二叉树的叶子数为:%d",num);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        }
    }while(k!=0);
}

其中在菜单建二叉树的时候,问题就出来了,
Btree *Great_Btree()
{
    char x;
    Btree *T;
    
    scanf("%c",&x);getchar();
    if(x==flag)
        T=NULL;
    else
    {
        T=(Btree*)malloc(sizeof(Btree));
        T->data=x;
        printf("\n请输入%c结点的左小孩:",T->data);
        T->Lchild=Great_Btree();
        printf("\n请输入%c结点的右小孩:",T->data);
        T->Rchild=Great_Btree();
    }
    return T;
}
我想是这个函数的问题,每次执行到printf("\n请输入%c结点的左小孩:",T->data);
的时候屏幕显示的是"请输入
                   结点的左孩子"
也就是%c 没有输出来,而且还换行怎么回事呢,麻烦帮看看一下

回复列表 (共3个回复)

沙发

尝试使用fflush(stdin)
来代替getchar()吸收回车

板凳

/*呵呵,谢谢搞清楚了,把它修改了一下,谢谢楼上的*/
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define flag '0'
typedef struct Tnode
{
    char data;
    struct Tnode *Lchild;
    struct Tnode *Rchild;
}Btree;
Btree *Great_Btree()
{
    char x;
    Btree *T;
    
    scanf("%c",&x);getchar();
    if(x==flag)
        T=NULL;
    else
    {
        T=(Btree*)malloc(sizeof(Btree));
        T->data=x;
        printf("\n请输入%c结点的左小孩:",T->data);
        T->Lchild=Great_Btree();
        printf("\n请输入%c结点的右小孩:",T->data);
        T->Rchild=Great_Btree();
    }
    return T;
}
void Preorder_Btree(Btree *T)
{
    if(T==NULL)
        return;
    else
    {
        printf("%c ",T->data);
        if(T->Lchild!=NULL)
            Preorder_Btree(T->Lchild);
        if(T->Rchild!=NULL)
            Preorder_Btree(T->Rchild);
    }
}

3 楼

void Inorder_Btree(Btree *T)
{
    if(T==NULL)
        return;
    else
    {
        if(T->Lchild!=NULL)
            Inorder_Btree(T->Lchild);
        printf("%c ",T->data);
        if(T->Rchild!=NULL)
            Inorder_Btree(T->Rchild);
    }
}
void Postorder_Btree(Btree *T)
{
    if(T==NULL)
        return;
    else
    {
        if(T->Lchild!=NULL)
            Postorder_Btree(T->Lchild);
        if(T->Rchild!=NULL)
            Postorder_Btree(T->Rchild);
        printf("%c ",T->data);
    }
}
int height_Btree(Btree *T)
{
    int lh,rh;
    if(T==NULL)
        return 0;
    else
    {
        lh=height_Btree(T->Lchild);
        rh=height_Btree(T->Rchild);
    }
    if(lh>rh)
        return lh+1;
    else
        return rh+1;
}
int num=0;
void leavenum_Btree(Btree *T)
{
    if(T==NULL)
        return ;
    else
    {
      if(T->Lchild==NULL&&T->Rchild==NULL)
          num++;
      leavenum_Btree(T->Lchild);
      leavenum_Btree(T->Rchild);
    }

}
int main()
{
    Btree *T;
    int k,flag1;
    do{
        printf("\n\n\n");
        printf("\t\t\t        二叉树子系统\n");
        printf("\t\t\t******************************\n");
        printf("\t\t\t**    1--建  二  叉 树      **\n");
        printf("\t\t\t**    2--前序遍历二叉树     **\n");
        printf("\t\t\t**    3--中序遍历二叉树     **\n");
        printf("\t\t\t**    4--后序遍历二叉树     **\n");
        printf("\t\t\t**    5--求二叉树的高度     **\n");
        printf("\t\t\t**    6--求二叉树叶子数     **\n");
        printf("\t\t\t**    0--返          回     **\n");
        printf("\t\t\t******************************\n");
        printf("\t\t\t   请输入菜单(0-6):");
        scanf("%d",&k);getchar();
        switch(k)
        {
        case 1:
            printf("\n请输入树的根结点:");
            T=Great_Btree();
            printf("\n树已建立! 请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 2:
            printf("\n前序遍历二叉树得的结果为:\n");
            Preorder_Btree(T);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 3:
            printf("\n中序遍历二叉树得的结果为:\n");
            Inorder_Btree(T);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 4:
            printf("\n后序遍历二叉树得的结果为:\n");
            Postorder_Btree(T);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 5:
            flag1=height_Btree(T);
            printf("\n该二叉树的高度为:%d",flag1);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        case 6:
            leavenum_Btree(T);
            printf("\n该二叉树的叶子数为:%d",num);
            printf("\n请按任意键返回菜单..");
            getch();
            system("cls");
            break;
        }
    }while(k!=0);
    return 0;
}


我来回复

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