哪位朋友帮我解决一下这个问题啊,谢谢了!
来源:互联网 发布:类似谷歌娘的软件 编辑:程序博客网 时间:2024/04/27 21:28
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#define MAXSIZE 100
int count = 0; //纪录结点个数
typedef struct node{
char data;
struct node *lchild,*rchild;
}tree,*p_tree;
typedef struct {
tree data[MAXSIZE];
int front,rear;
}seq_queue,*p_seq_queue;
p_seq_queue init_seq_queue() //队列初始化
{
p_seq_queue a;
a = (p_seq_queue)malloc(sizeof(seq_queue));
if( a )
{
a->front = 0;
a->rear = 0;
}
return a;
}
int empty_seq_queue(p_seq_queue a) //判断队列是否为空
{
if(a && a->front == a->rear)
return 1;
else
return 0;
}
int in_seq_queue(p_seq_queue a,p_tree x) //入队列
{
if((a->rear + 1)%MAXSIZE == a->front)
{
printf("队列满");
return -1;
}
else
{
a->rear = (a->rear + 1)%MAXSIZE;
a->data[a->rear] = *x;
}
return 1;
}
int out_seq_queue(p_seq_queue a,p_tree x) //出队列
{
if(empty_seq_queue( a ))
{
printf("队空");
return -1;
}
else
{
a->front = (a->front + 1)%MAXSIZE;
*x = a->data[a->front];
return 1;
}
}
void level_traversal( p_tree p) //层次遍历
{
int r;
p_seq_queue a;
a = init_seq_queue();
tree x;
x = *p; //这是通过引用进出队列的函数调用,下面那个被注释掉的层次遍历
if( &x )
{ //函数是通过指针进出队列的。
printf("%c",x.data); //问题在于:通过指针的层次遍历函数后,再调用求深度的函数,不能得到正确的结果。
r=in_seq_queue( a , &x); //我问题描述的不清楚,不明白的就是为什么指针和引用在这个层次调用上的差别?
while(r!=-1&&!empty_seq_queue(a))//如果可以的朋友你亲自调试下!
{
out_seq_queue( a , &x );
if(x.lchild)
{
printf("%c",x.lchild->data);
r = in_seq_queue( a , x.lchild );
}
if(x.rchild)
{
printf("%c",x.rchild->data);
r = in_seq_queue( a , x.rchild );
}
}//end of while
}
}
/*void level_traversal( p_tree p) //层次遍历
{
int r;
p_seq_queue a;
a = init_seq_queue();
p_tree x;
x = p;
if( x )
{
printf("%c",x->data);
r=in_seq_queue( a , x);
while(r!=-1&&!empty_seq_queue(a))
{
out_seq_queue( a , x );
if(x->lchild)
{
printf("%c",x->lchild->data);
r = in_seq_queue( a , x->lchild );
}
if(x->rchild)
{
printf("%c",x->rchild->data);
r = in_seq_queue( a , x->rchild );
}
}//end of while
}
}*/
p_tree creat_tree( ) //先序创建树
{
p_tree p;
char ch;
scanf("%c",&ch);
if(ch == '0')
{
p = NULL;
return p;
}
else
{
p = (p_tree)malloc(sizeof(tree));
p->data = ch;
p->lchild = creat_tree();
p->rchild = creat_tree();
}
return p;
}
void out_put(p_tree P) //后序输出树
{
if( P!=NULL )
{
out_put(P->lchild);
out_put(P->rchild);
printf("%c",P->data);
count = count + 1; //计算结点的个数
}
}
int height(p_tree p) //计算树的深度
{
int h1,h2;
if(p == NULL)
return 0;
else
{
h1 = height(p->lchild);
printf("h1 = %d",h1);
h2 = height(p->rchild);
if(h1 > h2)
return (h1 + 1);
else
return (h2 + 1);
}
}
void main()
{
p_tree t,q;
q = NULL;
q = creat_tree( );
t = q;
printf("后序遍历:");
out_put( t );
printf("/n");
int i;
i = height( t );
printf("深度为: %d",i);
printf("/n层次遍历:");
level_traversal( t );
printf("/n");
printf("结点个数为:%d/n",count);
// int i;
i = height( q );
printf("深度为:%d",i);
printf("后序遍历:");
out_put( t );
getch();
}
- 哪位朋友帮我解决一下这个问题啊,谢谢了!
- 哪位大侠能帮我解决一下,为什么二级关联能用,三级关联老是出错,谢谢了.
- 请好心人的朋友帮我找一下错误,谢谢了~
- 哪位用CSDNBLOG的朋友帮我解决个问题
- 哪位老哥帮我完成一下这个配置~多谢了
- Fragment嵌套ViewPager+Fragment使用FragmentStatePagerAdapter报错,哪位大神帮我解决这个问题
- 希望大家帮我做一下这套题啊,太谢谢了
- 希望大家帮我做一下这套题啊,太谢谢了
- 希望大家帮我做一下这套题啊,太谢谢了
- 帮我顶顶,朋友的,谢谢
- 谁能帮我解决这个问题哦 谢谢
- 谁能帮我解决一下这个问题
- 高手来帮我解决下这个问题啊..!!
- 求大神帮解答javaEE这个问题,谢谢了
- 哪位高手帮我解决一个问题?
- POP3 邮件接收 出现乱码了,希望哪位能帮忙看下啥问题!帮我改下,谢谢
- 我的程序哪里错了?谢谢各位帮我看一下!
- 大家好!我是一个菜鸟!!遇到问题了,请教大家帮我一下,急,谢谢了!!!
- ADO.Net连接Infomix数据库
- MyEclipse 快捷键大全
- Weblogic集群概念和配置(五)
- sap实施流程(关键用户)
- Weblogic集群概念和配置(六)
- 哪位朋友帮我解决一下这个问题啊,谢谢了!
- 80后被分成九大派,你是哪一派?
- smartfoxserver中配置文件config.xml的使用技巧及其说明
- struts redirect
- Jquery 实现放大镜效果 jqzoom插件
- C# 实现完整的仿QQ截图控件
- 修改注册表 打造个性化右键菜单
- Menlow上网本可畅享雷神之锤
- 人,应该有所为