Introduction to Algorithms 12.1-3

Source: Internet
Author: User

title: Design of a non-recursive algorithm for the execution of sequential traversal

Answer:

Analysis:

1, the use of stack simulation recursive call process, that is, you can achieve the middle sequence traversal

2, increase the pointer field in the node, so that the pointer field points to the parent node, by iterating to achieve the middle sequence traversal

Non-recursive algorithm:

Stack simulation algorithm version one:

// inorder TRAVERASL 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;}}    }

Note: This algorithm is implemented using depth-first search techniques, which are visit when backtracking.

Stack simulation algorithm version two:

//inorder TRAVERASL version2.0voidinorder (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 ); }        }    }}

Note: The algorithm uses the width-first search technique to mark whether the Childpushed flag can be visit.

Parent Node Implementation version:

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; }}

Note: Using this method requires adding a pointer field to the parent node, which will have some difficulty in the achievement

Algorithm time complexity Analysis: O (N)

Introduction to Algorithms 12.1-3

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.