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

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


/**
 * 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.
 **/

你可能感兴趣的:(算法,二叉树,算法,数据结构,stack)