<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Visit on Zio Ivan</title>
    <link>https://ivandemarino.me/tags/visit/</link>
    <description>Recent content in Visit on Zio Ivan</description>
    <generator>Hugo</generator>
    <language>en</language>
    <managingEditor>detronizator&#43;blog@gmail.com (Ivan De Marino, aka &#34;Zio Ivan&#34;, aka &#34;detro&#34;)</managingEditor>
    <webMaster>detronizator&#43;blog@gmail.com (Ivan De Marino, aka &#34;Zio Ivan&#34;, aka &#34;detro&#34;)</webMaster>
    <copyright>2004-2026 Ivan De Marino. Licensed under CC BY 4.0</copyright>
    <lastBuildDate>Fri, 05 Feb 2010 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://ivandemarino.me/tags/visit/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Binary Tree Rebuilder</title>
      <link>https://ivandemarino.me/posts/binary-tree-rebuilder/</link>
      <pubDate>Fri, 05 Feb 2010 00:00:00 +0000</pubDate><author>detronizator&#43;blog@gmail.com (Ivan De Marino, aka &#34;Zio Ivan&#34;, aka &#34;detro&#34;)</author>
      <guid>https://ivandemarino.me/posts/binary-tree-rebuilder/</guid>
      <description>&lt;p&gt;Imagine you have a &lt;a href=&#34;http://en.wikipedia.org/wiki/Binary_tree&#34;&gt;Binary Tree&lt;/a&gt;, with those characteristics:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;* Nodes do not respect any order relation - In other words: it&#39;s &amp;lt;strong&amp;gt;not&amp;lt;/strong&amp;gt; a [Binary Search Tree](http://en.wikipedia.org/wiki/Binary_search_tree) of any kind

* Every node appears &amp;lt;em&amp;gt;once and only once&amp;lt;/em&amp;gt; within the tree
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img alt=&#34;A nice Binary Tree&#34; loading=&#34;lazy&#34; src=&#34;http://www.drunkmenworkhere.org/bintree/pagerank.png&#34;&gt;
&lt;em&gt;A nice Binary Tree&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Then, your little brother passes by your desk and, to upset you, deletes the tree from your computer memory/HD (yeah, I know, I&amp;rsquo;m pathetic at inventing hypothetical situations :-P ).&lt;/p&gt;
&lt;p&gt;Fortunately though, you previously did a &lt;a href=&#34;http://en.wikipedia.org/wiki/Tree_traversal&#34;&gt;Pre-Order and an In-Order visit of your tree&lt;/a&gt;, and stored the result in an 2 nice array.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Can you rebuild the original tree structure out of this 2 array?&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;How are you going to rebuild it?&lt;/h3&gt;
Yes, you can! (Sorry, I couldn&#39;t resist). And it&#39;s quite easy as well. What you have to do, is the following: 
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Take the first element of the PreOrder Array and use it as root of a new tree&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Find the position of this New Node in the InOrder Array, scanning it from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt; (&lt;code&gt;n&lt;/code&gt; is the number of Nodes)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;IF next element in the PreOrder Array is on the left of the New Node in the InOrder array: call RECURSIVELY this procedure, this time taking into account the portion of InOrder array that goes from &lt;code&gt;0&lt;/code&gt; to the &lt;code&gt;position of the New Node in the InOrder Array -1&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;IF next element in the PreOrder Array is on the right of the New Node in the InOrder array: call RECURSIVELY this procedure, this time taking into account the portion of InOrder array that goes from the&lt;code&gt; position of the New Node in the InOrder Array +1&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Return the New Node&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By the way, this doesn&amp;rsquo;t work. To fix it we should be more generic, specifying things a little bit better. Things like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;* Every recursive calls takes into account a portion of the InOrder array; in the case of the first call it&#39;s the entire array

* There is going to be as much recursive calls as the number of elements in the PreOrder array
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, is a tree what we are talking about here: &lt;strong&gt;recursion is a MUST&lt;/strong&gt;.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
