实现二叉树的各种遍历算法

实现二叉树的各种遍历算法
头文件+函数

#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef char ElemType;
typedef struct node
{
    ElemType data; 
    struct node *lchild; 
    struct node *rchild; 
}BTNode; 
/*-------------由括号表示串str创建二叉链b-----------------*/
static void create_btree(BTNode *&b, char *str)
{
    BTNode *p;
    BTNode *St[MAX_SIZE]; 
    int k;
    int j = 0;
    int top = -1; 
    char ch;

    b = NULL; 
    ch = str[j]; 
    while(ch != '\0') 
    {
        switch(ch)
        {
        case '(': 
            top++;
            St[top] = p;
            k = 1;
            break;
        case ')': 
            top--;
            break;
        case ',':
            k = 2;
            break;
        default:
            p = (BTNode *)malloc(sizeof(BTNode)); 
            p->data = ch;
            p->lchild = p->rchild = NULL;
            if(b == NULL) 
                b = p;
            else 
            {
                switch(k)
                {
                case 1:
                    St[top]->lchild = p;
                    break;
                case 2:
                    St[top]->rchild = p;
                    break;
                }
            }
            break;
        }
        j++;
        ch = str[j];
    }
}

/*--------------------------以括号表示法输出二叉树b----------------------*/
// "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"
static void disp_btree(BTNode *b)
{
    if(b != NULL)
    {
        printf("%c", b->data);
        if(b->lchild != NULL || b->rchild != NULL)
        {
            printf("("); 
            disp_btree(b->lchild); 
            if(b->rchild != NULL) 
                printf(",");
            disp_btree(b->rchild);
            printf(")"); 
        }
    }
}

/*--------------------------释放二叉树b的所有结点----------------------*/
static void destroy_btree(BTNode *&b) 
{
    if(b != NULL)
    {
        destroy_btree(b->lchild);
        destroy_btree(b->rchild);
        free(b);
    }
}

/*--------------------------二叉树b的层次遍历算法----------------------*/
static void trav_level(BTNode *b)
{
    BTNode *que[MAX_SIZE];
    int que_front, que_rear; 

    que_front = que_rear = 0;
    if(b != NULL)
        printf("%c ", b->data);
    que_rear++;
    que[que_rear] = b; 
    while(que_rear != que_front) 
    {
        que_front = (que_front + 1) % MAX_SIZE; 
        b = que[que_front]; 
        if(b->lchild != NULL) 
        {
            printf("%c ", b->lchild->data);
            que_rear = (que_rear + 1) % MAX_SIZE; 
            que[que_rear] = b->lchild;
        }
        if(b->rchild != NULL) 
        {
            printf("%c ", b->rchild->data);
            que_rear = (que_rear + 1) % MAX_SIZE; 
            que[que_rear] = b->rchild;
        }
    }
    printf("\n");
}

/*--------------------------二叉树b的先序遍历递归算法----------------------*/
static void pre_order(BTNode *b)
{
    if(b == NULL)
        return;

    printf("%c ", b->data); 
    pre_order(b->lchild);
    pre_order(b->rchild); 
}

/*--------------------------二叉树b的先序遍历非递归算法----------------------*/
static void pre_order1(BTNode *b)
{
    BTNode *p;
    BTNode *st[MAX_SIZE]; 
    int top = -1;

    if(b == NULL)
        return;

    top++;
    st[top] = b; 
    while(top > -1) 
    {
        p = st[top]; 
        top--;
        printf("%c ", p->data);
        if(p->rchild != NULL) 
        {
            top++;
            st[top] = p->rchild;
        }
        if(p->lchild != NULL) 
        {
            top++;
            st[top] = p->lchild;
        }
    }
    printf("\n");
}

/*--------------------------二叉树b的中序遍历递归算法----------------------*/
static void in_order(BTNode *b)
{
    if(b == NULL)
        return;

    in_order(b->lchild); 
    printf("%c ", b->data); 
    in_order(b->rchild); 
}

/*--------------------------二叉树b的中序遍历非递归算法----------------------*/
// A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))
static void in_order1(BTNode *b)
{
    BTNode *p;
    BTNode *st[MAX_SIZE];
    int top = -1;

    if(b == NULL)
        return;

    p = b; 
    while(top > -1 || p != NULL)
    {
        while(p != NULL) 
        {
            top++;
            st[top] = p;
            p = p->lchild;
        }
        if(top > -1)
        {
            p = st[top]; 
            top--;
            printf("%c ", p->data);
            p = p->rchild;
        }
    }
    printf("\n");
}
/*--------------------------二叉树b的后序遍历递归算法----------------------*/
static void post_order(BTNode *b)
{
    if(b == NULL)
        return;

    post_order(b->lchild); 
    post_order(b->rchild);
    printf("%c ", b->data); 
}

/*--------------------------二叉树b的后序遍历非递归算法----------------------*/
static void post_order1(BTNode *b)
{
    int top = -1; 
    BTNode *st[MAX_SIZE]; 
    BTNode *p;
    bool flag;

    if(b == NULL)
        return;
    do
    {
        while(b != NULL) 
        {
            top++;
            st[top] = b;
            b = b->lchild;
        }
        p = NULL; 
        flag = true; 
        while(top != -1 && flag)
        {
            b = st[top]; 
            if(b->rchild == p) 
            {
                printf("%c ", b->data); 
                top--;
                p = b; 
            }
            else
            {
                b = b->rchild; 
                flag = false; 
            }
        }
    }while(top != -1);

    printf("\n");
}

主函数

int main(int argc, char *argv[])
{
    BTNode *b;

    create_btree(b, "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
    printf("二叉树b:");
    disp_btree(b);
    printf("\n");
    printf("先序遍历序列:\n");
    printf("    递归算法:");
    pre_order(b);
    printf("\n");
    printf("  非递归算法:");
    pre_order1(b);
    printf("\n");
    
    printf("中序遍历序列:\n");
    printf("    递归算法:");
    in_order(b);
    printf("\n");
    printf("  非递归算法:");
    in_order1(b);
    printf("\n");
    
    printf("后序遍历序列:\n");
    printf("    递归算法:");
    post_order(b);
    printf("\n");
    printf("  非递归算法:");
    post_order1(b);
    printf("\n");
    
    printf("层次遍历序列:");
    trav_level(b);
    destroy_btree(b);

    return 0;
}

运行结果:
实现二叉树的各种遍历算法

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OLoyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值