Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer is : <strong>you can recover a binary search tree from its pre order traversal</strong>. </p> <p>I'm not sure what is your mathematical background so please ask if you need more explanation. </p> <p>For simplicity, I'm assuming that the node are labeled by the integer 1,2... n where n is the number of node. Then the pre-order traversal of the tree t gives gives you a permutation of <code>[n] = {1,2,...,n}</code> which have a particular property: each time you have a letter b in your permutation, you can't find two consecutive letter <code>ca</code> after the <code>b</code> in the permutation such that <code>a&lt;b&lt;c</code>. Such a permutation is said to <em>avoid</em> the pattern <code>b-ac</code> (the - stand for an arbitrary number of letters). </p> <p>For example, 4 2 1 3 avoids b-ac whereas 3 1 4 2 doesn't because 3 - 4 2.</p> <p>This is actually an equivalence: A permutation is the pre order reading of a tree iff is avoid b-ac. </p> <p>It is know that there are as many trees of size n as permutation avoiding b-ac so this is a bijection. Their number are know as Catalan number. You probably can find this as an exercice of Stanley's book "enumerative combinatorics".</p> <p><strong>Here is a more algorithmic explanation</strong>:</p> <pre><code>RecTree: Recovering a tree from is Pre-order traversal: input: list l output: tree t b &lt;- l[0] find an index i such that - for 1&lt;=j&lt;=i then l[j] &lt; b and - for i&lt;j&lt;=n then l[j] &gt; b if there isn't exists such an index return Failure else return Node(key=b, RecTree(l[1..i]), RecTree(l[i+1..n])) </code></pre> <p>As a consequence </p> <p><strong>Two binary search trees are equal if and only if they have the same pre order traversal</strong> </p> <p>Does it makes sense to you ?</p> <p>Some more references </p> <ul> <li><a href="http://oeis.org/A000108," rel="nofollow">Catalan number in the On-Line Encyclopedia of Integer Sequences</a></li> <li><a href="http://combinatorics.cis.strath.ac.uk/download/Cl01__Generalized_Pattern.pdf" rel="nofollow">Anders Claesson Generalized Pattern Avoidance, European Journal of Combinatorics 22 (2001) 961–971</a></li> </ul>
    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. 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