Binary Tree Rebuilder

Imagine you have a Binary Tree, with those characteristics: * Nodes do not respect any order relation - In other words: it's <strong>not</strong> a [Binary Search Tree](http://en.wikipedia.org/wiki/Binary_search_tree) of any kind * Every node appears <em>once and only once</em> within the tree A nice Binary Tree Then, your little brother passes by your desk and, to upset you, deletes the tree from your computer memory/HD (yeah, I know, I’m pathetic at inventing hypothetical situations :-P ). Fortunately though, you previously did a Pre-Order and an In-Order visit of your tree, and stored the result in an 2 nice array. Can you rebuild the original tree structure out of this 2 array? How are you going to rebuild it? Yes, you can! (Sorry, I couldn't resist). And it's quite easy as well. What you have to do, is the following: Take the first element of the PreOrder Array and use it as root of a new tree Find the position of this New Node in the InOrder Array, scanning it from 0 to n-1 (n is the number of Nodes) 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 0 to the position of the New Node in the InOrder Array -1. 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 position of the New Node in the InOrder Array +1 to n-1. Return the New Node By the way, this doesn’t work. To fix it we should be more generic, specifying things a little bit better. Things like: * Every recursive calls takes into account a portion of the InOrder array; in the case of the first call it's the entire array * There is going to be as much recursive calls as the number of elements in the PreOrder array Of course, is a tree what we are talking about here: recursion is a MUST. ...

February 5, 2010 · 5 min · 862 words

"Bidirectionally multiplied" array

Another small problem before I go to sleep tonight: There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n). ...

January 22, 2010 · 2 min · 281 words

Money change problem: Greedy vs. Dyn.Pro.

This is a classical problem of Computer Science: it’s used to study both Greedy and Dynamic Programming algorithmic techniques. I hate having my pocket full of copper!!! -_- Definition Given: A set of n Denominations D[0…n-1] in ascending order, representing a Monetary Coin System An money amount A, as input calculate a solution: * <strong><code>S[0...n-1]</code></strong>, with <code>0 &lt;= S[i] &lt;= (A/S[i])</code> and <code>0 &lt; i &lt; n-1</code> where: * <strong><code>A = Sum<sub>[i=0 -> n-1]</sub> { D[i] * S[i] }</code></strong> * <strong><code>Min{ Sum<sub>[i=0 -> n-1]</sub> { S[i] } }</code></strong> In other words Find the smallest amount of coins to make the given amount. ...

January 17, 2010 · 4 min · 778 words

Fibonacci's numbers calculator

Another simple-but-yet-interesting problem that I found challenging solving is the to Write a Fibonacci’s numbers calculator. It’s a REALLY SIMPLE problem, but still can demonstrate how superficial thinking in programming can lead to dramatically bad solutions. What’s a Fibonacci’s number A Fibonacci’s number is an integer number generated using the following function: Assumed that: f(0) = 0 f(1) = 1 for a generic “n” Integer: f(n) = f(n-1) + f(n-2) For example, the first 20 Fibonacci’s number are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 ...

January 5, 2010 · 11 min · 2288 words

Browser Adaptive CSS with AppEngine

As I said, I’m doing some stuff with Google AppEngine. And, of course, I’m facing the usual problem of Browser Incompatibility: Browser Incompatibility ;-) =Browser Incompatibilities: the Most Common Problem= The most common problem for Web Site developers is the fact that every browser treats HTML Tags, CSS and Javascript in it’s own way. This Recipe tries to address one of the problem I faced the most: having a slightly different CSS for every Browser. =The Usual Solution= The usual solution is to load every time a different CSS depending on the Browser. But this solution has some side effects: ...

September 21, 2008 · 1 min · 190 words