注册 登录
编程论坛 数据结构与算法

求大神指导,我不知道这visit函数怎么啦

chenwei926fl 发布于 2013-05-04 16:51, 5729 次点击
/*创建二叉树:先序创建;
遍历二叉树:先,中,后;
二叉树属性:深度,结点数,叶子结点数;
*/
#include<stdio.h>
#include<malloc.h>
typedef char DataType;
typedef struct BNode
{
    char data;
    struct BNode *Lchild;
    struct BNode *Rchild;
}BNode,*BiTree;
void CreateTree(BiTree &T)
{
    char ch;
    scanf("%c",&ch);        //读入一个字符   
    if(ch==' ') T=NULL;
    else{T=(BNode *)malloc(sizeof(BNode)); //生成一个新结点
        T->data=ch;   
        CreateTree(T->Lchild);      //生成左子树   
        CreateTree(T->Rchild);      //生成右子树
        }
}
void visit(BiTree root)
{
    if(root!=NULL)
    printf("%c",root->data);
}
void PreOrder(BiTree root)
{if(root!=NULL)
    {
    visit(root->data);
    PreOrder(root->Lchild);
    PreOrder(root->Rchild);
    }
}
void InOrder(BiTree root)
{if(root!=NULL)
    {
    InOrder(root->Lchild);
    visit(root->data);
    InOrder(root->Rchild);
    }
}
void PostOrder(BiTree root)
{if(root!=NULL)
    {
    PostOrder(root->Lchild);
    PostOrder(root->Rchild);
    visit(root->data);
    }
}
int PostTreeDepth(BiTree bt)
{
    int h1,h2,max;
    if(bt!=NULL)
    {
        h1=PostTreeDepth(bt->Lchild);
        h2=PostTreeDepth(bt->Rchild);
        max=h1>h2?h1:h2;
        return(max+1);
    }
    else return(0);
}
int TreeCount(BiTree root)
{
    int Count;
    if(root==NULL) return(0);
    else
    Count=TreeCount(root->Lchild)+TreeCount(root->Rchild)+1;
    return(Count);
}
int leaf(BiTree root)
{
    int LeafCount;
    if(root==NULL)
    LeafCount=0;
    else if((root->Lchild==NULL)&&(root->Rchild==NULL))
    LeafCount=1;
    LeafCount=leaf(root->Lchild)+leaf(root->Rchild);
    return(LeafCount);
}

void main()
{
    BiTree T;
    char j;
    int flag=1;
    //---------------------程序解说-----------------------
    printf("本程序实现二叉树的操作。\n");
    printf("可以进行建立二叉树;递归先序、中序、后序遍历;二叉树属性:深度,结点数,叶子结点数\n");
    //----------------------------------------------------
    printf("\n");
    printf("请建立二叉树。\n");
    CreateTree(T);        
    getchar();
    while(flag)
    {     
    printf("\n");   
    printf("请选择: \n");      
    printf("1.递归先序遍历\n");   
    printf("2.递归中序遍历\n");   
    printf("3.递归后序遍历\n");
    printf("4.二叉树深度\n");
    printf("5.二叉树结点数\n");  
    printf("6.二叉树叶子结点数\n");
    printf("0.退出程序\n");     
    scanf(" %c",&j);      
    switch(j)   
    {      
    case '1':if(T)     
        {    printf("递归先序遍历二叉树:");
            PreOrder(T);   
            printf("\n");
        }   
        else
            printf("二叉树为空!\n");
            break;  
    case '2':if(T)               
        {  
            printf("递归中序遍历二叉树:");  
            InOrder(T);      
            printf("\n");         
        }   
        else
            printf("二叉树为空!\n");      
            break;      
    case '3':if(T)
        {   printf("递归后序遍历二叉树:");        
            PostOrder(T);        
            printf("\n");   
        }
        else
            printf("二叉树为空!\n");
        break;
    case '4':if(T)              
        {
            printf("二叉树深度:");  
            PostTreeDepth(T);
            printf("\n");   
        }
            else
            printf("二叉树为空!\n");
            break;     
    case '5':if(T)            
        {
            printf("二叉树结点数:");
            TreeCount(T);  
            printf("\n");
        }
        else printf("二叉树为空!\n");
        break;
    case '6':if(T)   
         {
            printf("二叉树叶子结点数:");
            leaf(T);  
            printf("\n");
         }
        else printf("二叉树为空!\n");
        break;   
        default:flag=0;printf("程序运行结束,按任意键退出!\n");
    }

    }
}
5 回复
#2
Juson2013-05-05 01:07
这是你定义的visit函数
void visit(BiTree root)
{
    if(root!=NULL)
    printf("%c",root->data);
}

这是你对visit的调用
visit(root->data);
自己看看哪里错了


另外说一下这个函数
void CreateTree(BiTree &T)
{
    char ch;
    scanf("%c",&ch);        //这里读入一个字符之后, 如果你按了回车键,下次scanf会把回车符赋给ch,所以最好还是对回车符做一下处理
    if(ch==' ') T=NULL;
    else{T=(BNode *)malloc(sizeof(BNode)); //生成一个新结点
        T->data=ch;  
        CreateTree(T->Lchild);      //生成左子树  
        CreateTree(T->Rchild);      //生成右子树
        }
}
#3
飞黄腾达2013-05-05 10:02
帮你改了一下,好了,你看看

/*创建二叉树:先序创建;
遍历二叉树:先,中,后;
二叉树属性:深度,结点数,叶子结点数;
*/
#include<stdio.h>
#include<malloc.h>
typedef char DataType;
typedef struct Node
{
    DataType data;
    struct Node *Lchild;
    struct Node *Rchild;
}BNode,*BiTree;
BiTree CreateTree()
{
    char ch;
    BiTree T;
    scanf("%c",&ch);        //读入一个字符   
    if(ch==' ') T=NULL;
    else{T=(BiTree)malloc(sizeof(BNode)); //生成一个新结点
        T->data=ch;   
        T->Lchild=CreateTree();      //生成左子树   
        T->Rchild=CreateTree();      //生成右子树
        }
return T;
}

void visit(char c)
{
   
    printf("%c",c);
}
void PreOrder(BiTree root)
{if(root!=NULL)
    {
    visit(root->data);
    PreOrder(root->Lchild);
    PreOrder(root->Rchild);
    }
}
void InOrder(BiTree root)
{if(root!=NULL)
    {
    InOrder(root->Lchild);
    visit(root->data);
    InOrder(root->Rchild);
    }
}
void PostOrder(BiTree root)
{if(root!=NULL)
    {
    PostOrder(root->Lchild);
    PostOrder(root->Rchild);
    visit(root->data);
    }
}
int PostTreeDepth(BiTree bt)
{
    int h1,h2,max;
    if(bt!=NULL)
    {
        h1=PostTreeDepth(bt->Lchild);
        h2=PostTreeDepth(bt->Rchild);
        max=h1>h2?h1:h2;
        return(max+1);
    }
    else return(0);
}
int TreeCount(BiTree root)
{
    int Count;
    if(root==NULL) return(0);
    else
    Count=TreeCount(root->Lchild)+TreeCount(root->Rchild)+1;
    return(Count);
}
int leaf(BiTree root)
{
    int LeafCount;
    if(root==NULL)
    return 0;
    else if((root->Lchild==NULL)&&(root->Rchild==NULL))
    return 1;
    LeafCount=leaf(root->Lchild)+leaf(root->Rchild);
    return(LeafCount);
}

void main()
{
    BiTree T;
    char j;
    int flag=1;
    //---------------------程序解说-----------------------
    printf("本程序实现二叉树的操作。\n");
    printf("可以进行建立二叉树;递归先序、中序、后序遍历;二叉树属性:深度,结点数,叶子结点数\n");
    //----------------------------------------------------
    printf("\n");
    printf("请建立二叉树。\n");
    T=CreateTree();        
    getchar();
    while(flag)
    {     
    printf("\n");   
    printf("请选择: \n");      
    printf("1.递归先序遍历\n");   
    printf("2.递归中序遍历\n");   
    printf("3.递归后序遍历\n");
    printf("4.二叉树深度\n");
    printf("5.二叉树结点数\n");  
    printf("6.二叉树叶子结点数\n");
    printf("0.退出程序\n");     
    scanf(" %c",&j);      
    switch(j)   
    {      
    case '1':if(T)     
        {    printf("递归先序遍历二叉树:");
            PreOrder(T);   
            printf("\n");
        }   
        else
            printf("二叉树为空!\n");
            break;  
    case '2':if(T)               
        {  
            printf("递归中序遍历二叉树:");  
            InOrder(T);      
            printf("\n");         
        }   
        else
            printf("二叉树为空!\n");      
            break;      
    case '3':if(T)
        {   printf("递归后序遍历二叉树:");        
            PostOrder(T);        
            printf("\n");   
        }
        else
            printf("二叉树为空!\n");
        break;
    case '4':if(T)              
        {
            printf("二叉树深度:");  
            printf("%d",PostTreeDepth(T));
            printf("\n");   
        }
            else
            printf("二叉树为空!\n");
            break;     
    case '5':if(T)            
        {
            printf("二叉树结点数:");
            printf("%d",TreeCount(T));  
            printf("\n");
        }
        else printf("二叉树为空!\n");
        break;
    case '6':if(T)   
         {
            printf("二叉树叶子结点数:");
             printf("%d",leaf(T));  
            printf("\n");
         }
        else printf("二叉树为空!\n");
        break;   
        default:flag=0;printf("程序运行结束,按任意键退出!\n");
    }

    }
}
#4
chenwei926fl2013-05-06 19:07
回复 2楼 Juson
额,visit函数和需调用 的数据类型不一样
好的,谢谢你把新的问题提出来,我会注意的
#5
chenwei926fl2013-05-06 19:22
回复 3楼 飞黄腾达
你把这函数的左 右子树改为遍历了,可是我还是不能实现对深度超过2的树的调用
BiTree CreateTree()
 {
     char ch;
     BiTree T;
     scanf("%c",&ch);        //读入一个字符   
     if(ch==' ') T=NULL;
     else{T=(BiTree)malloc(sizeof(BNode)); //生成一个新结点
        T->data=ch;   
        T->Lchild=CreateTree();      //生成左子树   
         T->Rchild=CreateTree();      //生成右子树
         }
 return T;
 }
我还有一个错误,粗心了
 int leaf(BiTree root)
 {
     int LeafCount;
     if(root==NULL)
     return 0;
     else if((root->Lchild==NULL)&&(root->Rchild==NULL))
     return 1;
     else   LeafCount=leaf(root->Lchild)+leaf(root->Rchild);
     return(LeafCount);
 }

还有,谢谢你帮我看出下面的错误
 case '4':if(T)              
         {
             printf("二叉树深度:");  
             printf("%d",PostTreeDepth(T));
             printf("\n");   
         }
             else
             printf("二叉树为空!\n");
             break;     
#6
邓士林2013-05-07 21:09
函数的参数要多注意
1