How to traverse a binary tree with a stack

Excerpt: https://blog.csdn.net/cocoiehl/article/details/80959143

 

      https://bbs.csdn.net/topics/391882020

(Just for the convenience of their own review)

 

 

 

Generally, when we traverse the binary tree using a recursive, relatively simple, using recursive code is as follows:

/ **************** 
After based on recursive preorder, 
******************************************************** / 
void PostOrderTraverse (NODEs PROOT *) { 
	IF (PROOT == NULL) { 
		return; 
	} 
	the else { 
		PostOrderTraverse (pRoot-> pLeft); 
		PostOrderTraverse (pRoot-> Pright); 
		the printf ( "% C", pRoot-> chValue); 
	} 
	return; 
}

By changing the position of printf statements will be achieved in the preamble and preorder.

Let us look at how to achieve binary tree traversal stack-based, can be divided into binary tree root, left, right three parts
order preorder traversal as root, left, right;
the order of traversal sequence is left, root, right;
postorder order of left, right, root;
preorder traversal order and discussed before, obviously be achieved by the following procedure
1. constantly left subtree stack, until the left subtree is empty
2. Keep the stack until the right subtree of the stack element is not empty
3. If the current stack is not empty or not empty root node, repeat steps 1 and 2
preorder in step # 1 is the root of the tree before the stack output, while the sequence is popped tree root node in step 2 of the output
code is as follows:

/ **************** 
traversal stack implementation based on the preamble sequence and 
******************************************************** / 
void OrderTraverseByStack (NODEs * PROOT ) { 
	NODEs Stack * [1000]; 
	int Top = 0; 
	! the while (Top> 0 || PROOT = NULL) { 
		PROOT NULL =;; for (! = pRoot- PROOT> pLeft) { 
			Stack [Top ++] = PROOT; 
			order traversal // 
			// the printf ( "% C", pRoot-> chValue); 
		} 
		for (; PROOT == NULL && Top> 0; PROOT = pRoot-> Pright) { 
			PROOT = Stack [- Top]; 
			// preorder 
			// the printf ( "% C", pRoot-> chValue); 
		} 
	} 
}

Finally, based on how the stack after traversing Binary Tree to achieve it? By can be found observing
the order preorder traversal as root, left, right;
the order after order traversal of left, right, root;
the preorder traversal of the left and right exchanged in reverse output, we can achieve post-order traversal!
Therefore, we can modify the code to achieve the above traversal order, as follows:
1, the preorder traversal code left and right reversed, and data is present in the stack S.
2, after a preorder traversal, the data in the stack S, the stack one by one and can be printed.
code show as below:

/****************
基于栈实现后序遍历
*****************/
void PostOrderTraverseByStack(NODE* pRoot) {
	NODE *StackA[1000],*StackB[1000];
	int topA = 0,topB = 0;
	while (topA > 0 || pRoot != NULL) {
		for (; pRoot != NULL;pRoot = pRoot->pRight) {
			StackA[topA++] = pRoot;
			StackB[topB++] = pRoot;
		}
		for (; pRoot == NULL&&topA > 0; pRoot = pRoot->pLeft) {
			pRoot = StackA[--topA];
		}
	}
	while (topB > 0) {
		printf("%c", StackB[--topB]->chValue);
	}
}


Here is the test code:

#include<stdio.h>
#include<stdlib.h>
 
#define TREELEN 6
 
struct NODE {
    NODE* pLeft;
    NODE* pRight;
    char chValue;
};
 
void ReBuild(char* pPreOrder,char* pInOrder,int nTreeLen,NODE** pRoot) {
    if (pPreOrder == NULL || pInOrder == NULL) {
        return;
    }
    NODE* pTemp =(NODE* )malloc(sizeof(NODE));
    pTemp->chValue = *pPreOrder;
    pTemp->pLeft = NULL;
    pTemp->pRight = NULL;
    if (*pRoot == NULL) {
        *pRoot = pTemp;
    }
    if (nTreeLen == 1) {
        return;
    }
    char* pOrgInOrder = pInOrder;
    char* pLeftEnd = pInOrder;
    int nTempLen = 0;
    while (*pPreOrder != *pLeftEnd) {
        if (pPreOrder == NULL || pLeftEnd == NULL) {
            return;
        }
        nTempLen++;
        if (nTempLen > nTreeLen) {
            break;
        }
        pLeftEnd++;
    }
    int nLeftLen = 0;
    nLeftLen = (int)(pLeftEnd - pOrgInOrder);
    int nRightLen = 0;
    nRightLen = nTreeLen - nLeftLen - 1;
    if (nLeftLen > 0) {
        ReBuild(pPreOrder+1, pInOrder, nLeftLen, &((*pRoot)->pLeft));
    }
    if (nRightLen > 0) {
        ReBuild (pPreOrder + + nLeftLen. 1, pInOrder nLeftLen + + 1'd, nRightLen, & ((* PROOT) -> Pright)); 
    ! the while (Top> 0 || PROOT = NULL ) {
    }
} 
 
/ **************** 
after recursive traversal order based, 
******************************************************** / 
void PostOrderTraverse (NODEs PROOT *) { 
    IF (PROOT == NULL) { 
        return; 
    } 
    the else { 
        PostOrderTraverse (pRoot-> pLeft); 
        PostOrderTraverse (pRoot-> Pright); 
        the printf ( "% C", pRoot-> chValue); 
    } 
    return; 
} 
 
/ ** ************** 
based stack implementation preamble and preorder 
******************************************************** / 
void OrderTraverseByStack (NODEs PROOT *) { 
    NODEs Stack * [1000]; 
    int Top = 0; 
            Stack [Top ++] = PROOT; 
            // before preorder
        for (; = NULL PROOT;! = pRoot- PROOT> pLeft) { 
            // the printf ( "% C", pRoot-> chValue); 
        } 
        for (; PROOT == NULL && Top> 0; PROOT = pRoot-> Pright) { 
            stack = PROOT [- Top]; 
            // preorder 
            the printf ( "% C", pRoot-> chValue); 
        } 
    } 
} 
 
/ ********************************************************* 
based stack to achieve postorder 
******************************************************** / 
void PostOrderTraverseByStack (NODEs PROOT *) { 
    NODEs StackA * [1000], * StackB [1000]; 
    int TOPA = 0, 0 = topB ; 
    the while (! TOPA> 0 || PROOT = NULL) { 
        for (; = NULL PROOT;! = pRoot- PROOT> Pright) { 
            StackA [TOPA ++] = PROOT; 
            StackB [topB ++] = PROOT; 
        } 
        for (; PROOT == NULL && TOPA> 0;pRoot = pRoot->pLeft) {
            = StackA PROOT [- TOPA]; 
        } 
    } 
    the while (topB> 0) { 
        the printf ( "% C", StackB [- topB] -> chValue); 
    } 
} 
 
int main () { 
    NODEs PROOT * = NULL; 
    char pre [TREELEN] = { 'A', 'B', 'D', 'E', 'C', 'F'}; 
    char in [TREELEN] = { 'D', 'B', 'E', 'a', 'F', 'C'}; 
    ReBuild (pre, in, TREELEN, & PROOT); 
    the printf ( "\ preorder result n by the stack to achieve: \ n"); 
    OrderTraverseByStack (PROOT); 
    the printf ( "\ n postorder results achieved by recursively: \ n"); 
    PostOrderTraverse (PROOT); 
    the printf ( "\ n postorder results achieved by the stack: \ n"); 
    PostOrderTraverseByStack(pRoot);
    getchar();
    return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/myhnb/p/11537506.html
Recommended