二叉树的遍历(递归与非递归)

#include <bits/stdc++.h>
#include <vector>
#include <map>

#define x first
#define y second
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define sz(v) size(v)

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 510;

typedef struct Tree
{
    int data;
    struct Tree *lchild;
    struct Tree *rchild;
}Tree, *BitTree;

BitTree stk[N];
BitTree que[N];

int tt, tt1, tt2;
int front, rear = -1;

BitTree create() //二叉树的创建
{
    int op;
    BitTree t;
    scanf("%d", &op);
    if(op == -1) return NULL;

    t = (BitTree)malloc(sizeof(Tree));
    t->data = op;
    printf("请输入%d的左子树:", op);
    t->lchild = create();
    printf("请输入%d的右子树:", op);
    t->rchild = create();
    return t;
}
//二叉树的递归遍历
void xianxu(BitTree t) //先序遍历
{
    if(t == NULL) return;

    printf("%d", t->data);

    xianxu(t->lchild);
    xianxu(t->rchild);
}

void houxu(BitTree t) //后续遍历
{
    if(t == NULL) return;

    houxu(t->lchild);
    houxu(t->rchild);

    printf("%d", t->data);
}

void zhongxu(BitTree t) //中序遍历
{
    if(t == NULL) return;

    zhongxu(t->lchild); //递归遍历左子树

    printf("%d", t->data);

    zhongxu(t->rchild); //递归遍历右子树
}

//二叉树的非递归遍历
void xianxu2(BitTree t) //压入栈的顺序为右左根
{
    if(t == NULL) return;
    stk[++tt] = t;

    while(tt > 0)
    {
        BitTree temp = stk[tt--];
        printf("%d\n", temp->data);
        if(temp->rchild != NULL) stk[++tt] = temp->rchild;
        if(temp->lchild != NULL) stk[++tt] = temp->lchild;
    }

}


void zhongxu2(BitTree t) 
{
    if(t == NULL) return;
    BitTree temp = t;
    while(tt > 0 || temp != NULL)
    {
        if(temp != NULL)
        {
            stk[++tt] = temp;
            temp = temp->lchild;
        }
        else
        {
            temp = stk[tt--];
            printf("%d\n", temp->data);
            temp = temp->rchild;
        }
    }
}

void houxu2(BitTree t)
{
    if(t == NULL) return;

    BitTree stk1[N];
    BitTree stk2[N];

    stk1[++tt1] = t;
    while(tt1 > 0)
    {
        BitTree temp = stk1[tt1--];
        stk2[++tt2] = temp;
        if(temp->lchild != NULL) stk1[++tt1] = temp->lchild;
        if(temp->rchild != NULL) stk1[++tt1] = temp->rchild;

    }
    while(tt2 > 0)
    {
        printf("%d\n", stk2[tt2--]->data);
    }
}

//二叉树的层序遍历
void cengxu2(BitTree t)
{
    if(t == NULL) return;

    que[++rear] = t;

    while(front <= rear)
    {
        BitTree temp = que[front++];

        printf("%d ", temp->data);
        if (temp->lchild != NULL)
        {
            que[++rear] = temp->lchild;
        }

        if (temp->rchild != NULL)
        {
            que[++rear] = temp->rchild;
        }
    }
}

int main()
{

    BitTree t = create();
    
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Eliauk-GX

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

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

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

打赏作者

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

抵扣说明:

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

余额充值