Recursively creating a tree with binomial coefficient - java

I'm trying to recursively populate a tree with integers (as shown in the link below). But I don't understand how to solve the inductive step using recursion.
PS. I have already created the recursive function to calculate the binomial coefficient
Thank you in advance!

The math notation in the picture at the link is a little obscure. But you want to implement that almost exactly. It's saying this:
Node buildTree(n) {
Let tree be a new Node (with no children)
For i from 0 to n - 1
Let b = binomial(n, i)
for b times:
Add buildTree(i) as a child of tree
return tree
}
The obscure part is that multiplication by some integer k in this "tree math" means "repeat the graph k times," and the summation means "add all as children" to an implicit parent node. This parent is the value of the expression.
Here's a development hint. Store a unique integer in each node as a label. Once you get that working, then write a little tree walker that prints out DOT language. When I did that for n=3, I got:
graph {
0 -- 1
2 -- 3
0 -- 2
4 -- 5
0 -- 4
6 -- 7
0 -- 6
8 -- 9
10 -- 11
8 -- 10
12 -- 13
8 -- 12
0 -- 8
14 -- 15
16 -- 17
14 -- 16
18 -- 19
14 -- 18
0 -- 14
20 -- 21
22 -- 23
20 -- 22
24 -- 25
20 -- 24
0 -- 20
}
You can see what that looks like by using online GraphViz here.

Pseudocode to illustrate recursive step
TreeNode getRootOfSomeFancyTree(n)
TreeNode root = new TreeNode()
if(n == 1) return root
else for(i = 0; i < n; i++)
//not sure of correct way to multiply an integer by a tree in your application
int x = binomialCoefficient(n, i) * i
//anyway, recurring is easy and safe provided x < n
TreeNode subTree = getRootOfSomeFancyTree(x)
//build the tree
root.addChild(subTree)
endfor return root

Related

What is the sum of binary tree with given root and target nodes?

I have a binary tree
as a function input I have tree root and 2 nodes
I need to calculate the sum along the path between the two given nodes.
A tree example:
4
/ \
8 13
/ \
24 45
Code:
List<Node> findPath(root, target):
if (root !=null)
return
if root == node{
return nodes.add(target)
}
path = findPath(root.left, target)
if (path !=null){
return nodes.add(root).addAll(path)
}
path = findPath(root.right, target)
if (path!=null)
return nodes.add(root).addAll(path)
I don't know what is the next step if I have paths to target nodes how should I calculate optimal way?
Input: sumTree(4, 24, 45)
Output: 8 + 24 + 45 = 77
Input: sumTree(4, 24, 13)
Output: 13 + 4 + 8 + 24 = 49
Input: sumTree(4, 4, 13)
Output: 4 + 13 = 17
Input: sumTree(4, 45, 45)
Output: 45
Language is JAVA but language doesn't matter unless I understand the syntax
I just want to have optimal solution.
Is it possible to provide some pseudocode?
Your two paths will have the same prefix (at least the root should be there).
You need to remove the common prefix and add only the last (deepest) common node (once). For the parts that are different you need to add all the values. This should be O(N) complexity, and in-line with the rest of the solution.
Your search algorithm is not efficient because you keep copying the values from one list to the other (O(N^2) if you don't have any constraints on the tree). If you modify it to build the response in place it should become O(N).

Efficient Data structure and Algorithm - Natural Sequence

1 - 1
2 - 2,3
3 - 4,5,6
4 - 7,8,9,10
Given any number from 4 to 6 ,I need the output as 3.
Given any number from 7 to 10 ,I need the output as 4.
I need the fastest solution for the above problem to solve an algorithm.
What I could think of is a brute force algorithm :
Given 7:
n-square + n = 7*2 = 14
1 + 1 = 2 < 14
4 + 2 = 6 < 14
9 + 3 = 12 < 14
16+ 4 = 20 >=14 --> So 4
Is there any better approach to arrive at the solution ? OR My approach to the algorithm itself is flawed ?
Brief explanation of the algo :
A,B,C
After every iteration every element becomes increased by one.
A,A,B,B,C,C
Given 3, C will be returned.
Given 4 or 5, A will be returned.
Given 6 or 7, B will be returned.
Given 8 or 9, C will be returned.
Given 10 or 11 or 12, A will be returned.
Given 13 or 14 or 15, B will be returned.
How the solution to the mathematical problem will help solve the algo :
Total number of elements = 3
Given number = 13 (Output to be B)
Divide and Ceiling = Ceil (13/3) = 5 [So 13 falls under when every element has become * 3] (From Mathematical problem : If given number is 5, 3 is to be used)
Starting index of when every element has become * 3 [IS_EQUAL_TO = ] 3 * 3(summation of previous iteration => 1 + 2) + 1 = 10
To Find the index = Ceil(13-10+1/3 (this 3,comes from the mathematical problem) ) = Ceil (4/3) = 2nd index = B
Given number of rows N, the size of the triangle is N(N+1)/2. You are essentially trying to find the least integer N such that N(N+1)/2 >= M, with M given. If you have a function to compute square roots, you can solve this equation in constant time.
N(N+1)/2 >= M, multiply both sides with 2,
N²+N >= 2M, complete the square, take the square root, blablabla
N >= sqrt(2M+1/4)-1/2
Therefore the answer is N = ceil(sqrt(2*M + .25) - .5)

How would I recreate a binary tree from its inorder and postorder values?

Inorder: 3 2 1 5 4 6 8 9 7 11 10
Postorder: 1 2 3 4 5 6 9 11 10 7 8
I believe I am on the right track by taking the last postorder value as the root, finding that value in the Inorder list and splitting the tree to the left and the right of that value. From what I've been trying, I was able to come up with this result: It was not formatting correctly so I screenshot it.
Get the last element from the Postorder, this is your root element, then find that element in the Inorder and split the elements in left and right list giving you LEFT: "3 2 1 5 4 6" and RIGHT: "9 7 11 10" then walk the Postorder list and split it once you find the first number before the index of the root element in the Inorder list, in this case this is "6" so walk till 6 and this will give you "1 2 3 4 5 6" and the rest is "9 11 10 7". Then insert those lists in reverse order, e.g.: "6 5 4 3 2 1" and after that "7 10 11 9" this should give you the correct order of the tree.
It is a little tricky, maybe because contradicts a little the brain hemispheres ;-)
The parameters of interest are:
post: an array containing the postorder traversal
lp: left index of post array
rp: right index of post array
in: an array containing the inorder traversal
li: left index of in array
ri: riggh index of ìn array
The procedure is "naturally" recursive. At each recursion, the root always is post[rp]. That is the last visited node (in postorder).
So, the first thing to do is to know the index of the root in the array in. In order to compute that, we scan from li to ri and we search post[rp]. Let i the index of root in the array in. I assume that the tree does not have duplicated keys.
Given the index of root, then
i - li is the number of nodes in the left subtree
ri - i is the number of nodes in the right subtree
Now, in becomes naturally partitioned. The left subtree is between [li, i - 1] and the right subtree is between [i + 1, ri].
What I think is a little confuse is to determine where are the subtrees in post. The left subtree is between [lp, lp + (i - li) - 1] and the right is between [rp - (ri - i), rp - 1]. Take in account the numbers of nodes of each subtree expressed above (in the enumerated list).
With this knowledge we would be ready for designing an algorithm (I write in pseudo-C++, but I think it is very easy to translate to java):
Node * build_postorder(const vector<int> & post, long lp, long rp,
const vector<int> & in, long li, long ri)
{
if (lp > rp)
return nullptr; // we stop recursion when the tree is empty
Node * root = new Node(post[rp]); // Creates the root with key value post[rp]
int i = li;
for (; i <= ri; ++i) // search in inorder array the index of root
if (in[i] == post[rp])
break; // this line must always to execute it (if everything is oK)
LLINK(root) = build_postorder(post, lp, lp + (i - li) - 1, in, li, i - 1);
RLINK(root) = build_postorder(post, rp - (ri - i), rp - 1, in, i + 1, ri);
return root;
}
Good luck!

Generating a Full Binary Tree Recursively through a Java Method

The method is supposed to take in two parameters one for depth and one for the integer value of the root of the tree. For Example: for any given N, returns the root reference of a full binary search tree of depth N such that the nodes store the integers 1, 2, …, 2 N+1 – 1. I'm struggling to get this right. Here is what I have:
public static BinaryNode BSTFactory(int top,int depth) {
BinaryNode root=new BinaryNode(null,null,top);
BinaryNode leftChild,rightChild;
if(depth==0){
return root;
}
if(depth==1){
//create 2 children left and right
leftChild=new BinaryNode(null,null,top-1);
rightChild=new BinaryNode(null,null,top+1);
root=new BinaryNode(rightChild,leftChild,top);
return root;
}
if(depth>1){
leftChild=BSTFactory(top-1,depth-1);
rightChild=BSTFactory(top+1,depth-1);
root=new BinaryNode(rightChild,leftChild,top);
return root;
}
return root;
}
First of all, the two parameters of your method depend on each other. For example, BSTFactory(1,3) can't be a full binary tree with a minimal node of 1, since if the root already contains the minimal node, the left sub-tree must be empty (unless you allow negative values in your tree, which is not clear from your question, since you seem to want the tree to store integers starting from 1).
Therefore, I would suggest a wrapper method that would accept only the depth, and calculate the matching root node. We'll see how the two parameters are related later.
Now let's look at some small full binary trees to figure out the recursion :
Depth 0
1
Depth 1
2
1 3
Depth 2
4
2 6
1 3 5 7
Depth 3
8
4 12
2 6 10 14
1 3 5 7 9 11 13 15
What can we learn from these examples?
If we are creating a full binary search tree of depth n :
The root would be 2^n
The left sub-tree will be rooted at root - 2^(n-1)
The right sub-tree will be rooted at root + 2^(n-1)
Therefore, the recusion should be :
public static BinaryNode BSTFactory(int root, int depth)
{
BinaryNode leftChild,rightChild;
if (depth==0){
return new BinaryNode(null,null,root);
} else {
leftChild=BSTFactory(root-Math.pow(2,depth-1),depth-1);
rightChild=BSTFactory(root+Math.pow(2,depth-1),depth-1);
return new BinaryNode(rightChild,leftChild,root);
}
}
Note that in order for this to work (i.e. that your minimal node would be 1), you must call the method with a root and depth such that root=2^depth. To ensure that, lets define a wrapper method :
public static BinaryNode BSTFactory(int depth)
{
return BSTFactory (Math.pow(2^depth),depth);
}
If you call the two parameter method with arbitrary root and depth, you can get binary trees such as :
BSTFactory (6,1)
6
5 7
BSTFactory (1,2)
1
-1 3
-2 0 2 4
There are still full binary trees, but their minimal value is not 1.

Simple division in Java - is this a bug or a feature?

I'm trying this simple calculation in a Java application:
System.out.println("b=" + (1 - 7 / 10));
Obviously I expect the output to be b=0.3, but I actually get b=1 instead.
What?! Why does this happen?
If I write:
System.out.println("b=" + (1 - 0.7));
I get the right result, which is b=0.3.
What's going wrong here?
You're using integer division.
Try 7.0/10 instead.
You've used integers in the expression 7/10, and integer 7 divided by integer 10 is zero.
What you're expecting is floating point division. Any of the following would evaluate the way you expected:
7.0 / 10
7 / 10.0
7.0 / 10.0
7 / (double) 10
Please do not take this as an answer to the question. It is not, but an advice related to exploiting the difference of int and float. I would have put this under a comment except that the answer box allows me to format this comment.
This feature has been used in every respectable programming language since the days of fortran (or earlier) - I must confess I was once a Fortran and Cobol punch card programmer.
As an example, integer division of 10/3 yields integer value 3 since an integer has no facility to hold fractional residual .3333.. .
One of the ways we (old time ancient programmers) had been using this feature is loop control.
Let's say we wish to print an array of 1000 strings, but we wish to insert a line break after every 15th string, to insert some prettyfying chars at the end of the line and at the beginning of the next line. We exploit this, given that integer k is the position of a string in that array.
int(k/15)*15 == k
is true only when k is divisible by 15, an occurrence at a frequency of every 15th cell. Which is akin to what my friend said about his grandfather's dead watch being accurate twice a day.
int(1/15) = 0 -> int(1/15)*15 = 0
int(2/15) = 0 -> int(2/15)*15 = 0
...
int(14/15) = 0 -> int(14/15)*15 = 0
int(15/15) = 1 -> int(15/15)*15 = 15
int(16/15) = 1 -> int(16/15)*15 = 15
int(17/15) = 1 -> int(17/15)*15 = 15
...
int(29/15) = 1 -> int(29/15)*15 = 15
int(30/15) = 2 -> int(30/15)*15 = 30
Therefore, the loop,
leftPrettyfy();
for(int k=0; k<sa.length; k++){
print(sa[k]);
int z = k + 1;
if ((z/15)*15 == z){
rightPrettyfy();
leftPrettyfy();
}
}
By varying k in a fanciful way in the loop, we could print a triangular printout
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
That is to demonstrate that, if you consider this a bug, this "bug" is a useful feature that we would not want to be removed from any of the various languages that we have used thus far.
I find letter identifiers to be more readable and more indicative of parsed type:
1 - 7f / 10
1 - 7 / 10f
or:
1 - 7d / 10
1 - 7 / 10d
In my case I was doing this:
double a = (double) (MAX_BANDWIDTH_SHARED_MB/(qCount+1));
Instead of the "correct" :
double a = (double)MAX_BANDWIDTH_SHARED_MB/(qCount+1);
Take attention with the parentheses !

Categories