/* Recursive and non-recursive form *///***************void preOrder1 (binarytreenode* proot) {if (Proot==null) return for the back/hierarchy traversal in front of the two fork tree; cout<<proot->value; if (proot->left!=null) preOrder1 (proot->left); if (proot->right!=null) preOrder1 (proot->right);} void PreOrder2 (binarytreenode* proot) {stack<binarytreenode*> s; Binarytreenode *p=proot; if (proot==null) return; while (p!=null| |! S.empty ()) {while (p!=null) {cout<<p->value<< ""; S.push (P); p=p->left; } if (!s.empty ()) {p=s.top (); S.pop (); p=p->right; }}}void PreOrder2 (binarytreenode* proot) {if (proot==null) return; Stack<binarytreenode*> s; S.push (root); Binarytreenode* p; while (!s.empty ()) {p = s.top (); cout<<p->data;//Traverse root node s.pop (); if (p>right) {s.push (p->right); Stack the right subtree first} iF (p->left) {s.push (p->left); Then the left subtree is pressed to stack}}}//*****************//in the sequence traversal void InOrder1 (binarytreenode* proot) {if (proot==null) return; if (proot->left!=null) inOrder1 (proot->left); cout<<proot->value; if (proot->right!=null) inOrder1 (proot->right);} void InOrder2 (binarytreenode* proot) {stack<binarytreenode*> s; Binarytreenode *p=proot; if (proot==null) return; while (p!=null| |! S.empty ()) {while (p!=null) {s.push (P); p=p->left; } if (!s.empty ()) {p=s.top (); cout<<p->value<< ""; S.pop (); p=p->right; }}}//*****************//post-traversal of void PostOrder1 (binarytreenode* proot) {if (proot==null) return; PostOrder1 (Proot->left); PostOrder1 (Proot->right); cout<<proot->value<< "";} void PostOrder2 (binarytreenode* proot) {stack<binarytrEenode*> s; Binarytreenode *cur; Binarytreenode *pre=null;//Records the node of the previous output s.push (proot);//root node into the stack while (!s.empty ()) {cur=s.top (); if ((cur->left==null&&cur->right==null) | | | (pre!=null&& (pre==cur->left| | Pre==cur->right)) {//left child and right child are both empty, or the left child or right child of the current node has traversed the cout<<cur->value<< " "; S.pop (); Pre=cur; } else {if (cur->right!=null) S.push (cur->right); if (cur->left!=null) S.push (cur->left); }}}//*****************//hierarchy traversal, using the queue void Printfromtoptobottom (binarytreenode* proot) {if (Proot = = NULL) return; Deque<binarytreenode *> Dequetreenode; Dequetreenode.push_back (Proot); while (Dequetreenode.size ()) {Binarytreenode *pnode = Dequetreenode.front (); Dequetreenode.pop_front (); cout<<pnode->m_nvalue<< ""; if (PNODE->m_pleft) Dequetreenode.push_back (pnode->m_pleft); if (pnode->m_pright) dequetreenode.push_back (pnode->m_pright); }}//Depth First Traversal ~ First order traversal void Dfs (binarytreenode* root) {stack<binarytreenode* *> nodestack; Nodestack.push (root); Node *node; while (!nodestack.empty ()) {node = Nodestack.top (); cout<<node->data;//Traverse root node Nodestack.pop (); if (node->rchild) {Nodestack.push (node->rchild); Press the right subtree first to stack} if (node->lchild) {Nodestack.push (node->lchild); Re-stack the left subtree}}}//breadth-First traversal ~ hierarchy traversal void BFs (binarytreenode* root) {queue<binarytreenode* *> nodequeue; Nodequeue.push (root); Node *node; while (!nodequeue.empty ()) {node = Nodequeue.front (); Nodequeue.pop (); cout<<node->data;//traverse the root node if (node->lchild) {Nodequeue.push (node->lchild); Queue the left subtree first} if (Node->rchild) {nodequEue.push (Node->rchild); Queue the right subtree again}}}
Recursive and non-recursive forms of back/hierarchy traversal in front of a binary tree (c + +)