"Data Structure" review notes--Two fork Tree 2

Source: Internet
Author: User

Non-recursive traversal of a binary tree:

A non-recursive traversal algorithm for middle sequence traversal
The basic idea of non-recursive algorithm implementation: using stacks:

void Inordertraversal (Bintree BT) {    bintree t=bt;    Stack S = Creatstack (MaxSize); /* Create and initialize the stack s*/while    (T | |! IsEmpty (S))    {        while (T)/  * always left and the nodes along the way are pressed into the stack */        {            Push (s,t);            T = t->left;        }        if (! IsEmpty (s))        {            T = pop (s);/* node pop-up stack */            printf ("%5d", t->data);/* (Access) Print node */            T = t->right;/* Turn to right sub-tree *}}    }


void Inordertraversal (Bintree BT) {    bintree t=bt;    Stack S = Creatstack (MaxSize); /* Create and initialize the stack s*/while    (T | |! IsEmpty (S))    {        while (T)/  * always left and the nodes along the way are pressed into the stack */        {            Push (s,t);              printf ("%5d", t->data); /* (Access) Print node */            T = t->left;        }        if (! IsEmpty (s))        {            T = pop (s);/* node pop-up stack */            T = t->right;/* Turn right subtree */        }}    }

void Postorder_norecurse (Bintree bt, Void (*do) (Bintree))//non-recursive implementation of post-traversal {bintree T = BT;    Stack s = createstack (20);   int tag[20]; Tag is used to mark the first few times that an element inside the stack is encountered, which is itself an shaping stack while (T | |!            Stack_isempty (s)) {while (T)//each encounter a new element, then to the control here {Stack_push (S, T);//Put the stack and loop to its leftmost            Tag[s->size-1] = 0;        T = t->left; } while (! T &&!                Stack_isempty (s)) {T = Stack_pop (s);//Remove an element from the stack and determine its Tag if (Tag[s->size]) { (*do)   (T);        Tag! = 0 shows that the node was met for the third time, and it was manipulated T = 0;                Set T to 0 to trigger the while condition, continue looping} else//tag = 0 description is the second encounter (the first is to set Tag to 0) { if (!   T->right) (*do) (T);      If the right son does not exist, direct output else {Stack_push (S, T);        If the right son exists, put it back on the stack tag[s->size-1]++;  and accumulate the corresponding tag} T = t->right; Return to the right son. Note that if the right son does notexists, the while continuation loop will be triggered}//Otherwise it will be decided to meet the new element to jump out of the loop and continue outside the large while loop}}} 
Two fork tree determined by two traversal sequences?

Is it possible to uniquely determine a binary tree if any two traversal sequences are known to be three kinds of traversal? We know that either way, you have to know first-order traversal.

For example: First order and middle sequence traversal sequence to determine a binary tree
Analysis
1 The root node is determined based on the first node of the sequential traversal sequence;
2 split the left and right two sub-sequences from the root node in the middle sequence traversal sequence.
3 The right subtree of the Saozi is recursively used in the same way to continue decomposition.

Topic Practice: Put it on again in a few days.

"Data Structure" review notes--Two fork Tree 2

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.