How to get the order of the two-fork tree by the pre-sequence and the middle order of the two-fork tree?

Source: Internet
Author: User

How to get the order of the two-fork tree by the pre-sequence and the middle order of the two-fork tree?

First of all, we must understand what is pre-order, middle order, sequence.

Binary Tree Pre-order: The traversal sequence is, the root node, the Zuozi, the right subtree; middle order: The traversal order is, the Zuozi, the root node, the right sub-tree; Post order: Traversal sequence is, Zuozi, right subtree, root node

It can be found that the first node in the binary tree pre-order is the root node of the tree, and then find out the location of root in the middle order, you can divide the pre-order and the middle sequence into two parts of the left and right subtree, and then call it recursively.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/A7/wKioL1coJUDh5zixAAB9bRXcWK8857.png "title=" QQ picture 20160503120659.png "alt=" Wkiol1cojudh5zixaab9brxcwk8857.png "/>

#include <iostream>using namespace std;template<class t>struct binarytreenode{t  _data; binarytreenode* _left; binarytreenode* _right; Binarytreenode (const t& x): _data (x),  _left (null),  _right (null) {}};template<class  t>class binarytree{protected:binarytreenode<t>* _root;protected:void _preorder ( Binarytreenode<t>* root) {if  (root != null) {Cout << root->_data  <<  " "; _preorder (Root->_left); _preorder (root->_right);} return;} Binarytreenode<t>* _createbinary (char* preorder, char* inorder, int length ) {binarytreenode<t>* root = null;if  (length == 0) return NULL;int  tmp = *preOrder;int index = 0;while  (index < length&& inorder[index] != tmp) index++;if  (index < length) {ROOT&Nbsp;= new binarytreenode<t> (tmp-' 0 '); Root->_left = _createbinary (preOrder +  1, inorder,index); Root->_right = _createbinary (preorder + index +  1,&NBSP;INORDER&NBSP;+&NBSP;INDEX&NBSP;+&NBSP;1,&NBSP;LENGTH&NBSP;-&NBSP;INDEX&NBSP;-&NBSP;1);} Return root;} Void _clear (Binarytreenode<t>* root) {if  (root) {_clear (root->_left); _Clear (root->_ right);d elete root;}} Public:binarytree (): _root (NULL) {}~binarytree () {_clear (_root); _root = null;} Void  preorder () {_preorder (_root); Cout << endl;} Void createbinarytree (Char* preorder, char* inorder) {Int length = strlen ( preorder); _root = _createbinary (preorder, inorder,length);}; Void test1 () {char* preorder =  "12473568";char* inorder =  "47215386"; BINARYTREE&LT;INT&GT;&NBSP;BT;BT. Createbinarytree (Preorder, inorder); bt. Preorder ();} 

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/A9/wKiom1coJPDB1NQJAAAK79k-SQ0010.png "title=" QQ picture 20160503120913.png "alt=" Wkiom1cojpdb1nqjaaak79k-sq0010.png "/>

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1769658

How to get the order of the two-fork tree by the pre-sequence and the middle order of the two-fork tree?

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.