二叉树必学算法(二):二叉树的先序中序后序遍历 - 非递归

二叉树的先序中序后序遍历 - 非递归


/**
 * title: 二叉树的前序中序后序遍历 - 非递归算法
 * author: Whywait
 */

void PreOrder(BiTree T) {
    InitStack(S);                   //initalize a stack named S
    BiTree p = T;                   //the traversing pointer
    while (p || !IsEmpty(S)) {      //while the p is not NULL OR S is not empty
        if (p) {                    //always go to the left branch
            visit(p);               //visit the p
            push(S, p);             //push the p node into the stack named S
            p = p->lchild;          //move pointer named p to its left child
        }
        else {                      //pop and go into its right tree
            pop(S, p);              //pop,and p is the node poping just now
            p = p->rchild;          //move pointer p to its right child
        }
    }
}

void InOrder(BiTree T) {
    InitStack(S);                   //initalize a stack named S
    BiTree p = T;                   //the traversing pointer
    while (p || !IsEmpty(S)) {      //while the p is not NULL OR S is not empty
        if (p) {                    //always go to the left branch
            push(S, p);             //push the p node into the stack named S
            p = p->lchild;          //move pointer named p to its left child
        }
        else {                      //pop and go into its right tree
            pop(S, p);              //pop,and p is the node poping just now
            visit(p);               //visit the p
            p = p->rchild;          //move pointer p to its right child
        }
    }
}

void PostOrder(BiTree T) {
    InitStack(S);                   //Initalize a stack named S
    p = T;                          //p is the traversing pointer
    r = NULL;                       //r is the assisting pointer
    while (p || !isEmpty(S)) {      //if p is not NULL OR S is not empty
        if (p) {
            push(S, p);             //push p into the Stack named S
            p = p->lchild;          //move pointer named p to its left child
        }
        else {
            GetTop(S, p);           //get the top node of stack named S
            if (p->rchild && p->rchild != r) {
                p = p->rchild;      //move pointer named p to its right child
                push(S, p);         //push the node into the stack
                p = p->lchild;      //move pointer to its left child
            }
            else {
                pop(S, p);          //pop the node on the top of the stack
                visit(p->data);     //visit p
                r = p;              //memorize the node recently visited
                p = NULL;           //empty p
            }
        }
    }
}

/**
 * the analysis of the postorder traversal:
 *  1. push the root into the stack;
 *  2. search along the left subtree untill there's no more left subtree,
 *  but it is not the time to pop and visit. If it has a right subtree, we
 *  need to do the same thing as we do before -- searching along the left
 *  subtree.
 *  3. until step 2 cannot be done anymore.
 *  4. Only if the top stack element has no right child OR its right child has
 *  been visited just now, it can be visited and poping.

 *  if you find it hard to understand, 
        simulate the traversing process in paper will helps you a lot.
 **/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AuthurLEE

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

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

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

打赏作者

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

抵扣说明:

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

余额充值