First, understand two concepts:
1. Deep traversal includes three types: Pre-and Post-order traversal;
2. The breadth-first traversal is hierarchical traversal.
PS:
Pre-and post-order traversal. Recursive traversal is easy to understand;
If we use a non-recursive method, we should first consider using the stack structure to control the entire process, because recursion is also implemented using the stack;
In the non-recursive mode of forward and backward traversal, the non-recursive mode of backward traversal is comparatively more complex.
Directly run the Code:
# Include "stdlib. H "# include <iostream >#include <stack >#include <queue> using namespace STD; struct binarytreenode {int value; binarytreenode * leftchild; binarytreenode * rightchild ;}; /* perform the following steps to traverse the recursion mode in the forward order: 1. process the current node first. after recursion, the left branch is processed. 3. recursive processing right branch */void preorderrecursive (binarytreenode * parent) {If (null = parent) return; cout <parent-> value <""; preorderrecursive (parent-> leftchild); preorderrecursive (parent-> rightchild);}/* traverse non-recursive vertices in the forward order Note: The nodestack stack is used to record nodes that are not traversed, and the left and right nodes are not traversed. Therefore, the traversal end condition is that the stack is empty (When can the stack be output (traversed), because it is a sequential traversal, you only need to output the result at the top of the stack,) 1. if the stack is not empty, the top element of the stack is pushed out of the stack and the right left child is pushed to the stack in sequence. This Order ensures that the first parent, the last left child, and the last right child traverse the First Order */void preorderstack (binarytreenode * root) {If (null = root) return; stack <binarytreenode *> nodestack; binarytreenode * pnode = NULL; nodestack. push (Root); While (! Nodestack. Empty () {pnode = nodestack. Top (); nodestack. Pop (); cout <pnode-> value <""; if (null! = Pnode-> rightchild) nodestack. Push (pnode-> rightchild); If (null! = Pnode-> leftchild) nodestack. push (pnode-> leftchild);}/* perform sequential traversal and Recursion: 1. recursively process the left branch 2. reprocess the current node. 3. finally, recursively process the right branch */void inorderrecursive (binarytreenode * parent) {If (null = parent) return; inorderrecursive (parent-> leftchild ); cout <parent-> value <"; inorderrecursive (parent-> rightchild);}/* indicates that the node stack records nodes that have not been traversed in a non-recursive manner, the left and right nodes are not traversed. The traversal end condition is that the stack is empty and the current node is invalid. (When can I get the stack output? It must have been processed by the left branch. Only the top stack can get the stack output, and the right child node can be processed immediately after the stack is released.) 1. before traversing the current node in the middle order, all the left nodes should be traversed. So the first step is to find the leftmost node in a loop and press the stack in sequence until the leftmost node (no left child) is found. the middle order is (left middle right). Since there are no left children, you can directly output the current node (on the top of the stack) 3. then process the right child of the current node */void inorderstack (binarytreenode * root) {If (null = root) return; stack <binarytreenode *> nodestack; binarytreenode * pnode = root; while (null! = Pnode |! Nodestack. Empty () {While (null! = Pnode) {nodestack. push (pnode); pnode = pnode-> leftchild;}/* If (nodestack. empty () return; this check is not required. if the outer while condition is pnode, it will enter the inner while, then nodestack is certainly not empty; 2. if the outer while condition is nodestack, this check is not required. */Pnode = nodestack. top (); cout <pnode-> value <""; nodestack. pop (); pnode = pnode-> rightchild;}/* perform post-order traversal and Recursion: 1. recursively process the left branch 2. recursively process the right Branch 3. finally, process the current node */void postorderrecursive (binarytreenode * parent) {If (null = parent) return; postorderrecursive (parent-> leftchild); postorderrecursive (parent-> rightchild ); cout <parent-> value <";}/* perform a post-order traversal in non-recursive mode. The nodestack stack is used to record nodes that are not traversed, in addition, the Left or Right children of the node are not traversed. The traversal end condition is that the stack is empty. (When can I get the stack? 1. out of the left and right children; 2. if there are left and right children, and the left and right children have already been traversed and output, they can also go out of the stack) Use prenode to record the node 1 that has recently traversed the output. check whether the output conditions are met. If yes, the output is. 2. if this parameter is not met, it indicates that there are left or right children and the child is not traversed, after processing, let's talk about the current node */void postorderstack (binarytreenode * root) {If (null = root) return; stack <binarytreenode *> nodestack; binarytreenode * pnode = NULL; binarytreenode * prenode = NULL; nodestack. push (Root); While (! Nodestack. Empty () {pnode = nodestack. Top (); If (null = pnode-> leftchild & null = pnode-> rightchild) | (prenode! = NULL & (prenode = pnode-> leftchild | prenode = pnode-> rightchild) {cout <pnode-> value <""; nodestack. pop (); prenode = pnode;} else {If (null! = Pnode-> rightchild) nodestack. Push (pnode-> rightchild); If (null! = Pnode-> leftchild) nodestack. push (pnode-> leftchild) ;}}/* width first traversal. Hierarchical traversal step: the queue is used to record nodes that have not been traversed, in addition, the Left or Right children of the node are not traversed. If the queue is empty, the traversal ends. (Think about hierarchical traversal, that is, the final output of the grandchildren at the same layer, the output of the uncles at the same layer, and the output of the grandfathers at the same layer. The longer the generations, the more advanced the output; the same layer means that the same generation must be adjacent, and the queue can satisfy) 1. output the node of the team header; 2. send the sons of the first node of the team to the end of the team to queue (because the father-in-law is in the queue, therefore, it is ensured that the sons of the elders are also in the queue) */void breadthfirst (binarytreenode * root) {If (null = root) return; queue <binarytreenode *> nodequeue; binarytreenode * pnode = NULL; nodequeue. push (Root); While (! Nodequeue. Empty () {pnode = nodequeue. Front (); cout <pnode-> value <""; nodequeue. Pop (); If (null! = Pnode-> leftchild) nodequeue. Push (pnode-> leftchild); If (null! = Pnode-> rightchild) nodequeue. push (pnode-> rightchild) ;}// test int main () {// empty tree binarytreenode * nulltree = NULL; // single-node tree binarytreenode singlenode; singlenode. value = 1; singlenode. leftchild = NULL; singlenode. rightchild = NULL; // left decision tree binarytreenode leftsidetree; leftsidetree. value = 1; binarytreenode leftsidenode2; leftsidenode2.value = 2; binarytreenode leftsidenode3; leftsidenode3.value = 3; leftsidetree. leftchild = & leftsidenode2; leftsidetree. rightchild = NULL; shard = & leftsidenode3; leftsidenode2.rightchild = NULL; leftsidenode3.rightchild = NULL; shard = NULL; // right decision tree binarytreenode rightsidetree; rightsidetree. value = 1; binarytreenode rightsidenode2; rightsidenode2.value = 2; binarytreenode rightsidenode3; rightsidenode3.value = 3; rightsidetree. leftchild = NULL; rightsidetree. rightchild = & rightsidenode2; bytes = NULL; rightsidenode2.rightchild = & rightsidenode3; bytes = NULL; // full Binary Tree binarytreenode completebinarytree; completebinarytree. value = 1; required btnode2; btnode2.value = 2; binarytreenode btnode3; btnode3.value = 3; required btnode4; btnode4.value = 4; required btnode5; btnode5.value = 5; required btnode6; btnode6.value = 6; binarytreenode btnode7; btnode7.value = 7; completebinarytree. leftchild = & btnode2; completebinarytree. rightchild = & btnode5; region = & btnode3; btnode2.rightchild = & btnode4; btnode3.leftchild = NULL; btnode3.rightchild = NULL; region = NULL; btnode4.rightchild = NULL; region = & btnode6; btnode5.rightchild = & btnode7; region = NULL; btnode6.rightchild = NULL; region = NULL; btnode7.rightchild = NULL; cout <Endl <"nulltree"; cout <Endl <"preorderrecursive: "; preorderrecursive (nulltree); cout <Endl <" preorderstack: "; preorderstack (nulltree); cout <Endl <" inorderrecursive: "; inorderrecursive (nulltree ); cout <Endl <"inordestack:"; inorderstack (nulltree); cout <Endl <"postorderrecursive:"; postorderrecursive (nulltree ); cout <Endl <"postorderstack:"; postorderstack (nulltree); cout <Endl <"breadthfirst:"; breadthfirst (nulltree); cout <Endl; cout <Endl <"singletree"; cout <Endl <"preorderrecursive:"; preorderrecursive (& singlenode); cout <Endl <"preorderstack :"; preorderstack (& singlenode); cout <Endl <"inorderrecursive:"; inorderrecursive (& singlenode); cout <Endl <"inordestack:"; inorderstack (& singlenode ); cout <Endl <"postorderrecursive:"; postorderrecursive (& singlenode); cout <Endl <"postorderstack:"; postorderstack (& singlenode ); cout <Endl <"breadthfirst:"; breadthfirst (& singlenode); cout <Endl <"leftsidetree "; cout <Endl <"preorderrecursive:"; preorderrecursive (& leftsidetree); cout <Endl <"preorderstack:"; preorderstack (& leftsidetree ); cout <Endl <"inorderrecursive:"; inorderrecursive (& leftsidetree); cout <Endl <"inordestack:"; inorderstack (& leftsidetree ); cout <Endl <"postorderrecursive:"; postorderrecursive (& leftsidetree); cout <Endl <"postorderstack:"; postorderstack (& leftsidetree ); cout <Endl <"breadthfirst:"; breadthfirst (& leftsidetree); cout <Endl <"rightsidetree "; cout <Endl <"preorderrecursive:"; preorderrecursive (& rightsidetree); cout <Endl <"preorderstack:"; preorderstack (& rightsidetree ); cout <Endl <"inorderrecursive:"; inorderrecursive (& rightsidetree); cout <Endl <"inordestack:"; inorderstack (& rightsidetree ); cout <Endl <"postorderrecursive:"; postorderrecursive (& rightsidetree); cout <Endl <"postorderstack:"; postorderstack (& rightsidetree ); cout <Endl <"breadthfirst:"; breadthfirst (& rightsidetree); cout <Endl <"completebinarytree "; cout <Endl <"preorderrecursive:"; preorderrecursive (& completebinarytree); cout <Endl <"preorderstack:"; preorderstack (& completebinarytree ); cout <Endl <"inorderrecursive:"; inorderrecursive (& completebinarytree); cout <Endl <"inordestack:"; inorderstack (& completebinarytree ); cout <Endl <"postorderrecursive:"; postorderrecursive (& completebinarytree); cout <Endl <"postorderstack:"; postorderstack (& completebinarytree ); cout <Endl <"breadthfirst:"; breadthfirst (& completebinarytree); cout <Endl; System ("pause"); Return 0 ;}
All kinds of corrections and exchanges are welcomed!
Binary tree traversal, deep finite traversal, breadth-first traversal, pre-order, middle-order, and later traversal, and hierarchical Traversal