广度优先遍历代码

广度优先周游二叉树(层序遍历)是用队列来实现的,从二叉树的第一层(根结点)开始,自上至下逐层遍历;在同一层中,按照从左到右的顺序对结点逐一访问。
按照从根结点至叶结点、从左子树至右子树的次序访问二叉树的结点。算法:
    1初始化一个队列,并把根结点入列;
    2根节点入队
    3循环直到队列空
      3.1  q=队列头元素出队
      3.2  访问q的数据域
      3.3  若q有左孩子  左孩子入队
      3.4  若q有右孩子  右孩子入队

代码来自 http://leidiqiu.iteye.com/blog/854087  

 
  #include <stdio.h>  
    #include <stdlib.h>  
      
    //树节点的定义  
    struct TNode{  
        int value;  
        struct TNode *left;  
        struct TNode *right;  
    };  
      
    //队列节点的定义  
    struct QNode{  
        struct TNode *p;  
        struct QNode *next;  
    };  
      
    //队列的定义  
    struct Queue{  
        struct QNode *front;  
        struct QNode *rear;  
    };  
      
    //初始化队列  
    void InitQueue(struct Queue *Q){  
        Q->front=Q->rear=(struct QNode*)malloc(sizeof(struct QNode));  
        Q->front->next=NULL;  
    }  
      
    //入队列  
    void EnQueue(struct Queue *Q, struct TNode *node){  
        printf("en-node-value:%d\n",node->value);  
        struct QNode *nd=(struct QNode*)malloc(sizeof(struct QNode));  
        nd->next=NULL;  
        nd->p=node;  
        Q->rear->next=nd;  
        Q->rear=nd;  
    }  
      
    //出队列,注意判定快为空时,应有 rear=front  
    struct TNode* DeQueue(struct Queue *Q){  
        struct QNode *p=Q->front->next;  
        Q->front->next=Q->front->next->next;  
        struct TNode *t=p->p;  
        if(Q->rear==p){  
            Q->rear=Q->front;  
        }  
        free(p);  
        printf("de-node-value:%d\n",t->value);  
        return t;  
    }  
      
    //建立二叉树  
    void createLR(struct TNode *node){  
        if(node->value<20){  
            struct TNode *n1=(struct TNode*)malloc(sizeof(struct TNode));  
            struct TNode *n2=(struct TNode*)malloc(sizeof(struct TNode));  
            node->left=n1;  
            n1->value=node->value*2;  
            node->right=n2;  
            n2->value=node->value*2+1;      
            createLR(n1);  
            createLR(n2);  
        }else{  
            node->left=NULL;  
            node->right=NULL;  
        }  
    }  
      
    //主程序  
    int main(int argc, char **argv){  
      
        struct TNode *head=(struct TNode*)malloc(sizeof(struct TNode));  
        head->value=1;  
        createLR(head);  
      
                    //输出左、右边,测试树是否正确建立  
        struct TNode *p=head;  
        while(p){  
            printf("%d\n",p->value);  
            p=p->left;  
        }  
        p=head;  
        while(p){  
            printf("%d\n",p->value);  
            p=p->right;  
        }  
        printf("\n");  
      
                    //用一个辅助的链队列,广度遍历树  
        p=head;  
        struct Queue *q;  
        InitQueue(q);  
        printf("%d\n",p->value);  
        EnQueue(q,p);  
        while(q->front->next!=NULL){  
            p=DeQueue(q);  
            if(p->left!=NULL){  
                printf("%d\n",p->left->value);  
                EnQueue(q,p->left);  
            }  
            if(p->right!=NULL){  
                printf("%d\n",p->right->value);  
                EnQueue(q,p->right);  
            }  
        }  
          
                    //以下代码没有别的意思,就是测试命令行参数  
        int i=0;  
        for(i=0; i<argc; i++){  
            printf("%s\n",argv[i]);  
        }  
      
        return 0;  
    }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值