Tree permutations

Write a program to calculate the different number of tree structures possible,
given n identical values.
For Example:

For one node, the tree is a trivial one node tree.
For two nodes, two combinations are possible:
O O
\ and /
O O

For three nodes :
O O O O O
/ / / \ \ \
O O O O O O
/ \ \ /
O O O O


Answer:



/*
For the above diagrams, we can see the recursive property,
i.e,
For given N nodes:
* One node becomes the root
* Divide the remaining N-1 nodes between left and right subtrees, and this can be
done as follows:
Left Subtree Right Subtree
0 N-1
1 N-2
2 N-3
... ...
N-1 0

Summing up the above combinations, to get the value of N,
The solution can be recursively defined as following:

{ 1 if n == 1 }
F(n) = { sigma( F(i) * F(n-i-1 ) where i = 0 to n-1 }

note that its F(i) * F(n-i-1) , product, not a sum because its a combinatorics problem,
If event A can be in m ways, and another event B can be done in n ways, then the total
possible events are (m x n)

Note that optimization can be achived by caching the results in the recursive call.


*/


#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
// the cache used to "cache the results in a recursive call"
vector cache;
size_t TreeCombinations(size_t n)
{
if ( (n == 1) || (n == 0) )
return cache[n];
// Check if the result was already computed
if ( cache[n] != 0 )
return cache[n];
else
{
int i = 0;
size_t combinations = 0;
//printf("{ %d \n", n);
for ( i =0 ; i <= n-1; ++i)
{
size_t tmp = TreeCombinations(i) * TreeCombinations(n-i-1);
//printf(" f(%d) * f(%d) = %d \n", i, n-i-1, tmp );
combinations += tmp;
}
//printf("}\n");
cache[n] = combinations;
}

return cache[n];
}

int main(int argc, char* argv[])
{
size_t n = atoi(argv[1]);
// initialize the cache
cache.reserve(n+1);
for ( int i = 0; i <= n; ++i )
{
cache[i] = 0;
}
cache[0] = 1;
cache[1] = 1;
// ----
return printf("Tree combinations for %d nodes = %d \n", n, TreeCombinations(n) );
}

Clumps in a Binary Tree

Assume that each node in a binary tree represents a color. A clump is defined as 3 or more adjacent nodes with same color.
Write an algorithm to determine the number of clumps.
For example:

R
/ \
R R
/ \ / \
B B R G
/ \ / \
B B G G
\
G
The Above tree has 3 clumps:
4 Adjacent Reds starting at the root.
4 adjacent Greens in the right subtree.
3 adjacent Blues in the left subtree.


Answer:

Here is the solution in C++:

#include
using namespace std;
// The colors of the tree
enum Color
{
Unknown,
Red,
Green,
Blue
};

struct TreeNode
{
Color color;
TreeNode* left;
TreeNode* right;

TreeNode(Color c, TreeNode* _left=0, TreeNode* _right=0):
color(c), left(_left), right(_right){}
};

void CountClumps(TreeNode* node, Color parentColor, size_t &curCount, size_t &clumpCount)
{
if( node )
{
if (node->color != parentColor)
{
/* The color of the current node is not equal to the
* parent node's color, then we dont increase the curCount, but start a new
* clumpCount */
size_t _curCount = 1; // 1 because of the current node color
CountClumps(node->left, node->color, _curCount, clumpCount);
CountClumps(node->right, node->color, _curCount, clumpCount);
if ( _curCount >= 3 )
++clumpCount;
}
else
{
/* increase the curCount ( which signifies the nodes with same color )
* and let the parent decide about the clump, and traverse the left and
* right subtrees
* */
++curCount;
CountClumps(node->left, node->color, curCount, clumpCount);
CountClumps(node->right, node->color, curCount, clumpCount);
}
}
}

int main(int argc, char* argv[])
{
// The left subtree
TreeNode *n1 = new TreeNode(Green,0,0);
TreeNode *n2 = new TreeNode(Green,0,n1);
TreeNode *n3 = new TreeNode(Green,0,0);
TreeNode *n4 = new TreeNode(Green,n3,n2);

TreeNode *n5 = new TreeNode(Red,0,0);
TreeNode *n6 = new TreeNode(Red,n5,n4);

// The right subtree
TreeNode *n7 = new TreeNode(Blue, 0,0);
TreeNode *n8 = new TreeNode(Blue, 0,0);
TreeNode *n9 = new TreeNode(Blue, n7,n8);
TreeNode *n10 = new TreeNode(Blue, 0, 0);
TreeNode *n11 = new TreeNode(Blue, n10, n9);

// The Root
TreeNode *root = new TreeNode(Red, n11, n6);

size_t curCount = 0;
size_t clumpCount = 0;

CountClumps( root, Unknown, curCount, clumpCount );

cout << "Clumps " << clumpCount << endl;

// set the left subtree to null
curCount = 0;
clumpCount = 0;
root->left = 0;
CountClumps( root, Unknown, curCount, clumpCount );

cout << "Clumps " << clumpCount << endl;

// set the right subtree to null
curCount = 0;
clumpCount = 0;
root->right= 0;
root->left = n11;
CountClumps( root, Unknown, curCount, clumpCount );

cout << "Clumps " << clumpCount << endl;

// Sorry forgot to release the memory of the TreeNode :D
return clumpCount;
}


Matrix Multiplication

Write code for matrix multiplication.

Answer:



(M x N) * (N x P) = (M x P) matrix
^^^^^^^

N
---->
1 2 3 x 1 4 |
4 5 6 2 5 | N
3 6 \/

From the above diagram, it can be infered that
M and P will form the outer loops and N will be the
innermost loop.

for ( i = 0 to M )
for ( j = 0 to P )
{
a[i][j] = 0;
for ( k = 0 to N )
{
a[i][j] += a[i][k] * a[k][j];

}
}