二叉树遍历---非递归实现

本文详细探讨了如何使用非递归方式实现二叉树的前序、中序和后序遍历。通过迭代和栈操作,避免了递归带来的额外开销,提高了效率。同时,讲解了各个遍历方法的逻辑和步骤,帮助读者深入理解二叉树遍历的本质。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 二叉树的二叉链表存储表示
typedef int ElemType;

typedef struct BinaryTreeNode {
    ElemType data;
    struct BinaryTreeNode *lchild;
    struct BinaryTreeNode *rchild;
}BinaryTreeNode;
====================================================================================================================================
// 先序遍历
BinaryTreeNode * GoFarLeft(BinaryTreeNode *T, std::stack<BinaryTreeNode *> &st)
{
    while (T->lchild != NULL)
    {
        printf("%2d", T->data);
        st.push(T);
        T = T->lchild;
    }
    return T;
}
void PreOrderTraverse(BinaryTreeNode *T, std::stack<BinaryTreeNode *> &st)
{
    if (T != NULL)
    {
        BinaryTreeNode *t = GoFarLeft(T, st);
        bool flag = true;

        while (t)
        {
             if (flag)
             {
                 printf("%2d", t->data);
             }
            if (t->rchild != NULL)
            {
                 t = GoFarLeft(t->rchild, st);
                 flag = true;
             }
            else if (!st.empty())
            {
                  t = st.top();
                  st.pop();
                  flag = false; //上溯至父节点,而父节点之前已经被打印出
            }
            else
            {
                  t = NULL;
             }
       }
    }
}
====================================================================================================================================
// 中序遍历
BinaryTreeNode * GoFarLeft(BinaryTreeNode *T, std::stack<BinaryTreeNode *> &st)
{
    while (T->lchild != NULL)
    {
        st.push(T);
        T = T->lchild;
    }
    return T;
}
void InOrderTraverse(BinaryTreeNode *T, std::stack<BinaryTreeNode *> &st)
{
if (T != NULL)
{
    BinaryTreeNode *t = GoFarLeft(T, st);
    while (t)
   {
           printf("%2d", t->data);
           if (t->rchild != NULL)
           {
                   t = GoFarLeft(t->rchild, st);
           }
           else if (!st.empty())
           {
                  t = st.top();
                  st.pop();
           }
           else
            {
                    t = NULL;
            }
    }
}
}
====================================================================================================================================
// 后序遍历
struct SNode {
       BinaryTreeNode *p;
       bool rvisited;
       SNode(BinaryTreeNode *_p, bool _rvisited): p(_p), rvisited(_rvisited) {}
};

void GoFarLeft(BinaryTreeNode *T, std::stack<SNode> &st)
{
     while (T != NULL)
     {
            st.push(SNode(T, false));
            T = T->lchild;
     }
}
void LastOrderTraverse(BinaryTreeNode *T, std::stack<SNode> &st)
{
       if (T != NULL)
       {
             GoFarLeft(T, st);
             while (!st.empty())
             {
                    SNode t = st.top();
                    if (t.p->rchild == NULL || t.rvisited == true) // 结点t已‘最左’,如果右孩子为空或者右孩子已经访问,那么输出t
                    {
                             printf("%2d", t.p->data);
                             st.pop();
                    }
                   else
                   {
                            t.rvisited = true;
                            st.pop();
                            st.push(t);
                            GoFarLeft(t.p->rchild, st);
                   }
             }
       }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值