| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 440 人关注过本帖
标题:对二叉树的按层遍历问题。
只看楼主 加入收藏
qq865738652
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-10-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
对二叉树的按层遍历问题。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
typedef struct node
{
    char data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
typedef struct QNode      
{
    BTNode data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

void InitQueue(LinkQueue &q)
{
 q.front=q.rear=(QueuePtr)malloc(sizeof(QNode));
 q.front->next=NULL;
}

int QueueEmpty(LinkQueue Q)
{
    if(Q.front->next==NULL)
       return 1;
    return 0;
}

void EnQueue(LinkQueue &Q,BTNode p)
{
    QueuePtr s;
    s=(QueuePtr)malloc(sizeof(QNode));
    s->data=p;
    s->next=NULL;
    Q.rear->next=s;
    Q.rear=s;
}
void DeQueue(LinkQueue &Q,BTNode e)
{
    QueuePtr p;
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(Q.rear==p)
        Q.rear=Q.front;
    free(p);
}
BTNode *CreateTree(BTNode *L,char str[100]);
int i=0;

BTNode *CreateTree(BTNode *L,char str[100])            /*这棵树输入时为中序输入*/
{
    BTNode *k=NULL;
    if(str[i]=='0') L=NULL;
    else{L=(BTNode *)malloc(sizeof(BTNode));
    L->data=str[i];
    L->lchild=NULL;
    L->rchild=NULL;
    i++;L->lchild=CreateTree(k,str);
    i++;L->rchild=CreateTree(k,str);}
    return(L);
}
void Preorder(BTNode *L)                  /*先序遍历*/
{
    if(L!=NULL)
    {
            printf("%c ",L->data);
            Preorder(L->lchild);
            Preorder(L->rchild);

    }
}
void visit(int e)
 { printf("%s ",e);
 }
int Depth(BTNode *L)
{
   int i,j;
   if(!L) return 0;
   if(L->lchild) i=Depth(L->lchild);
   else i=0;
   if(L->rchild) j=Depth(L->rchild);
   else j=0;
   return i>j?i+1:j+1;
}

void Inorder(BTNode *L)                         /*中序遍历*/
{
   if(L!=NULL)
   {
          Inorder(L->lchild);
          printf("%c ",L->data);
          Inorder(L->rchild);
   }   
}

void Postorder(BTNode *L)                 /*后序遍历*/
{
    if(L!=NULL)
    {
       Postorder(L->lchild);
          Postorder(L->rchild);
          printf("%c ",L->data);
    }
}

void display(BTNode *L)
{
    LinkQueue Q;
    InitQueue(Q);
    if(L) EnQueue(Q,*L);
    while(QueueEmpty(Q)==0)
    {
        DeQueue(Q,*L);
        printf("%c ",L->data);
        if(L->lchild) EnQueue(Q,*(L->lchild));
        if(L->rchild) EnQueue(Q,*(L->rchild));
    }
}

int main()
{
    BTNode *L;
    char str[100];
    L=(BTNode *)malloc(sizeof(BTNode));
    scanf("%s",str);
    L=CreateTree(L,str);
    printf("先序遍历:");
    Preorder(L);
    printf("\n");
    printf("中序遍历:");
    Inorder(L);
    printf("\n");
    printf("后序遍历:");
    Postorder(L);
    printf("\n");
    printf("树的深度:");
    Depth(L);
    printf("%d\n",Depth(L));
    display(L);
}



我这个代码的display函数有问题,按层遍历输出是无线循环,例如输入二叉树3860090040500,正确输出应该是384695
2015-10-24 15:04
w2009w
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:190
专家分:542
注 册:2015-4-20
收藏
得分:20 
2015-10-24 15:32
快速回复:对二叉树的按层遍历问题。
数据加载中...
 
   



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

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