Obtain the post-order sequence based on the pre-order sequence and Middle-order sequence of the binary tree.

Source: Internet
Author: User

Example:

Pre-sequence: abdgcefh

Central sequence: dgbaechf

Result: descending sequence

Analysis:

According to the pre-sequence, a must be the root node. Find a in the middle sequence, and find that the left side of a is DGB, and the right side is echf, then the DGB should be the left child of a, and the echf is the right child of.

Then, we only need to recursively judge the posterior sequence of left and right children. For example, if DGB is the central sequence of the left child, you can obtain it from abdgcefh. the first sequence is BDG. The same applies to right children.

Programming Implementation ideas:

(1) Find the first element in the forward sequence as the root node root;

(2) Search for the location index of the root in the central sequence, and divide the central sequence into two parts based on the index (leftmidorder, rightmidorder ), according to the index, the pre-order sequence is divided into two parts (leftpreorder and rightpreorder );

(3) leftpreorder and leftmidorder are used to obtain the post-order leftpostorder of the left child of the root node;

(4) based on rightpreorder and rightmidorder, obtain the descending sequence of the right child of the root node: rightpostorder;

(5) leftpostorder + rightpostorder + root is the post sequence.

Code:

Package binarytree; public class restorebinarytree {/*** according to the preorder and middle order, get the post-Order * @ Param preorder * @ Param midorder * @ return */public static string getpostbypreandmid (string [] preorder, string [] midorder) throws nullpointerexception, illegalargumentexception {If (preorder = NULL | midorder = NULL) throw new nullpointerexception ("the passed array cannot be null! "); If (preorder. length! = Midorder. Length) throw new illegalargumentexception ("the length of the pre-order array is different from that of the middle-order array! "); If (preorder. length = 0) Return ""; if (preorder. length = 1) return preorder [0]; string postorder = ""; string root = preorder [0]; // get rootint Index = indexof (midorder, root ); if (Index =-1) throw new illegalargumentexception ("the parameter passed is incorrect"); string [] leftpreorder = new string [Index]; system. arraycopy (preorder, 1, leftpreorder, 0, index); string [] leftmidorder = new string [Index]; system. arraycopy (midorder, 0, Leftmidorder, 0, index); string leftpostorder = getpostbypreandmid (leftpreorder, leftmidorder); int rightpartlen = preorder. length-index-1; string [] rightpreorder = new string [rightpartlen]; system. arraycopy (preorder, index + 1, rightpreorder, 0, rightpartlen); string [] rightmidorder = new string [rightpartlen]; system. arraycopy (midorder, index + 1, rightmidorder, 0, rightpartlen); string rightpostorder = Ge Tpostbypreandmid (rightpreorder, rightmidorder); If (leftpostorder! = NULL &&! Leftpostorder. equals ("") {If (leftpostorder. endswith (",") {// The purpose of the judgment is to output, each element is separated by "," postorder + = leftpostorder ;} else {postorder + = leftpostorder + "," ;}} if (rightpostorder! = NULL &&! Rightpostorder. equals ("") {If (rightpostorder. endswith (",") {postorder + = rightpostorder;} else {postorder + = rightpostorder + "," ;}} postorder + = root; return postorder ;} /*** obtain the element index in the array * @ Param strarr * @ Param key * @ return */Private Static int indexof (string [] strarr, string key) {int Index =-1; for (INT I = 0; I <strarr. length; I ++) {If (strarr [I]. equals (key) {Index = I; break;} Return Index;} public static void main (string [] ARGs) {string [] preorder = new string [] {"A", "B", "D", "g", "C", "E", "F ", "H"}; string [] midorder = new string [] {"D", "g", "B", "A", "E", "C ", "H", "F"}; string postorder = getpostbypreandmid (preorder, midorder); system. out. println (postorder );}}

Extension:

The idea of obtaining a forward Sequence Based on the Post-order sequence of a binary tree is the same as that for obtaining the post-order sequence based on the pre-order and Middle-order.

In addition, can we obtain the central sequence based on the pre-order sequence and the post-order sequence? It is possible to obtain a unique ordinal sequence, or multiple ordinal sequences. Why?

The reason is: if a parent node has only one child node, whether the child is a left child or a right child, there is no impact on the pre-and post-order sequences, therefore, for the same forward and backward sequences, there may be a variety of intermediate sequences. The following question is: when is there only one possibility? If there is a possibility of multiple ordinal sequences, how can we find all possible ordinal sequences?

(1) When is there only one possibility?

The above analysis has pointed out that there are multiple possible causes of the central sequence of non-leaf nodes with only one child. Therefore, if all the non-leaf nodes in the binary tree have two children, the ordinal sequence is uniquely identified.

(2) If there are multiple possibilities, how can we find all the possible sequences?

There are multiple possibilities, so there must be several non-leaf nodes with only one child. As long as you change the child's location, you can get a different central sequence. The algorithm logic is as follows:

It is still a recursive problem:

(1) If the left and right children of the node are empty, the node itself is returned;

(2) If the left child is not empty, all possible ordinal sequences of the left child are returned;

(3) If the right child is not empty, all possible ordinal sequences of the right child are returned;

(4) If the left and right children are not empty, all the ordinal sequences with the node as the root may be: take one from the possible middle sequence of the left child + root node + the right child. If there is only the left child, all the Middle sequence is: get one + root node from the left child, which is the same as the root node + left child. Only the right child is the same as the left child.

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.