C语言编写二叉树的先序遍历、中序遍历、后序遍历、层次遍历

前面三个遍历都是通过递归的方法完成,最后的层次遍历通过调用队列出队、入队函数完成。参考代码如下:

tree.h

#ifndef  TREE_H
#define  TREE_H

typedef struct tree{
    int data;
    struct tree *lchild;//创建左指针
    struct tree *rchild;//创建右指针
}Tree_t;

Tree_t *create_tree(int i,int n);
void preorder(Tree_t *root);
void inorder_show(Tree_t *root);
void last_show(Tree_t *root);
void queue_show(Tree_t *root);
#endif

tree.c

#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
#include "linkqueue.h"

Tree_t *create_tree(int i,int n)
{
    Tree_t *root=(Tree_t *)malloc(sizeof(Tree_t));
    if(root == NULL)
        return NULL;
    root->data=i;

    if(i*2 <= n)
        root->lchild=create_tree(2*i,n);
    else
        root->lchild=NULL;

    if(i*2+1 <= n)
        root->rchild=create_tree(2*i+1,n);
    else
        root->rchild=NULL;
    return root;
}
//先序遍历
void preorder(Tree_t *root)
{
    if(NULL==root)
        return;
    printf("%d ",root->data);//root
    preorder(root->lchild);//左子树
    preorder(root->rchild);//右子树
}
//中序遍历
void inorder_show(Tree_t *root)
{
    if(NULL==root)
        return;
    inorder_show(root->lchild);//左子树
    printf("%d ",root->data);//root
    inorder_show(root->rchild);//右子树
}
//后序遍历
void last_show(Tree_t *root)
{
    if(NULL==root)
        return;
    last_show(root->lchild);//左子树
    last_show(root->rchild);//右子树
    printf("%d ",root->data);//root
}
//层次遍历
void queue_show(Tree_t *root)
{
    lqueue_t *lq=create_lqueue();//创建队链
    enlqueue(lq,root);//二叉树的根节点入队
    while(lqueue_is_empty(lq)!=1)
    {
        Tree_t *root=delqueue(lq);//将根节点出队
        printf("%d ",root->data);
        //判断根节点后面有没有左右孩子节点,如果有将他们入队。
        if(root->lchild!=NULL)
            enlqueue(lq,root->lchild);
        if(root->rchild!=NULL)
            enlqueue(lq,root->rchild);
    }
}

队链代码,具体参考如下:

linkqueue.h

#ifndef LINKQUEUE_H
#define LINKQUEUE_H
#include "tree.h"
typedef Tree_t* data_t;
typedef struct node{
    data_t data;
    struct node *next;
}node_t;
typedef struct lqueue{

    node_t *front;
    node_t *rear;
}lqueue_t;

lqueue_t *create_lqueue();
int lqueue_is_empty(lqueue_t *lq);
int enlqueue(lqueue_t *lq,data_t  data);
 data_t delqueue(lqueue_t *lq);
void printf_lqueue(lqueue_t *lq);
void clear_lqueue(lqueue_t *lq);
void destroy_lqueue(lqueue_t **lq);
#endif 

linkqueue.c

#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.h"
#include "tree.h"

//创建队链
lqueue_t *create_lqueue()
{
    node_t *head=(node_t *)malloc(sizeof(node_t));
        if(head==NULL)
        return NULL;
        head->next=NULL;
    lqueue_t *lq=(lqueue_t *)malloc(sizeof(lqueue_t));
        if(lq==NULL)
            return NULL;
        lq->front=head;
        lq->rear=lq->front;
        return lq;
}
//判空
int lqueue_is_empty(lqueue_t *lq)
{
    if(lq!=NULL)
        return (lq->front==lq->rear ? 1:0);
    else
        return -1;
}
//入队
int enlqueue(lqueue_t *lq,data_t data)
{
    if(lq==NULL)
       return -1;
    node_t *new=(node_t *)malloc(sizeof(node_t));
    new->data=data;
    new->next=NULL;
    lq->rear->next=new;
    lq->rear=new;
    return 0;
}
//出队
data_t delqueue(lqueue_t *lq)
{
    if(lqueue_is_empty(lq))
        return NULL;
    node_t *p=lq->front->next;
    lq->front->next=p->next;
    Tree_t* data=p->data;
    free(p);
    p=NULL;
    if(lq->front->next==NULL)
    {
        lq->rear=lq->front;
    }
    return data;
}
void clear_lqueue(lqueue_t *lq)
{
    node_t *p=lq->front->next;
    node_t *q=NULL;
    lq->front->next=NULL;
    while(p!=NULL)
    {
        q=p->next;
        free(p);
        p=q;
    }
}
void destroy_lqueue(lqueue_t **lq)
{
    free(*lq);
    *lq=NULL;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值