二叉树的四种遍历,包含递归和非递归实现

#pragma once
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct binary_tree
{
    binary_tree *lchild = 0;
    binary_tree *rchild = 0;
    int data;
};
/*递归写法*/
//先序遍历:root,lchild,rchild
void pre_print(binary_tree *node)
{
    if (node)
    {
        cout << node->data << " ";
        pre_print(node->lchild);
        pre_print(node->rchild);
    }
}

//中序遍历:lchild,root,rchild
void mid_print(binary_tree *node)
{
    if (node)
    {
        mid_print(node->lchild);
        cout << node->data << " ";
        mid_print(node->rchild);
    }
}

//后序遍历:lchild,rchild,root
void last_print(binary_tree *node)
{
    if (node)
    {
        last_print(node->lchild);
        last_print(node->rchild);
        cout << node->data << " ";
    }
}

//层序遍历:从上到下,从左到右
void lev_print(binary_tree *node)
{
    if (!node)
        return;
    queue<binary_tree *> q;
    binary_tree *next;
    q.push(node);
    while (!q.empty())
    {
        next = q.front();
        q.pop();
        cout << next->data << " ";
        if (next->lchild)
            q.push(next->lchild);
        if (next->rchild)
            q.push(next->rchild);
    }
    cout << endl;
}

/*非递归写法*/

//先序遍历
void pre_print_stack(binary_tree *node)
{
    if (!node)
        return;
    stack<binary_tree *> s;
    s.push(node);
    binary_tree *next;
    while (!s.empty())
    {
        next = s.top();
        s.pop();
        cout << next->data << " ";
        if (next->rchild)
            s.push(next->rchild);
        if (next->lchild)
            s.push(next->lchild);
    }
    cout << endl;
}

//中序遍历
void mid_print_stack(binary_tree *node)
{
    if (!node)
        return;
    stack<binary_tree *> s;
    s.push(node);
    binary_tree *next = node->lchild;
    while (!s.empty() || next) //需要加上next的条件是因为当根节点被弹出后如果根节点的右子树非空,就要进入右子树
    {
        if (next) //next不是从s中弹出的节点
        {
            if (next->lchild) //左子树非空,next入栈
            {
                s.push(next);
                next = next->lchild;
            }
            else //左子树为空,输出next,next指向右子树
            {
                cout << next->data << " ";
                next = next->rchild;
            }
        }
        //右子树为空,弹出栈顶节点
        else //从栈中弹出的节点不需要检查左子树,因为左子树已经被输出
        {
            cout << s.top()->data << " ";
            //如果从栈中弹出的节点的右子树非空,那么next就指向它
            //否则继续弹出栈中节点
            next = s.top()->rchild;
            s.pop();
        }
    }
    cout << endl;
}

//中序遍历的另一种写法
void mid_print_stack_2(binary_tree *node)
{
    if (!node)
        return;
    stack<binary_tree *> s;
    binary_tree *next = node;
    while (!s.empty() || next) 
    {
        while (next)
        {
            s.push(next);
            next = next->lchild;
        }
        if (!s.empty())
        {
            next = s.top();
            s.pop();
            cout << next->data << " ";
            next = next->rchild;
        }
    }
    cout << endl;
}

//后序遍历
void last_print_stack(binary_tree *node)
{
    if (!node)
        return;
    stack<binary_tree *> s1;
    stack<int> s2;
    binary_tree *next = node->lchild;
    s1.push(node);
    s2.push(1);
    int flag;//记录是否要访问右子树,1表示要,0表示不需要
    while(!s1.empty())
    {
        while(next)
        {
            s1.push(next);
            s2.push(1);
            next = next->lchild;
        }
        next=s1.top();
        flag=s2.top();
        while(!flag)//将所有不需要访问右子树的节点输出
        {
            cout<<next->data<<" ";
            s1.pop();
            s2.pop();
            if(!s1.empty())
            {
                next=s1.top();
                flag=s2.top();
            }
            else break;
        }
        if(!s1.empty())
        {
            next=next->rchild;
            s2.pop();
            s2.push(0);
        }
    }
    cout<<endl;
}```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值