<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elvis Montero</title>
	<atom:link href="http://www.elvismontero.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.elvismontero.com</link>
	<description>Technologist. Blogger. Human.</description>
	<lastBuildDate>Sun, 19 Jun 2011 15:19:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>King Crab</title>
		<link>http://www.elvismontero.com/2011/06/19/king-crab/</link>
		<comments>http://www.elvismontero.com/2011/06/19/king-crab/#comments</comments>
		<pubDate>Sun, 19 Jun 2011 15:19:50 +0000</pubDate>
		<dc:creator>emontero</dc:creator>
				<category><![CDATA[Pictures]]></category>

		<guid isPermaLink="false">http://www.elvismontero.com/?p=983</guid>
		<description><![CDATA[Well, not really. This tiny land crab is not comparable in size to its aquatic cousin, but it certainly looks rather large in the closeup above. I snapped this picture at a beach in Samaná, which is a beautiful northeastern province in Dominican Republic. Small land crabs like this are really elusive. They&#8217;re incredibly fast. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" src="/pictures/king-crab-small.jpg" alt="" width="400" height="265" /></p>
<p>Well, not really. This tiny land crab is not comparable in size to its <a href="http://en.wikipedia.org/wiki/King_crab" target="_blank">aquatic cousin</a>, but it certainly looks rather large in the closeup above. I snapped this picture at a beach in Samaná, which is a beautiful northeastern province in Dominican Republic. Small land crabs like this are really elusive. They&#8217;re incredibly fast. I knew I&#8217;d have a hard time getting this evasive little friend in focus, so I set a fast shutter speed on my camera and tried focusing where I thought the crab would go next. After a few shots in rapid continuous mode, this is the result. Needless to say, I&#8217;m pleased with the outcome.</p>
<p>If you&#8217;d like to download a hi-res version, <a href="http://www.elvismontero.com/pictures/king-crab.jpg" target="_blank">click here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elvismontero.com/2011/06/19/king-crab/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Lowest Common Ancestor</title>
		<link>http://www.elvismontero.com/2011/03/21/lowest-common-ancestor/</link>
		<comments>http://www.elvismontero.com/2011/03/21/lowest-common-ancestor/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 22:58:10 +0000</pubDate>
		<dc:creator>emontero</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming Praxis]]></category>

		<guid isPermaLink="false">http://www.elvismontero.com/?p=909</guid>
		<description><![CDATA[Yet another PP challenge piqued my interest. Here&#8217;s the problem statement: Today’s problem appears with some regularity at places like Proggit and Stack Overflow and in lists of programming interview questions: Given a binary tree t and two elements of the tree, m and n, with m&#60;n, find the lowest element of the tree (farthest [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another <a href="http://www.programmingpraxis.com" target="_blank">PP</a> challenge piqued my interest. <a href="http://programmingpraxis.com/2011/03/11/lowest-common-ancestor/" target="_blank">Here&#8217;s the problem statement:</a></p>
<blockquote><p><a href="http://www.elvismontero.com/wp-content/uploads/2011/03/bst.png"><img class="alignright size-full wp-image-915" style="margin-right: 10px; margin-left: 10px; border: 0pt none;" title="bst" src="http://www.elvismontero.com/wp-content/uploads/2011/03/bst.png" alt="" width="200" height="167" /></a></p>
<p>Today’s problem appears with some regularity at places like Proggit  and Stack Overflow and in lists of programming interview questions:</p>
<p><strong>Given a binary tree <em>t</em> and two elements of the tree, <em>m</em> and <em>n</em>, with <em>m</em>&lt;<em>n</em>, find the lowest element of the tree (farthest from the root) that is an ancestor of both <em>m</em> and <em>n</em>.</strong></p>
<p>For example, in the tree shown at right, the lowest common ancestor  of 4 and 7 is 6, the lowest common ancestor of 4 and 10 is 8, and the  lowest common ancestor of 1 and 4 is 3.  It is possible for an element  of the tree to be its own ancestor, so the lowest common ancestor of 1  and 3 is 3, and the lowest common ancestor of 3 and 6 is 3.</p>
<p>Your task is to write a function that finds the lowest common  ancestor of two elements of a binary tree.  When you are finished, you  are welcome to <a href="http://programmingpraxis.com/2011/03/11/lowest-common-ancestor/2/">read</a> or <a href="http://programmingpraxis.codepad.org/h8u1RuTR">run</a> a suggested solution, or to post your own solution or discuss the exercise in the comments below.</p></blockquote>
<p>When it comes to real life applicability (and sheer awesomeness!), <a href="http://en.wikipedia.org/wiki/Binary_trees" target="_blank">binary trees</a> are among the highest of the pack in the realm of data structures. Because of this innocuous little fact, I decided I simply had to solve this problem right away. Let&#8217;s dive right into it!</p>
<p><strong>NOTE: The Java source code for the solution described below can be found <a href="../progpraxis/LowestCommonAncestor.html" target="_blank">here</a>.</strong></p>
<h1>Step 1</h1>
<p>We need to represent nodes and the tree itself first. <strong>Trees are recursive identities in nature</strong>. Namely, trees are made up of trees (subtrees), which are in time composed of more trees (child nodes), which, by definition, are also trees. For instance, analyzing the tree from the problem statement, we can say 3 is the root node for 1 and 6. 6 is the root node for 4 and 7. 4 is its own root node. If we take 3 and detach it from 8, we now have a full-blown tree. In order to model these relationships in code, a class like the following would suffice:</p>
<div style="overflow: auto; width: auto; color: black; background: none repeat scroll 0% 0% white; padding: 0.2em 0.6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;">1
2
3
4
5
6</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"><span style="color: #0000aa;">class</span> <span style="color: #00aa00; text-decoration: underline;">Node</span>{
     <span style="color: #0000aa;">private</span> Node left;
     <span style="color: #0000aa;">private</span> Node right;
     <span style="color: #0000aa;">private</span> <span style="color: #00aaaa;">int</span> value;
     <span style="color: #0000aa;">private</span> String path;
}
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>What we&#8217;re basically saying with this trivial Java code is that each node can have two nodes (right and left). Nodes also have a value (specifically, an integer in this case) and a String representation of their paths to the root node. This austere model has everything we need in order to solve the problem at hand.</p>
<h1>Step 2</h1>
<p>Once we have an instance of our tree in place, we must find a way to traverse it and determine the path from the root node to every other node. The following snippet does the trick:<br />
<!-- HTML generated using hilite.me --></p>
<div style="overflow: auto; width: auto; color: black; background: none repeat scroll 0% 0% white; padding: 0.2em 0.6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"><span style="color: #0000aa;">static</span> <span style="color: #00aaaa;">void</span> <span style="color: #00aa00;">traverse</span>(Node node, Stack&lt;Node&gt; stack){
     stack.<span style="color: #1e90ff;">push</span>(node);

     <span style="color: #0000aa;">if</span>(node.<span style="color: #1e90ff;">getRight</span>() != <span style="color: #0000aa;">null</span>)
       traverse(node.<span style="color: #1e90ff;">getRight</span>(), stack);

     <span style="color: #0000aa;">if</span>(node.<span style="color: #1e90ff;">getLeft</span>() != <span style="color: #0000aa;">null</span>)
       traverse(node.<span style="color: #1e90ff;">getLeft</span>(), stack);

     StringBuilder path = <span style="color: #0000aa;">new</span> StringBuilder();
     <span style="color: #0000aa;">for</span>(Node n : stack)
       path.<span style="color: #1e90ff;">append</span>(n).<span style="color: #1e90ff;">append</span>(<span style="color: #aa5500;">" "</span>);
     node.<span style="color: #1e90ff;">setPath</span>(path.<span style="color: #1e90ff;">toString</span>().<span style="color: #1e90ff;">trim</span>());

     stack.<span style="color: #1e90ff;">pop</span>();
     <span style="color: #0000aa;">return</span>;
}
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>The algorithm used here to visit each node is famously called <a href="http://en.wikipedia.org/wiki/Depth-first_search" target="_blank">DFS</a> (<em>depth-first search</em>). Also, notice how we&#8217;re using a stack, a <a href="http://en.wikipedia.org/wiki/LIFO_%28computing%29" target="_blank">LIFO</a> data structure, to keep visited nodes accessible when we decide to map a node&#8217;s path. After computing all the nodes&#8217; paths in our tree, we now must find the lowest common ancestor for the two nodes given. This is rather easy once steps 1 and 2 are out of the way.</p>
<h1>Step 3</h1>
<p>After the <em><strong>traverse</strong></em> function is executed, we have all nodes&#8217; paths in their respective <em><strong>path</strong></em> properties. For 4, this would be the result:</p>
<pre style="text-align: center;"><strong>8 3 6 4</strong></pre>
<p>Now for 7:</p>
<pre style="text-align: center;"><strong>8 3 6 7</strong></pre>
<p>And for 14:</p>
<pre style="text-align: center;"><strong>8 10 14</strong></pre>
<p>Now, I know what you&#8217;re thinking: <em>how is this useful, Elvis?</em> Well, I&#8217;m glad you asked. The answer is we must merely check which of these space-separated elements are identical for two nodes up to a certain point (starting from left to right). That is to say, <strong>for two specific nodes, the rightmost match in two paths is the lowest common ancestor</strong>. For instance, let&#8217;s say we want to find out the lowest common ancestor for nodes 1 (path: 8 3 1) and 4 (path: 8 3 6 4). We&#8217;d line both Strings up like this:</p>
<pre style="text-align: center;"><strong>8 3 1  </strong></pre>
<pre style="text-align: center;"><strong>8 3 6 4</strong></pre>
<p>We&#8217;re now going to iterate the String with the fewer elements and compare each constituent to the other node&#8217;s path. In this case, because of 1&#8242;s path, it&#8217;d be <strong>1 vs 4</strong>. Essentially, this is what we&#8217;d ask each time: <em>are these two elements equal?</em> If they are, we&#8217;ve found the lowest ancestor. We move on and compare the next elements in line. We do this until there aren&#8217;t anymore items to compare (i.e. we&#8217;ve ran out of elements in the shorter path). Here&#8217;s some pseudo code that exemplifies this process for nodes 1 and 4:</p>
<pre style="padding-left: 30px;">Step 1: 8 equals 8? Yes. 8 is the lowest ancestor.</pre>
<pre style="padding-left: 30px;">Step 2: 3 equals 3? Yes. 3 is now the lowest ancestor.</pre>
<pre style="padding-left: 30px;">Step 3: 1 equals 6? No. 3 is still the lowest ancestor.</pre>
<pre style="padding-left: 30px;">Step 4: No more elements to compare. We're done.</pre>
<pre style="padding-left: 30px;">Step 5: Answer: 3.
</pre>
<p>Elementary, right? The function <em><strong>getLowestCommonAncestor</strong></em> does exactly what&#8217;s described here.</p>
<h1>Step 4?</h1>
<p>That&#8217;s it! We don&#8217;t need anything else. A straightforward solution is possible in 3 steps. <strong>Can you do better?</strong> I recommend you read some of the solutions posted at PP. There are incredible implementations there (e.g. <small><a href="http://bonsaicode.wordpress.com/2011/03/11/programming-praxis-lowest-common-ancestor/" target="_blank">Remco&#8217;s amazing Haskell solution</a>). </small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.elvismontero.com/2011/03/21/lowest-common-ancestor/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Locked</title>
		<link>http://www.elvismontero.com/2011/02/11/locked/</link>
		<comments>http://www.elvismontero.com/2011/02/11/locked/#comments</comments>
		<pubDate>Sat, 12 Feb 2011 00:31:50 +0000</pubDate>
		<dc:creator>emontero</dc:creator>
				<category><![CDATA[Pictures]]></category>

		<guid isPermaLink="false">http://www.elvismontero.com/?p=885</guid>
		<description><![CDATA[For the first time in all my terrestrial existence, I had the opportunity to visit my father&#8217;s hometown a couple of months ago. El Cercado, located in San Juan, Dominican Republic, is a small, colorful, and very cozy town. As soon as I got into what could conceivably be construed as the downtown area, an [...]]]></description>
			<content:encoded><![CDATA[<p>For the first time in all my terrestrial existence, I had the opportunity to visit my father&#8217;s hometown a couple of months ago. El Cercado, located in San Juan, Dominican Republic, is a small, colorful, and very cozy town. As soon as I got into what could conceivably be construed as the downtown area, an old, wooden house caught my eye. The house, showcasing no windows and in a very terrible shape, sported a seemingly brand new lock on its main door:</p>
<p style="text-align: center;"><img class="aligncenter" title="locked" src="http://www.elvismontero.com/pictures/locked-small.jpg" alt="lock picture" width="400" height="265" /></p>
<p>This image has been my computer&#8217;s wallpaper for the past couple of days. Even though I&#8217;d taken the picture a while ago, I haven&#8217;t really checked out that day&#8217;s collection until very recently. This is one of the many aspects I find so interesting about photography. A photo is a feelings-producer, transformative time machine: you experience some of the same feelings that made you aim your camera at the subject whenever you contemplate the shot, but these feelings are always nuanced and magnified by your evolving mind. It is truly remarkable.</p>
<p>If you&#8217;d like to download a hi-res version of the image, <a href="../pictures/locked.jpg" target="_blank">click here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elvismontero.com/2011/02/11/locked/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Golden Gate</title>
		<link>http://www.elvismontero.com/2011/01/24/golden-gate/</link>
		<comments>http://www.elvismontero.com/2011/01/24/golden-gate/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 21:17:46 +0000</pubDate>
		<dc:creator>emontero</dc:creator>
				<category><![CDATA[Pictures]]></category>

		<guid isPermaLink="false">http://www.elvismontero.com/?p=869</guid>
		<description><![CDATA[I&#8217;ve never been to San Francisco yet, but that didn&#8217;t stop me from thinking about the Golden Gate when I was photographing the Japanese Garden at El Jardín Botánico: Yes, I know this isn&#8217;t a suspension bridge. I also know this bridge is not connecting the Pacific Ocean to a huge landmass. However, I&#8217;d like [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve never been to San Francisco yet, but that didn&#8217;t stop me from thinking about the Golden Gate when I was photographing the Japanese Garden at <a href="http://www.jbn.gob.do/" target="_blank"><em>El Jardín Botánico</em></a>:</p>
<p style="text-align: center;"><img class="aligncenter" title="golden-gate" src="http://www.elvismontero.com/pictures/golden-gate-small.jpg" alt="" width="400" height="265" /></p>
<p>Yes, I know this isn&#8217;t a suspension bridge. I also know this bridge is not connecting the Pacific Ocean to a huge landmass. However, I&#8217;d like to imagine some people feel the same way I did when they&#8217;re in the presence of the real Golden Gate, or anything that&#8217;s both man-made and aesthetically pleasing.</p>
<p>If you&#8217;d like to download a hi-res version of the image, <a href="http://www.elvismontero.com/pictures/golden-gate.jpg" target="_blank">click here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elvismontero.com/2011/01/24/golden-gate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Niece</title>
		<link>http://www.elvismontero.com/2011/01/02/niece/</link>
		<comments>http://www.elvismontero.com/2011/01/02/niece/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 00:44:43 +0000</pubDate>
		<dc:creator>emontero</dc:creator>
				<category><![CDATA[Pictures]]></category>

		<guid isPermaLink="false">http://www.elvismontero.com/?p=842</guid>
		<description><![CDATA[My niece is a petite walking bundle of joy. She&#8217;s constantly laughing and swiveling her arms. Despite her age and diminutive size (she&#8217;s only one year old), she quickly becomes the center of attention as soon as she enters any room. I was fortunate enough to capture the following image while she entertained family members [...]]]></description>
			<content:encoded><![CDATA[<p>My niece is a petite walking bundle of joy. She&#8217;s constantly laughing and swiveling her arms. Despite her age and diminutive size (she&#8217;s only one year old), she quickly becomes the center of attention as soon as she enters any room. I was fortunate enough to capture the following image while she entertained family members during the recent holiday season:</p>
<p style="text-align: center;"><img class="aligncenter" title="leah-holidays-small" src="http://www.elvismontero.com/wp-content/uploads/2011/01/leah-holidays-small.jpg" alt="" width="400" height="265" /></p>
<p>I think the adjective you&#8217;re looking for is &#8220;badass&#8221;, right? <img src='http://www.elvismontero.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you&#8217;d like to download a hi-res version of the image, <a href="http://www.elvismontero.com/pictures/leah-holidays.jpg" target="_blank">click here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.elvismontero.com/2011/01/02/niece/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

