| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 347 人关注过本帖
标题:二叉树遍历有误
只看楼主 加入收藏
a136141238
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-3-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
二叉树遍历有误
#include "stdio.h"
#include "stdlib.h"
struct  node
{
   int num;
   int count;
  struct  node *pleft;
  struct  node *pright;
};

struct  node *pnode;  //定义全局指针 指向二叉树根节点

struct  node *creatnode(int i)           //创建二叉树节点的函数
{
    struct  node *q;
    q=(struct  node*)malloc(sizeof(struct  node));
    q->count=1;
    q->num=i;
    q->pleft=NULL;  q->pright=NULL;
    return q;
}
struct  node *charu(struct  node *p,int k)  //插入二叉树的节点
{
if (k==p->num)
{
    p->count++;
return NULL;
}
if (k>p->num)
{
    if (p->pright==NULL)
    {
        p->pright=creatnode(k);
        return p->pright;
    }
    else
        return charu(p->pright,k);
}
if (k<p->num)
{
    if (p->pleft==NULL)
    {
        p->pleft=creatnode(k);
        return p->pleft;
    }
    else
        return charu(p->pleft,k);
}


}
void bianli(struct  node *p)
{
    if (p->pleft==NULL)
    {
        printf("%d ",p->num);
    }
    else
    {    bianli(p->pleft);
    }
    if (p->pright!=NULL)
    {
        bianli(p->pright);
    }

}
void main()
{
    int b;
 pnode=creatnode(8);//创建根节点
 for (b=1;b<=10;b++)
 {
     charu(pnode,b);
 }
 printf("%d\n",pnode->num);
 bianli(pnode);
 getchar();
  getchar();

}
按理说输出应该是1 2 3 4 5 6 7 8 9 10
但是输出的是 1 2 3 4 5 6 7 9 10  没有8  这是为什么啊
[local]1[/local]
搜索更多相关主题的帖子: include return 二叉树 count 
2014-03-06 16:06
ljx小子
Rank: 8Rank: 8
来 自:星星
等 级:蝙蝠侠
威 望:2
帖 子:222
专家分:916
注 册:2013-10-7
收藏
得分:20 
程序代码:
#include "stdio.h"
#include "stdlib.h"
struct  node
{
   int num;
   int count;
  struct  node *pleft;
  struct  node *pright;
};

struct  node *pnode;  //定义全局指针 指向二叉树根节点

struct  node *creatnode(int i)           //创建二叉树节点的函数
{
    struct  node *q;
    q=(struct  node*)malloc(sizeof(struct  node));
    q->count=1;
    q->num=i;
    q->pleft=NULL;  q->pright=NULL;
    return q;
}
struct  node *charu(struct  node *p,int k)  //插入二叉树的节点
{
if (k==p->num)
{
    p->count++;
return NULL;
}
if (k>p->num)
{
    if (p->pright==NULL)
    {
        p->pright=creatnode(k);
        return p->pright;
    }
    else
        return charu(p->pright,k);
}
if (k<p->num)
{
    if (p->pleft==NULL)
    {
        p->pleft=creatnode(k);
        return p->pleft;
    }
    else
        return charu(p->pleft,k);
} 


}
void bianli(struct  node *p)
{
    if (p->pleft==NULL)
    {
        printf("%d ",p->num);
    }
    else
    {    bianli(p->pleft);
    }
    if(p->pleft!=NULL&&p->pright!=NULL)//这里你是以8为根节点建立的树,它的左儿子和右儿子都存在,所以你的遍历函数不能输出8,最简单的方法
        printf("%d ",p->num);//在这里加上一个判断左右儿子都存在的点并输出。
    if (p->pright!=NULL)
    {
        bianli(p->pright);
    }

}
void main()
{
    int b;
pnode=creatnode(8);//创建根节点
for (b=1;b<=10;b++) 
{
     charu(pnode,b);
}
printf("%d\n",pnode->num);
bianli(pnode);
getchar();
  getchar();

}
图片附件: 游客没有浏览图片的权限,请 登录注册

你的输出只是判断左儿子为空时输出,所以8这个根节点不会输出。

。。。。。。。。。。。
2014-03-07 18:20
快速回复:二叉树遍历有误
数据加载中...
 
   



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

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