Today, we reviewed the recursive and non-recursive algorithms for the binary tree's pre-sequence traversal, the middle sequence traversal, the post-order traversal, and, by the way, records:
//TreeTest.h#include <iostream>structtreenode{intvalue; TreeNode*Leftchild; TreeNode*Rightchild; voidprint () {printf ("%d", value); }};classmytree{Private: TreeNode*M_root; TreeNode* Create (treenode*&root); voidDestroy (treenode*root); voidPreorderrecursive (treenode*root); voidInorderrecursive (treenode*root); voidPostorderrecursive (treenode*root); voidLevelorderrecursive (treenode*root); Public: Mytree (); ~mytree (); TreeNode* Gettreenode (intvalue); voidCreatetreeforpreorder (); voidpreorderrecursive (); voidinorderrecursive (); voidpostorderrecursive (); voidlevelorderrecursive (); //non-recursive traversal voidpreorderunrecursive (); voidinorderunrecursive (); voidpostorderunrecursive ();};//TreeTest.cpp#include"TreeTest.h"#include<iostream>#include<queue>#include<stack>using namespacestd; Mytree::mytree (): M_root (NULL) {}mytree::~mytree () { This-destroy (M_root); M_root=NULL;}voidMytree::d Estroy (treenode*root) { if(!root)return; TreeNode* Left=null, *right=NULL; Left= root->Leftchild; Right= root->Rightchild; Delete root; if(left) destroy (left); if(right) destroy (right);} TreeNode* Mytree::create (treenode*&root) { intvalue; CIN>>value; if(Value <=0)returnNULL;//less than or equal to 0 represents null if(!root) Root= This-Gettreenode (value); if(!root->leftchild) This->create (root->leftchild); if(!root->rightchild) This->create (root->rightchild); returnRoot;} TreeNode* Mytree::gettreenode (intvalue) {TreeNode* p =NewTreeNode (); P->value =value; P->leftchild = P->rightchild =NULL; returnp;}voidMytree::createtreeforpreorder () { This-Create (M_root);}voidMytree::p reorderrecursive (treenode*root) { if(!root)return; printf ("%d",root->value); if(Root->leftchild) This->preorderrecursive (root->leftchild); if(Root->rightchild) This->preorderrecursive (root->rightchild);}voidMytree::inorderrecursive (treenode*root) { if(!root)return; if(Root->leftchild) This->inorderrecursive (root->leftchild); printf ("%d",root->value); if(Root->rightchild) This->inorderrecursive (root->rightchild);}voidMytree::p ostorderrecursive (treenode*root) { if(!root)return; if(Root->leftchild) This->postorderrecursive (root->leftchild); if(Root->rightchild) This->postorderrecursive (root->rightchild); printf ("%d", root->value);}voidMytree::levelorderrecursive (treenode*root) {Queue<TreeNode*>Q; if(Root) q.push (root); while(!Q.empty ()) {TreeNode* node =Q.front (); Q.pop (); Node-print (); if(Node->leftchild) Q.push (node->leftchild); if(Node->rightchild) Q.push (node->rightchild); }}voidmytree::p reorderrecursive () {printf ("recursive pre-order traversal:"); This-preorderrecursive (m_root); printf ("\ n");}voidmytree::inorderrecursive () {printf ("recursive mid-order traversal:"); This-inorderrecursive (m_root); printf ("\ n");}voidmytree::p ostorderrecursive () {printf ("Recursive post-traversal:"); This-postorderrecursive (m_root); printf ("\ n");}voidmytree::levelorderrecursive () {printf ("sequence Traversal:"); This-levelorderrecursive (m_root); printf ("\ n");}voidmytree::p reorderunrecursive () {if(!m_root)return; printf ("non-recursive first-order traversal:"); Stack<TreeNode*>S; S.push (M_root); while(!S.empty ()) {TreeNode* node =S.top (); S.pop (); Node-print (); if(Node->rightchild) S.push (node->rightchild); if(Node->leftchild) S.push (node->leftchild); } printf ("\ n");}voidmytree::inorderunrecursive () {if(!m_root)return; printf ("non-recursive in-sequence traversal:"); Stack<TreeNode*>S; TreeNode* p =M_root; while(P | |!S.empty ()) { while(P) {S.push (P); P= p->Leftchild; } P=S.top (); S.pop (); P-print (); /*since it is a middle-order traversal, the next step is to access the right subtree of the node after the current node has been accessed*/P= p->Rightchild; } printf ("\ n");}voidmytree::p ostorderunrecursive () {if(!m_root)return; printf ("non-recursive post-traversal:"); Stack<TreeNode*>S; TreeNode* p =M_root; TreeNode* Q =NULL; while(P | |!S.empty ()) { while(P) {S.push (P); P= p->Leftchild; } P=S.top (); /*Judging that the right node of the current node does not have or has been accessed Q represents the last access node, because it is a subsequent traversal, if the current node has the right node, then the last access node must be the right node of the current node*/ if(!p->rightchild | | p->rightchild = =q) {p-print (); Q=p; P=NULL; S.pop (); } Else{p= p->Rightchild; }} printf ("\ n");}//main.cpp#include"TreeTest.h"intMain () {mytree tree; Tree.createtreeforpreorder (); Tree.preorderrecursive (); Tree.preorderunrecursive (); Tree.inorderrecursive (); Tree.inorderunrecursive (); Tree.postorderrecursive (); Tree.postorderunrecursive (); Tree.levelorderrecursive (); GetChar (); GetChar (); return 0;}
Test Case:
1 2 3-1-1 4-1-1 5 6-1-1 7 8-1-1 9-1-1
Recursive and non-recursive algorithms for three traversal of binary tree