Introduction to Algorithms 12.1-4

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.