Non-recursive sequential traversal algorithm for two-fork tree without stack-assist

Source: Internet
Author: User
Topics

/****
"Title" assumes the addition of a flag field to the nodes of a three-strand list
(Mark takes a value of 0, 1, or 2) to distinguish it from reaching that node during the traversal.
You should continue to the left or right or access the node. Try to store the knot
The non-recursive sequential traversal algorithm of two-fork tree without stack-assist is programmed.
Cross-linked list type definition with flag field:
typedef struct TRITNODE {
Telemtype data;
struct Tritnode *lchild, *rchild, *parent;
int mark; Flag field
} Tritnode, *tritree;
* * * *Find the first node that the subtree begins to traverse, that is, the node in the bottom-left corner of the entire tree accesses the node that the entire subtree accesses first. Back to the parent node, the right node of the parent node is the second-visited node. If the parent node does not have a right subtree, or if the right subtree has already been accessed, it accesses itself and returns to the previous parent node. By the above has been determined that the parent node has a right subtree, then re-guide his right sub-tree. Code

void postorder (Tritree T, Void (*visit) (Telemtype))/* Do not use the stack, non-recursive sequential traversal of the binary tree T, */* For each node of the element domain data call
    Function Visit */{Tritree p,pr;

    p = T;
       while (p) {if (p->lchild)//Find the node at the bottom-most left corner p = p->lchild;
          else {if (p->rchild)//If the node in the lower-left corner has a right node P = p->rchild;   else {visit (p->data);

            Access the first node to be accessed P->mark = 1;     p = p->parent; Returns the parent node while (p && (p->rchild = = NULL | | p->rchild->mark = = 1))//If the right node has been accessed or not
              There is a right node {visit (p->data);
              P->mark = 1;
            p = p->parent;

          if (p)//through the above loop, determine that the current node must contain the right node, access the right subtree P = p->rchild; }                        

       }  
    }
}

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.