Data structure------known sequence ordering sequence, known sequence order

Source: Internet
Author: User

In this paper, we summarize the two known traversal methods of the two-fork tree to find the third kind of traversal order, and we know that the binary tree is the only one determined by the first order and the middle sequence traversal, or after the sequence and the middle order traversal, the following describes how to find the third traversal order.

The sequence of first order traversal is: root node--left Dial hand node--right sub-node, middle sequence traversal is: Left Dial hand node--root node--right sub-node, we notice that the first sequence traversal is a binary root node, we in the middle sequence traversal with the element is divided into two parts, then the left is the left dial hand tree, the right side is a subtree, Recursive can restore the binary tree, this process can directly output the order of sequential traversal. In the same vein, sequential and middle order can be used to restore the sequence of first order traversal.

The code and test data are as follows:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <malloc.h>6#include <string>7#include <vector>8#include <stack>9#include <queue>Ten#include <Set> One#include <map> A  - #defineFrer () freopen ("In.txt", "R", stdin); -  the using namespacestd; -  - //function Status Code definition - #defineTRUE 1 + #defineFALSE 0 - #defineOK 1 + #defineERROR 0 A #defineINFEASIBLE-1 at #defineOVERFLOW-2 -  -typedefCharTelemtype; -typedefintStatus; -  -typedefstructBinode { in telemtype data; -     structBinode *lchild, *Rchild; to}binode, *Bitree; +  -Bitree Binarytreeformorderings (Char*,Char*,int); theBitree Binarytreepostorderings (Char*,Char*,int); *  $ /*Panax Notoginseng abdecfg - DBEAFCG the DEBFGCA + */ A  the intMain () + { - Frer () $     intN; $     Charstr[ -], ptr[ -]; -CIN >> N >> str >>ptr; - binarytreepostorderings (str, PTR, n); the     return 0; - }Wuyi  theBitree Binarytreeformorderings (Char*pre,Char*inch,intLen) { -     if(Len <=0) Wu         returnNULL; -Binode *node =NewBinode; AboutNode->data = *Pre; $     intIDX =0; -      while(IDX <Len) { -         if(*(inch+ idx) = = *pre) -              Break; A++idx; +     } theNode->lchild = binarytreeformorderings (pre +1,inch, idx); -Node->rchild = binarytreeformorderings (pre + idx +1,inch+ idx +1, Len-(idx +1)); $cout << Node->data <<' '; the     returnnode; the } the  theBitree Binarytreepostorderings (Char*inch,Char*post,intLen) { -     if(len = =0) in         returnNULL; theBinode *node =NewBinode; theNode->data = * (post + len-1); Aboutcout << Node->data <<' '; the     intIDX =0; the      while(IDX <Len) { the         if(*(inch+ idx) = = * (post + len-1)) +              Break; -++idx; the     }BayiNode->lchild = Binarytreepostorderings (inch, post, idx); theNode->rchild = Binarytreepostorderings (inch+ idx +1, post + idx, Len-(idx +1)); the     returnnode; -}

Data structure------known sequence ordering sequence, known sequence order

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.