| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5729 人关注过本帖
标题:求大神指导,我不知道这visit函数怎么啦
取消只看楼主 加入收藏
chenwei926fl
Rank: 1
来 自:宜昌
等 级:新手上路
帖 子:16
专家分:0
注 册:2013-4-4
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
求大神指导,我不知道这visit函数怎么啦
/*创建二叉树:先序创建;
遍历二叉树:先,中,后;
二叉树属性:深度,结点数,叶子结点数;
*/
#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");
    }

    }
}
搜索更多相关主题的帖子: 函数 指导 include 二叉树 visit 
2013-05-04 16:51
chenwei926fl
Rank: 1
来 自:宜昌
等 级:新手上路
帖 子:16
专家分:0
注 册:2013-4-4
收藏
得分:0 
回复 2楼 Juson
额,visit函数和需调用 的数据类型不一样
好的,谢谢你把新的问题提出来,我会注意的

千百度
2013-05-06 19:07
chenwei926fl
Rank: 1
来 自:宜昌
等 级:新手上路
帖 子:16
专家分:0
注 册:2013-4-4
收藏
得分:0 
回复 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;     

千百度
2013-05-06 19:22
快速回复:求大神指导,我不知道这visit函数怎么啦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017049 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved