针对考研的C语言学习(二叉树专题层次遍历---广度优先遍历)

层次便利需要一个队列来辅助保存节点信息

代码

复制代码
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;

//树
typedef struct trees{
    ElemType data;
    struct trees*lc;
    struct trees*rc;
}treeNode,*Tree;

//链表
typedef struct Links{
    Tree tree;
    struct Links*next;
}LNode,*LinkList;

//队列
typedef struct{
  LinkList front;
  LinkList rear;
}LinkQue;

void init_que(LinkQue&q)
{
    q.front=q.rear=(LinkList)calloc(1,sizeof(LNode));
    q.front=q.rear;
}

bool isEmpty(LinkQue&q)
{
    return q.front == q.rear;
}

//入队
void push_que(LinkQue&q,Tree tree)
{
    //新建链表节点
    LinkList newList = (LinkList)calloc(1,sizeof(LNode));
    newList->next=NULL;
    newList->tree=tree;
    q.rear->next=newList;
    q.rear=q.rear->next;
}
bool pop_que(LinkQue&q,Tree &tree)
{
    if(isEmpty(q))
    {
        return false;
    }
    LinkList del = q.front->next;//头结点不存数据,第一个节点才是真的数据起始位置
    q.front->next=del->next;//断链
    tree=del->tree;
    if(q.rear == del)//只剩下尾节点的数据
    {
        q.rear=q.front;//置空
    }
    free(del);
    return true;
}

void build_tree(Tree&tree)
{
    LinkQue q;
    init_que(q);
    LinkList cur = NULL;
    ElemType data;
    while(scanf("%c",&data) && data != '\n')
    {
        Tree newTree = (Tree)calloc(1,sizeof(treeNode));//申请新的树的节点
        newTree->data=data;
        if(tree == NULL)
        {
            tree = newTree;
            push_que(q,tree);//入队
            cur = q.rear;
        }
        else
        {
            if(cur->tree->lc == NULL)
            {
                cur->tree->lc = newTree;
                push_que(q,newTree);
            }
            else
            {
                cur->tree->rc = newTree;
                push_que(q,newTree);
                //改变当前父亲节点
                cur = cur->next;
            }
        }
    }
}

void pre_print(Tree t)
{
    if(t)
    {
        putchar(t->data);
        pre_print(t->lc);
        pre_print(t->rc);
    }
}
void mid_print(Tree t)
{
    if(t)
    {
        mid_print(t->lc);
        putchar(t->data);
        mid_print(t->rc);
    }
}
void post_print(Tree t)
{
    if(t)
    {
        post_print(t->lc);
        post_print(t->rc);
        putchar(t->data);
    }
}

void level_print(Tree t)
{
    Tree cur;
    LinkQue q;//队列建立
    init_que(q);
    push_que(q,t);//树根入队
    while(!isEmpty(q))//队列不为空
    {
        //出队
        pop_que(q,cur);
        putchar(cur->data);
        if(cur->lc)
        {
            push_que(q,cur->lc);//左子树入队
        }
        if(cur->rc)
        {
            push_que(q,cur->rc);//右子树入队
        }
    }
}
int main()
{
    Tree tree = NULL;
    build_tree(tree);
    // pre_print(tree);
    level_print(tree);
    return 0;
}

代码运行结果

相关推荐
yuhaiqun198916 分钟前
Typora 技能进阶:从会写 Markdown 到玩转配置 + 插件高效学习笔记
经验分享·笔记·python·学习·学习方法·ai编程·markdown
我命由我123451 小时前
Python Flask 开发问题:ImportError: cannot import name ‘Markup‘ from ‘flask‘
开发语言·后端·python·学习·flask·学习方法·python3.11
全栈陈序员2 小时前
【Python】基础语法入门(二十)——项目实战:从零构建命令行 To-Do List 应用
开发语言·人工智能·python·学习
AA陈超2 小时前
枚举类 `ETriggerEvent`
开发语言·c++·笔记·学习·ue5
小六*^____^*2 小时前
虚拟列表学习
前端·javascript·学习
代码游侠2 小时前
学习笔记——IPC(进程间通信)
linux·运维·网络·笔记·学习·算法
charlie1145141913 小时前
如何把 Win32 窗口“置顶”(Windows + C++)
开发语言·c++·windows·笔记·学习·软件工程
名字不相符3 小时前
BUUCTF题目列表Misc题目(个人记录与学习)(第二页)
学习·misc·buuctf·萌新
点云SLAM3 小时前
Mitigation 英文单词学习
学习·英文单词学习·雅思备考·降低风险或影响·缓解、减轻·mitigation
xie_pin_an3 小时前
C 语言排序算法全解析:从原理到实战,附性能对比
c语言·算法·排序算法