title: for a tree with N nodes, the first order, middle sequence, and post-order traversal algorithms are designed to be completed in O (N) time
first, sequential traversal
Recursive implementations:
void inorder (Searchtree t) { if (t! = NULL) { Visit (t); Inorder (T-left); Inorder (T-right); }}
Non-recursive implementations:
Version one: Stack simulation (Depth-first search)
void Preorder (Searchtree T) { Stack S; while (T! = NULL | |! s.empty ()) { if (t! = NULL) { s.push (t); Visit (T); = T-> left; } Else { = s.pop (); = T-> Right;}} }
Version two: Stack simulation (depth first search)
void Preorder (Searchtree T) { Stack S; if (T! = NULL) { s.push (T); while ( ! s.empty ()) { = s.pop (); Visit (tnode); S.push (Tnode,right); S.push (Tnode-left); }}}
Version three: Set parent node backtracking
voidPreorder (Searchtree T) { while(T! = NULL) { if( ! T->visited) { Visit (T); T->visited =true; } if(T->left! = NULL &&!) T->left->visited) {T= t->Left ; } Else if(T->right! = NULL &&!) T->right->visited) {T= t->Right ; } ElseT = t->Parent; }}
second, middle sequence traversal
Recursive version:
void inorder (Searchtree t) { if (t! = NULL) { inorder (t-left); Visit (T); Inorder (T-right); }}
non-recursive version one: Depth-first search
void inorder (Searchtree T) { Stack S; while (T! = NULL | |! s.empty ()) { if (t! = NULL) { s.push (t) ; = T-> left; } Else { = s.pop (); Visit (T); = T-> Right;}} }
non-recursive version II: Depth-First search
voidinorder (Searchtree T) {Stack S; if(T! =NULL) S.push (T); T->childpushed =false; while( !S.empty ()) {Searchtree Tnode=S.pop (); if(tnode->childpushed) { //if the identity bit is true, it means that the left and right subtrees are already in the stack, so you need to access that node now.Visit (tnode); } Else { //The left and right sub-tree has not yet entered the stack, then, in turn , the node if(Tnode->right! =NULL) { //both left and right subtrees are set to Falsetnode->right->childpushed =false; S.push (Tnode-Right ); } tnode->childpushed =true;//root node flag bit is trueS.push (tnode); if(Tnode->left! =NULL) {Tnode->left->childpushed =false; S.push (Tnode-Left ); } } }}
version Three: Set parent node backtracking
voidinorder (Searchtree T) { while(T! =NULL) { while(T->left! = NULL &&!) T->left->visited) T= t->Left ; if( ! T->visited) {Visit (T); T->visited =true; } if(T->right! = NULL &&!) T->right->visited) T= t->Right ; ElseT= t->Parent; }}Third, post-sequential traversal
Recursive version:
void postorder (Searchtree t) { if (t! = NULL) { postorder (t-left);
postorder (T-right); Visit (T); }}
non-recursive version: Depth-First search
voidpostorder (Searchtree T) {Stack S; if(T! =NULL) S.push (T); T->childpushed =false; while( !S.empty ()) {Searchtree Tnode=S.pop (); if(tnode->childpushed) { //if the identity bit is true, it means that the left and right subtrees are already in the stack, so you need to access that node now.Visit (tnode); } Else{tnode->childpushed =true;//root node flag bit is trueS.push (tnode); //The left and right sub-tree has not yet entered the stack, then the root node, and in turn, if(Tnode->right! =NULL) { //both left and right subtrees are set to Falsetnode->right->childpushed =false; S.push (Tnode-Right ); } if(Tnode->left! =NULL) {Tnode->left->childpushed =false; S.push (Tnode-Left ); } } }}
algorithm Time complexity analysis : The time complexity of the above algorithms are O (N)
Introduction to Algorithms 12.1-4