These three kinds of common flat-calendar way, is an examination of the postgraduate examination and other occasions often encountered, in this to do a summary.
1, the pre-sequence traversal is relatively simple: with the pointer P point to the root node, if the P!=null and the stack is not empty, then directly access the node, and the node's right child into the stack, while the pointer P left the child to move.
2, the sequence of the flat calendar: With the pointer P point to the root node, if the P!=null and the stack is not empty, the current node into the stack, and the pointer P left the child move, the stack is the pointer to the current node of the right child.
3, after the order of the flat calendar is relatively complex: you need to set up a secondary stack, to identify whether the node is the second out of the stack, only the second out of the stack node can be accessed.
Concrete implementation will not wordy, directly on the code it!
#include <stack> #include <queue>using namespace std;//Binary tree structstruct binarytreenode{ Binarytreenode (): M_nvalue (0), m_pleft (null), m_pright (null) {}int m_nvalue; binarytreenode* M_pleft; binarytreenode* m_pright;};/ /create nodes by hierarchy binarytreenode* Createbinarytree () {int A; Binarytreenode *t = null;queue<binarytreenode*> queue_nodes;cout << "Enter The value of the current node value, (s Pace to the leaf node). "<< endl;if (Cin >> a && a! =-1) {t = new Binarytreenode;t->m_nvalue = A;qu Eue_nodes.push (t);} while (!queue_nodes.empty ()) {Binarytreenode *tnode = Queue_nodes.front (); Queue_nodes.pop (); if (Cin >> A & & A! =-1) {Binarytreenode *newleftnode = new Binarytreenode;newleftnode->m_nvalue = A;tnode->m_pleft = NewLeftN Ode;queue_nodes.push (Newleftnode);} if (Cin >> a && a! =-1) {Binarytreenode *newrightnode = new Binarytreenode;newrightnode->m_nvalue = A;tno De->m_pright = Newrightnode;queue_nodes.push (newrightnode);}} return t;} void Preordertraverse (Binarytreenode *t, Void (*visit) (int)) {stack<binarytreenode*> stack_nodes; Binarytreenode *p = t;while (P | |!stack_nodes.empty ()) {if (p) {visit (p->m_nvalue); if (p->m_pright) stack_ Nodes.push (p->m_pright);p = P->m_pleft;} Else{p = Stack_nodes.top (); Stack_nodes.pop ();}}} void Inordertraverse (Binarytreenode *t, Void (*visit) (int)) {stack<binarytreenode*> stack_nodes; Binarytreenode *p = t;while (P | |!stack_nodes.empty ()) {if (p) {Stack_nodes.push (p);p = P->m_pleft;} Else{p = Stack_nodes.top (); Stack_nodes.pop (); visit (p->m_nvalue);p = P->m_pright;}}} void Postordertraverse (Binarytreenode *t, Void (*visit) (int)) {stack<binarytreenode*> stack_nodes;stack< Bool> stack_visited; A secondary stack that identifies whether the node has been stacked, only the second out-of-stack node can be accessed Binarytreenode *p = T;bool visited = false;while (P | |!stack_nodes.empty ()) {if (p) {STA Ck_nodes.push (P); The current node is in the stack Stack_visited.push (false); Corresponding secondary identifier in the stack p = p->m_pleft; Operation of the left child}else{p = Stack_nodes.top (); Stack_nodEs.pop (); visited = Stack_visited.top (); Stack_visited.pop (); if (!visited) {Stack_nodes.push (P); Stack_visited.push ( true);p = p->m_pright; For the first time out of the stack, jump to its right child}else{visit (p->m_nvalue);p = NULL;}}}
C + + implementation of two-fork-tree pre-order, middle-order, sequential non-recursive flat calendar