Question

In java 1. Write a recursive algorithm to add all the elements of an array of...

In 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

Homework Answers

Answer #1

1. Write a recursive algorithm to add all the elements of an array of n elements

static int Cal_Sum(int arr[], int n) 
{ 
    if (n <= 0)     // base condition
        return 0; 
    return (Cal_Sum(arr, n - 1)+arr[n - 1]);   // recursive call
}

2. Write a recursive algorithm to get the minimum element of an array of n elements

public static int find_MAX(int arr[], int n) 
{ 
    if(n == 1) // base condition 
    return arr[0];  
    return Math.max(arr[n-1], find_MAX(arr, n-1));   //recursive call
} 

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.

static int[] fun(int a[], int b[],int c[],int n)
{
    if(n==0)    //base condition
    return c;
    c[n-1]=a[n-1]+b[n-1];   //add sum to c array 
    return fun(a,b,c,n-1);   //recursive call
}

4. Write a recursive algorithm to get the maximum element of a binary tree

static int MAX_ELEMENT(Node node) 
{ 
    if (node == null)             // base condition
        return Integer.MIN_VALUE; 
    int ans = node.data; 
    int left_ans = MAX_ELEMENT(node.left);   //recursive call
    int right_ans = MAX_ELEMENT(node.right); //recursive call
    if (right_ans > ans)         // finding max
        ans = right_ans;
    if (left_ans > ans)          // finding max
        ans = left_ans; 
    
    return ans; 
} 

5. Write a recursive algorithm to get the number of elements of a binary tree

static int Count_Nodes(Node root)
{
    if(root==null)  //  base condition
        return 0;
    return (1 + Count_Nodes(root.left) + Count_Nodes(root.right));  //recursive call
}

If you have any doubt please ask in comment.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a Java method to print the maximum and minimum value of an array containing elements...
Write a Java method to print the maximum and minimum value of an array containing elements [7,20,29,0,4,30,24,100]
1.        A. Write a recursive brute force algorithm that calculates an. B. Write the java code...
1.        A. Write a recursive brute force algorithm that calculates an. B. Write the java code that does the calculation. C. What is the recurrence relation for the number of multiplications? D. What is the efficiency class of the algorithm?
IN JAVA: Write recursive method to return true if a given array has element equal to...
IN JAVA: Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp,...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
Using Java write all 4 methods in one class 1) Write a value-returning method that returns...
Using Java write all 4 methods in one class 1) Write a value-returning method that returns the number of elements in an integer array. 2) Write a void method that multiples by 2 all the elements in an array of float. 3) Write a value- returning method that returns the product of all elements in an integer array. 4) Write a method that returns the total # of elements greater or equal to 90 in an array of integers.
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program. The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . .
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT