[tlinh2110@gmail.com] BAEL1377 - Recursion in Java (#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
This commit is contained in:
linhvovn 2018-02-24 15:11:23 +08:00 committed by Grzegorz Piwowarek
parent 327719e1bb
commit 058525f68b
3 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.baeldung.recursion;
public class BinaryNode {
int value;
BinaryNode left;
BinaryNode right;
public BinaryNode(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.recursion;
public class RecursionExample {
public int sum(int n){
if (n >= 1){
return sum(n - 1) + n;
}
return n;
}
public int tailSum(int currentSum, int n){
if (n <= 1) {
return currentSum + n;
}
return tailSum(currentSum + n, n - 1);
}
public int iterativeSum(int n){
int sum = 0;
if(n < 0){
return -1;
}
for(int i=0; i<=n; i++){
sum += i;
}
return sum;
}
public int powerOf10(int n){
if (n == 0){
return 1;
}
return powerOf10(n-1)*10;
}
public int fibonacci(int n){
if (n <=1 ){
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
public String toBinary(int n){
if (n <= 1 ){
return String.valueOf(n);
}
return toBinary(n / 2) + String.valueOf(n % 2);
}
public int calculateTreeHeight(BinaryNode root){
if (root!= null){
if (root.getLeft() != null || root.getRight() != null){
return 1 + max(calculateTreeHeight(root.left) , calculateTreeHeight(root.right));
}
}
return 0;
}
public int max(int a,int b){
return a>b ? a:b;
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.recursion;
import org.junit.Assert;
import org.junit.Test;
public class RecursionExampleTest {
RecursionExample recursion = new RecursionExample();
@Test
public void testPowerOf10() {
int p0 = recursion.powerOf10(0);
int p1 = recursion.powerOf10(1);
int p4 = recursion.powerOf10(4);
Assert.assertEquals(1, p0);
Assert.assertEquals(10, p1);
Assert.assertEquals(10000, p4);
}
@Test
public void testFibonacci() {
int n0 = recursion.fibonacci(0);
int n1 = recursion.fibonacci(1);
int n7 = recursion.fibonacci(7);
Assert.assertEquals(0, n0);
Assert.assertEquals(1, n1);
Assert.assertEquals(13, n7);
}
@Test
public void testToBinary() {
String b0 = recursion.toBinary(0);
String b1 = recursion.toBinary(1);
String b10 = recursion.toBinary(10);
Assert.assertEquals("0", b0);
Assert.assertEquals("1", b1);
Assert.assertEquals("1010", b10);
}
@Test
public void testCalculateTreeHeight() {
BinaryNode root = new BinaryNode(1);
root.setLeft(new BinaryNode(1));
root.setRight(new BinaryNode(1));
root.getLeft().setLeft(new BinaryNode(1));
root.getLeft().getLeft().setRight(new BinaryNode(1));
root.getLeft().getLeft().getRight().setLeft(new BinaryNode(1));
root.getRight().setLeft(new BinaryNode(1));
root.getRight().getLeft().setRight(new BinaryNode(1));
int height = recursion.calculateTreeHeight(root);
Assert.assertEquals(4, height);
}
}