哪位朋友帮我解决一下这个问题啊,谢谢了!

来源:互联网 发布:类似谷歌娘的软件 编辑:程序博客网 时间: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();
}

原创粉丝点击