Recursion - explanation how the code works (drawing) - java

i write this code and i try to understand all activities performed (recursive).
Can anyone help me with this draw me up a tree in memory of what happens?
public static String row(int n) {
if (n == 1)
return "1";
else
return row(n - 1) + " " + n;
}
public static String triangle(int a, int b) {
if (a == b)
return row(b);
else
return row(a) + "\n" + triangle(a + 1, b);
}
}
thank's

Your row method:
public static String row(int n) {
if (n == 1) {
return "1";
} else {
return row(n - 1) + " " + n;
}
}
Returns a String containing all the numbers from 1 - n separated by spaces. E.g.row(4) will return a string "1 2 3 4".
Your triangle method prints one row for each row of a triangle.
public static String triangle(int a, int b) {
if (a == b) {
return row(b);
} else {
return row(a) + "\n" + triangle(a + 1, b);
}
}
creates one row of a triangle for each step of row from a up to b.
e.g. triangle(4,6) will print:
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
How the code works
The row method, if given the value 1 will return "1". Any other value will result in a string equivalent to row(n-1) followed by n so essentially it will return 1 2 3 ... n.
The triangle method returns a string which attempts to draw a triangle with numbers. Thus triangle(4,6) returns row(4) + triangle(5,6) which returns row(5) + triangle(6,6) which will returns row(6). Thus the final result will be:
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

Related

Java simple recursion explanation

I was implementing a very simple recursive method to multiply two numbers together. I am very a hard time understanding the basics of recursion.
Is anyone able to explain to me (Line-by-line if possible) how this code works? I am especially confused about the base case is written to return 0 when in reality the actual multiplication is returned instead.
public static int multiply(int n, int m) {
if (m == 0) {
return 0; //Base case
}
return multiply(n, m - 1) + n; //Recursive Case - Decrements m, while adding n
}
Thanks for any help
I will try to make this answer beginner friendly. First I will answer this part of your question:
I am especially confused about the base case is written to return 0
when in reality the actual multiplication is returned instead.
The value returned in the base case depends on how you are implementing your algorithm. Here in order to calculate n*m, we extend this multiplication to addition. A few examples will make this more clear.
2*3 = 2 + 2 + 2 + 0
4*1 = 4 + 0
5*5 = 5 + 5 + 5 + 5 + 5 + 0
n*m = n + n + n + ... + (m times) + 0
That is why we return 0 in the base case, we have to stop the recursion without making any change in the result and that is only possible if we add 0.
Here is the working of this program:
public static int multiply(int n, int m) {
if (m == 0) {
return 0; //Base case
}
return multiply(n, m - 1) + n;
Let us take n = 4 and m = 3.
Our first call to the method is multiply(4, 3). We then proceed further, the base condition is false so we skip that.
We then get to this part: return multiply(n, m - 1) + n. We make another call to the same function. multiple(4, 2). Again we skip the base case. Here is a table:
multiply(4, 3) -> return multiply (4, 2) + 4
multiple(4, 2) -> return multiply (4, 1) + 4
multiple(4, 1) -> return multiply (4, 0) + 4
multiple(4, 0) -> 0
On substituting the returned values we get,
multiple(4, 3) -> return 0 + 4 + 4 + 4
multiple(4, 2) -> return 0 + 4 + 4
multiple(4, 1) -> return 0 + 4
multiple(4, 0) -> return 0
If our first call is multiply(n, m), then the end returned value is:
multiply (n, m) -> return 0 + n + n + n + ... + (m times)
I hope I have helped you. Try to construct a similar relation like this on your own and you will understand it better. Here is a link on further explanation on recursion:
https://www.geeksforgeeks.org/recursion

Why is my test case failing for the hailstones method?

Returns a string consisting of a Hailstone sequence beginning with the positive integer n and ending with 1. The
string should consist of a sequence of numerals, with each numeral followed by a single space. When a numeral m
(other than 1) appears in the sequence, it should be followed by nextHailstone(m).
Examples: nextHailstone(1) is "1 " and nextHailstone(5) is "5 16 8 4 2 1 ".
public static String hailstones (int n)
{
int calculation = 1;
System.out.print(n + " ");
while (n > 1)
{
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = (n*3) + 1;
}
calculation++;
System.out.print(n + " ");
}
return " ";
}
The code works fine when I call the method in the main method but the test case for it is failing.
#Test
public void testHailstones ()
{
assertEquals("1 ", hailstones(1));
assertEquals("16 8 4 2 1 ", hailstones(16));
assertEquals("7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 ", hailstones(7));
}
Those are the test cases. any insight into this would be great. thanks!
You return " " every time the function is called. You need to build up an internal string and return that as result.

Turn Assembler algorithm to a Java Sequence calculator

When I enter "3" in my java code, it prints
3 will be multiplied by 3 and +1
Value is 10
Any advice as to how I should modify it, so that it calculates the sequences correctly
import javax.swing.*;
public class sequences {
/**
* #param args
*/
public static void main(String[] args) {
calculateSequences();
}//ends main
public static void calculateSequences()
{
int value;
String valueInput = JOptionPane.showInputDialog("Value");
value = Integer.parseInt(valueInput);
if(value == 1)
{
System.out.println("Value is equal to 1, closing down");
System.exit(0);
}
else if ((value%2)==0)
{
System.out.println(value + " will be divided by 2");
value = value/2;
System.out.println("Value is even " + value);
}
else
{
System.out.println(value + " will be multiplied by 3 and +1");
value = 3*value+1;
System.out.println("Value is " + value);
}
}//ends calculateSequences
}//ends class
You forgot the "go to start"
Here are the results from one test.
23 will be multiplied by 3 and + 1
Value is 70
70 will be divided by 2
Value is even 35
35 will be multiplied by 3 and + 1
Value is 106
106 will be divided by 2
Value is even 53
53 will be multiplied by 3 and + 1
Value is 160
160 will be divided by 2
Value is even 80
80 will be divided by 2
Value is even 40
40 will be divided by 2
Value is even 20
20 will be divided by 2
Value is even 10
10 will be divided by 2
Value is even 5
5 will be multiplied by 3 and + 1
Value is 16
16 will be divided by 2
Value is even 8
8 will be divided by 2
Value is even 4
4 will be divided by 2
Value is even 2
2 will be divided by 2
Value is even 1
Value is equal to 1, closing down
Here's your code with a while clause added.
import javax.swing.JOptionPane;
public class Sequences {
/**
* #param args
*/
public static void main(String[] args) {
calculateSequences();
}// ends main
public static void calculateSequences() {
int value;
String valueInput = JOptionPane.showInputDialog("Value");
value = Integer.parseInt(valueInput);
while (value > 0) {
if (value == 1) {
System.out.println("Value is equal to 1, closing down");
System.exit(0);
} else if ((value % 2) == 0) {
System.out.println(value + " will be divided by 2");
value = value / 2;
System.out.println("Value is even " + value);
} else {
System.out.println(value + " will be multiplied by 3 and + 1");
value = 3 * value + 1;
System.out.println("Value is " + value);
}
}
}// ends calculateSequences
}
You need to add iteration or recursion. Since this is Java, it would make most sense to use iteration.
This corresponds to the line "go to start ;" in the algorithm.
For this you can use a while loop, such as while(true).
public static void main(String[] args) {
process(Integer.parseInt(args[0]));
}
private static void process(int n) {
while (n != 1) {
if (n%2 == 0) {
System.out.println(n + " will be divided by 2");
n = n/2;
System.out.println("Value is even " + n);
}
else {
System.out.println(n + " will be multiplied by 3 and +1");
n = 3*n+1;
System.out.println("Value is " + n);
}
}
System.out.println("Value is equal to 1, closing down");
}
And the output:
3 will be multiplied by 3 and +1
Value is 10
10 will be divided by 2
Value is even 5
5 will be multiplied by 3 and +1
Value is 16
16 will be divided by 2
Value is even 8
8 will be divided by 2
Value is even 4
4 will be divided by 2
Value is even 2
2 will be divided by 2
Value is even 1
Value is equal to 1, closing down

Sum values in binary tree of integers weighted by depth

Here is my reference tree:
3
/ \
5 2
/ / \
1 4 6
Here is the expected output of the recursive method:
(1*3) + (2 * (5 + 2)) + (3 * (1 + 4 + 6)) = 50
...and here is the code I have thus far:
public int depthSum()
{
int depth = 1;
return depthSum(overallRoot, depth);
}
public int depthSum(IntTreeNode someNode, int someDepth)
{
if(someNode == null)
{
return 0;
}
else
{
return someDepth * someNode.data + //some recursion
}
}
I get that I have to likely call myself and increment someDepth, but I can't seem to get this right. Any ideas?
Presumably you mean:
return someDepth * someNode.data +
depthSum(someNode.left, someDepth+1) +
depthSum(someNode.right, someDepth+1);

Binary Tree diff of sum of nodes at odd and sum of nodes at even

How can I write the function to return the difference of the sum of values of nodes at odd height and the sum of values of nodes at even height. Considering the root node is at height 1 for a binary tree
input:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Output: -74 Explanation :
[ (1 + 4 + 5 + 6 + 7 ) - (2 + 3 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15) = -74 ]
Code:
public static int diff(Node n) {
if (n == null)
return 0;
return Sum(n) - Sum(n.left) - Sum(n.right);
}
public static int Sum(Node root) {
int sum = 0;
if (root == null) {
return sum;
}
sum = sum + root.data;
if (root.left != null) {
sum = sum + Sum(root.left.left) + Sum(root.left.right);
}
if (root.right != null) {
sum = sum + Sum(root.right.left) + Sum(root.right.right);
}
return sum;
}
I have given this solution but not selected... I don't know whats wrong with this.
public static int Sum(Node root) {
if (root == null) {
return 0;
}
return root.data-Sum(root.left)-Sum(root.right);
}
This is easiest and smartest way i found the solution
Here solution is explained using recusrsion in short..
Negate all levels under the current one (the level of the current node) and you do that on each step of the recursion.
sum[l1] – (sum[l2] – (sum[l3] – (sum[l4] – … = sum[l1] – sum[l2] + sum[l3] – sum[l4]…
www.crazyforcode.com/binary-tree-diff-sum-even-nodes-sum-odd-nodes/
How about...
public static int diff(Node n) {
return sumtree(Node n, 1);
}
public static int sumtree(Node n, int level) {
if (n == null) return 0;
if (level % 2 == 0) {
return sumtree(n.left, level + 1) + sumtree(n.right, level +1 ) - n.value;
} else {
return sumtree(n.left, level + 1) + sumtree(n.right, level + 1) + n.value;
}
}
Add values on odd level numbers (1, 3, 5 7...), subtract on even (2, 4, 6, 8...).
The approach I took was to first find a way of adding all of the leaves, then just add a "level factor" if the level is even you you add otherwise you subtract. This may look similar to others but I find it cleaner.
On ANSI C
int diffOddAndEven(Node *root)
{
return sumLevel(root, 0);
}
int sumLevel(Node *root, int level)
{
int sum=0;
int levelFactor = level%2 ? -1 : 1;
if(!root)
return 0;
sum = root->value * levelFactor;
sum += sumLevel(root->left, level+1);
sum += sumLevel(root->right, level+1);
return sum;
}
Check out my solution
traverse(root,1); //1 is passed since we want oddlevelsum - evenlevelsum,pass -1 for opposite
int traverse(Tree* root,int level){
if(root==NULL)return 0;
return (level*root->data)+ traverse(level*-1,root->left_node)
+ traverse(level*-1,root->right_node);
}

Categories

Resources