Skip to content

Commit 058525f

Browse files
linhvovnpivovarit
authored andcommitted
[tlinh2110@gmail.com] BAEL1377 - Recursion in Java (eugenp#3130)
* [tlinh2110@gmail.com] BAEL1377 - Recursion in Java * [BAEL1377] Remove unused method * [tlinh2110@gmail.com] Add sum method * [tlinh2110@gmail.com] Add example for iterative approach
1 parent 327719e commit 058525f

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.recursion;
2+
3+
public class BinaryNode {
4+
int value;
5+
BinaryNode left;
6+
BinaryNode right;
7+
8+
public BinaryNode(int value){
9+
this.value = value;
10+
}
11+
12+
public int getValue() {
13+
return value;
14+
}
15+
public void setValue(int value) {
16+
this.value = value;
17+
}
18+
public BinaryNode getLeft() {
19+
return left;
20+
}
21+
public void setLeft(BinaryNode left) {
22+
this.left = left;
23+
}
24+
public BinaryNode getRight() {
25+
return right;
26+
}
27+
public void setRight(BinaryNode right) {
28+
this.right = right;
29+
}
30+
31+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.recursion;
2+
3+
public class RecursionExample {
4+
5+
public int sum(int n){
6+
if (n >= 1){
7+
return sum(n - 1) + n;
8+
}
9+
return n;
10+
}
11+
12+
public int tailSum(int currentSum, int n){
13+
if (n <= 1) {
14+
return currentSum + n;
15+
}
16+
return tailSum(currentSum + n, n - 1);
17+
}
18+
19+
public int iterativeSum(int n){
20+
int sum = 0;
21+
if(n < 0){
22+
return -1;
23+
}
24+
for(int i=0; i<=n; i++){
25+
sum += i;
26+
}
27+
return sum;
28+
}
29+
30+
public int powerOf10(int n){
31+
if (n == 0){
32+
return 1;
33+
}
34+
return powerOf10(n-1)*10;
35+
}
36+
37+
public int fibonacci(int n){
38+
if (n <=1 ){
39+
return n;
40+
}
41+
return fibonacci(n-1) + fibonacci(n-2);
42+
}
43+
44+
public String toBinary(int n){
45+
if (n <= 1 ){
46+
return String.valueOf(n);
47+
}
48+
return toBinary(n / 2) + String.valueOf(n % 2);
49+
}
50+
51+
public int calculateTreeHeight(BinaryNode root){
52+
if (root!= null){
53+
if (root.getLeft() != null || root.getRight() != null){
54+
return 1 + max(calculateTreeHeight(root.left) , calculateTreeHeight(root.right));
55+
}
56+
}
57+
return 0;
58+
}
59+
60+
public int max(int a,int b){
61+
return a>b ? a:b;
62+
}
63+
64+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.recursion;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class RecursionExampleTest {
7+
8+
RecursionExample recursion = new RecursionExample();
9+
10+
@Test
11+
public void testPowerOf10() {
12+
int p0 = recursion.powerOf10(0);
13+
int p1 = recursion.powerOf10(1);
14+
int p4 = recursion.powerOf10(4);
15+
16+
Assert.assertEquals(1, p0);
17+
Assert.assertEquals(10, p1);
18+
Assert.assertEquals(10000, p4);
19+
}
20+
21+
@Test
22+
public void testFibonacci() {
23+
int n0 = recursion.fibonacci(0);
24+
int n1 = recursion.fibonacci(1);
25+
int n7 = recursion.fibonacci(7);
26+
27+
Assert.assertEquals(0, n0);
28+
Assert.assertEquals(1, n1);
29+
Assert.assertEquals(13, n7);
30+
}
31+
32+
@Test
33+
public void testToBinary() {
34+
String b0 = recursion.toBinary(0);
35+
String b1 = recursion.toBinary(1);
36+
String b10 = recursion.toBinary(10);
37+
38+
Assert.assertEquals("0", b0);
39+
Assert.assertEquals("1", b1);
40+
Assert.assertEquals("1010", b10);
41+
}
42+
43+
@Test
44+
public void testCalculateTreeHeight() {
45+
BinaryNode root = new BinaryNode(1);
46+
root.setLeft(new BinaryNode(1));
47+
root.setRight(new BinaryNode(1));
48+
49+
root.getLeft().setLeft(new BinaryNode(1));
50+
root.getLeft().getLeft().setRight(new BinaryNode(1));
51+
root.getLeft().getLeft().getRight().setLeft(new BinaryNode(1));
52+
53+
root.getRight().setLeft(new BinaryNode(1));
54+
root.getRight().getLeft().setRight(new BinaryNode(1));
55+
56+
int height = recursion.calculateTreeHeight(root);
57+
58+
Assert.assertEquals(4, height);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)