树的层序遍历

本文介绍了一种实现二叉树层序遍历的方法,包括基本思路、算法实现及完整的代码示例。通过队列操作,实现了从根节点开始逐层访问二叉树的所有节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.基本思路

层序遍历的基本思路就是,
1.根节点入队列。
2.根节点出队,同时将根节点左儿子和右儿子入队
3.结点出队,同时将该节点的左儿子和右儿子入队
4.重复3直到队列为空

2.算法实现

void layerprint(struct TreeNode* r)
{
    struct queue q1;        //创建队列
    q1.s=&q1.node[0];       //初始化队列
    q1.e=&q1.node[0];
    struct TreeNode* p;     //临时结点指针
    in(&q1,r);              //根节点入队
    while(q1.e!=q1.s)       //判断队列是否为空
    {
        p = out(&q1);           //出队
        printf("%d ",p->data);      //访问结点
        if(p->left !=NULL)          
            in(&q1,p->left);        //左儿子入队
        if(p->right!=NULL)
            in(&q1,p->right);       //右儿子入队
    }       
}

3.完整代码

//main.c
#include<stdio.h>
#include "fun.c"
struct queue
{
    struct TreeNode node[100];
    struct TreeNode* s;
    struct TreeNode* e;
};
struct stack
{
    struct TreeNode node[100];
    struct TreeNode *top;
};
struct TreeNode* pop(struct stack* a)
{
    struct TreeNode* tmp =a->top;//printf("%d",*a->top);
    a->top--;
    return tmp;
    
}
int isEmpty(struct stack* a)
{
    if(a->top==&a->node[0])
        return 1;
    else 
        return 0;
}
void push(struct stack* a,struct TreeNode *node)
{
    a->top++;
    a->top->data = node->data;
    a->top->left = node->left;
    a->top->right = node->right;

}
void PrePrint(struct TreeNode *r)
{
    struct stack stack1;
    stack1.top = &stack1.node[0];
    struct TreeNode *p =r;
    while(p||!isEmpty(&stack1))
    {
        if(p!=NULL)
        {
            printf("%d ",p->data);
            push(&stack1,p);
            p = p->left;
        }
        else
        {
            p=pop(&stack1);
            p=p->right;
        }
    }
}
    



    /*while(p!=NULL)
    {
        //printf("%d ",p->data);
        if(p->left!=NULL)
        {
            printf("%d ",p->data);
            push(&stack1,p);    
            p=p->left;
        }
        else 
        {
            if(p->right!=NULL)
                p=p->right;
            printf("%d ",p->data);
            struct TreeNode* tmp;
            tmp = pop(&stack1);
            while(tmp->right==NULL)
            {tmp = pop(&stack1);}           
            p = tmp->right;
        }
        
    }   //push(&stack1,r);
    //pop()*/
/*void layerprint(struct TreeNode* r)
{
    queue q1;
    q1.s=&q1.node[0];
    q1.e=&q1.node[0];
}*/
void in(struct queue* a,struct TreeNode* b)
{
    *a->e=*b;
    //*a->e->left = *b->left;
    //*a->e->right =*b->right;
    a->e++; 
}
struct TreeNode* out(struct queue* a)
{
    struct TreeNode *tmp=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    *tmp = *a->s;
    a->s++;
    return tmp;
}
void layerprint(struct TreeNode* r)
{
    struct queue q1;
    q1.s=&q1.node[0];
    q1.e=&q1.node[0];
    struct TreeNode* p;
    in(&q1,r);
    while(q1.e!=q1.s)
    {
        p = out(&q1);
        printf("%d ",p->data);
        if(p->left !=NULL)
            in(&q1,p->left);
        if(p->right!=NULL)
            in(&q1,p->right);
    }
    
}


int main()
{
    //struct stack stack1;
    //stack1.top = &stack1.node[0];
    int max;
    printf("input the num:");
    scanf("%d",&max);
    //printf("please input 6 num\n ");
    struct TreeNode *r;
    r=initTree();
    int i=0;
    for(;i<max-1;i++)
        createTree(r);
    //PrePrint(r);
    layerprint(r);
    return 0;
}
\\fun.文件内容
#include<stdio.h>
#include<stdlib.h>
struct TreeNode
{
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
}node;
struct TreeNode* initTree()
{
    struct TreeNode* r = (struct TreeNode* )malloc(sizeof(struct TreeNode));
    r->left = NULL;
    r->right =NULL;
    int num;
    scanf("%d",&num);
    r->data = num;
    return r;
}
void createTree(struct TreeNode* r)
{
    struct TreeNode *tmp = r;
    struct TreeNode *p ;
    int num;
    scanf("%d",&num);
    struct TreeNode *a = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    a->left = NULL;
    a->right = NULL;
    a->data = num;
    while(tmp!=NULL)
    {
        if(num >= tmp->data)
        {
            p=tmp;
            tmp = tmp->right;           
        }
        else
        {
            p=tmp;
            tmp = tmp->left;
        }
    }
    if(a->data>=p->data)
    {
        p->right = a;
    }
    else 
    {
        p->left = a;
    }   
}
struct TreeNode* insert(struct TreeNode* r,int num)
{
    /*struct TreeNode* a = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    int num;
    scanf("%d",&num);
    a->data = num;
    a->left = NULL;
    a->right = NULL;*/
    if(r==NULL)
    {
        struct TreeNode* a = (struct TreeNode*)malloc(sizeof(struct TreeNode));
            a->data = num;
            a->left = NULL;
            a->right = NULL;
        return a;
    }
    else
    {
        if(num >= r->data)
        {
            r->right = insert(r->right,num);
        } 
        else
        {
            r->left = insert(r->left,num);
        }
    return r;
    }

}

void preOrderSearch(struct TreeNode* r)
{
    if(r==NULL)
        return ;
    else 
    {
        preOrderSearch(r->left);
        printf("%d ",r->data);
        preOrderSearch(r->right);
    }
}

转载于:https://www.cnblogs.com/zhengkang/p/5621315.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值