Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A blatant copy and paste from <a href="http://forums.sun.com/thread.jspa?forumID=426&amp;threadID=667512" rel="nofollow noreferrer">Sun's (Oracle now, I guess...) forum</a>:</p> <blockquote> <blockquote> <p><strong>Question:</strong> <br /> <em>Can anybody help me on how to construct Binary tree from inorder and postorder traversals,i just want to know the algorithm so that i can apply it.</em></p> </blockquote> <p><strong>Answer:</strong><br /> Let <code>p_1</code>, <code>p_2</code> <code>...</code> <code>p_n</code> be the postorder traversal and let <code>i_1</code>, <code>i_2</code> <code>...</code> <code>i_n</code> be the inorder traversal. From the postorder traversal we know that the root of the tree is <code>p_n</code>. Find this element in the inorder traversal, say <code>i_1</code>, <code>i_2</code> <code>...</code> <code>i_k-1</code> <code>p_n</code> <code>i_k+1</code> <code>...</code> <code>i_n</code>. From the inorder traversal we find all the elements in the left subtree, i.e. <code>i_1</code>, <code>i_2</code> <code>...</code> <code>i_k-1</code> and in the right subtree, i.e. <code>i_k+1</code> <code>...</code> <code>i_n</code> respectively.</p> <p>Remove element <code>p_n</code> (and element <code>i_k</code> <code>==</code> <code>p_n</code>). Find the rightmost element <code>p_j</code> in <code>p_1</code>, <code>p_2</code> <code>...</code> <code>p_j</code> <code>...</code> <code>p_n-1</code> where <code>p_j</code> is an element in <code>i_1</code>, <code>i_2</code> ... <code>i_k-1</code>. This is the root of the left subtree of the original tree. Split <code>p_1</code>, <code>p_2</code> <code>...</code> <code>p_j</code> and <code>p_j+1</code> ... <code>p_n-1</code> and <code>i_1</code>, <code>i_2</code> <code>...</code> <code>i_k-1</code> and <code>i_k+1</code> <code>...</code> <code>i_n</code>. Now you have two subsequences representing the postorder and inorder traversal of the two subtrees of the original tree.</p> </blockquote> <p>Author: <a href="http://forums.sun.com/profile.jspa?userID=431408" rel="nofollow noreferrer">JosAH</a>.</p> <p>I've implemented the algorithm once following Jos' instructions, and it worked perfectly!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload