In: Computer Science
Recursion java:
1. Write a recursive algorithm to add all the elements of an array of n elements
2. Write a recursive algorithm to get the minimum element of an array of n elements
3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C
.4. Write a recursive algorithm to get the maximum element of a binary tree
5. Write a recursive algorithm to get the number of elements of a binary tree
1. Recursive algorithm to add elements of an array:
// ArraySum.java
import java.util.Scanner;
public class ArraySum
{
static int findSum(int array[], int n){
if(n<=0)
return 0;
return (findSum(array, n-1) + array[n-1]);
}
public static void main(String[] args) {
System.out.println("Enter number of elements: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int array[] = new int[n];
System.out.println("Enter elements:");
for(int i=0; i<n; i++)
array[i] = sc.nextInt();
int sum = findSum(array, n);
System.out.println("Sum of array elements: " + sum);
}
}
Output:
Enter number of elements:
5
Enter elements:
1 2 3 4 5
Sum of array elements: 15
2. Recursive algorithm to get the minimum element of an array:
// ArrayMinimum.java
import java.util.Scanner;
public class ArrayMinimum
{
static int findMin(int array[], int n){
if(n==1)
return array[0];
return Math.min(array[n-1], findMin(array, n-1));
}
public static void main(String[] args) {
System.out.println("Enter number of elements: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int array[] = new int[n];
System.out.println("Enter elements:");
for(int i=0; i<n; i++)
array[i] = sc.nextInt();
int min = findMin(array, n);
System.out.println("Min of array elements: " + min);
}
}
Output:
Enter number of elements:
3
Enter elements:
22 -30 10
Min of array elements: -30
3. Recursive algorithm to add the corresponding elements of two arrays and store them in third array.
// ArraySum.java
import java.util.Scanner;
public class ArraySum
{
static int[] findSum(int[] A, int[] B, int[] C, int n){
if(n<=0)
return C;
C[n-1] = A[n-1] + B[n-1];
return findSum(A, B, C, n-1);
}
public static void main(String[] args) {
System.out.println("Enter number of elements: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int A[] = new int[n];
int B[] = new int[n];
System.out.println("Enter elements of first array:");
for(int i=0; i<n; i++)
A[i] = sc.nextInt();
System.out.println("Enter elements of second array:");
for(int i=0; i<n; i++)
B[i] = sc.nextInt();
int C[] = new int[n];
findSum(A, B, C, n);
System.out.println("Sum Array:");
for(int i=0; i<n; i++)
System.out.print(C[i] + " ");
}
}
Output:
Enter number of elements:
5
Enter elements of first array:
1 2 3 4 5
Enter elements of second array:
1 2 3 4 5
Sum Array:
2 4 6 8 10
4. Recursive program to find the minimum element of binary tree.
// BinaryTreeMaximum.java
class Node{
int data;
Node left, right;
public Node(int data){
this.data = data;
left = right = null;
}
}
class BinaryTree{
Node root;
static int findMax(Node node){
if(node == null)
return Integer.MIN_VALUE;
int res = node.data;
int l = findMax(node.left);
int r = findMax(node.right);
if(l>r)
res = l;
if(r>res)
res = r;
return res;
}
}
public class BinaryTreeMaximum
{
public static void main(String[] args) {
BinaryTree t = new BinaryTree();
t.root = new Node(10);
t.root.left = new Node(25);
t.root.right = new Node(30);
t.root.left.left = new Node(50);
t.root.left.right = new Node(45);
System.out.println("Maximum element in binary tree: " + t.findMax(t.root));
}
}
Output:
Maximum element in binary tree: 50
5. Recursive program to find the number of elements in binary tree.
// BinaryTreeElements.java
class Node{
int data;
Node left, right;
public Node(int data){
this.data = data;
left = right = null;
}
}
class BinaryTree{
Node root;
int size(){
return size(root);
}
int size(Node node){
if(node == null)
return 0;
else
return (size(node.left) + 1 + size(node.right));
}
}
public class BinaryTreeElements
{
public static void main(String[] args) {
BinaryTree t = new BinaryTree();
t.root = new Node(10);
t.root.left = new Node(25);
t.root.right = new Node(30);
t.root.left.left = new Node(50);
t.root.left.right = new Node(45);
System.out.println("Number of elements in binary tree: " + t.size());
}
}
Output:
Number of elements in binary tree: 5
Note: If reader wants to copy and use the code provided in the answer, the reader has to make sure to replace [NBSP] with spaces, if found any in the code. Else the program may not compile/run successfully.