Question

In: Computer Science

Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...

Data Structures ( Recursion )

Assignment

Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list)

The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after the recursive call.


java language

Solutions

Expert Solution

import java.util.*;
public class RemoveMiddle{
    
public static void removeMidRecursive(ArrayList<Integer> Arrlist) {
    System.out.println("recursive: " + Arrlist);
    if (Arrlist.size() % 2 == 0) {
        System.out.println("list size is even, no middle element to remove");
    } else if (Arrlist.size() == 1) {
        int mid = Arrlist.remove(0);
        System.out.println("removed mid element: " + mid);
    } else {
        // find indexes of min and max elements
        int minId = 0;
        int maxId = 0;
        for (int i = 1; i < Arrlist.size(); i++) {
            int val = Arrlist.get(i);
            if (val < Arrlist.get(minId)) {
                minId = i;
            } else if (val >= Arrlist.get(maxId)) {
                maxId = i;
            }
        }

        // sort the indexes
        int tMin = Math.min(minId, maxId);
        int tMax = Math.max(minId, maxId);
        // start removing from bigger index and store the value
        // using `E remove(int index)`
        Integer atMax = Arrlist.remove(tMax);
         System.out.printf("removed Arrlist[%d]=%d%n", tMax, atMax);
        Integer atMin = Arrlist.remove(tMin);
         System.out.printf("removed Arrlist[%d]=%d%n", tMin, atMin);
        
        removeMidRecursive(Arrlist);

        // restore the values by insertig at appropriate index with a correction
        if (tMin > 0) Arrlist.add(tMin - 1, atMin); else Arrlist.add(0, atMin);
        if (tMax > 0) Arrlist.add(tMax - 1, atMax); else Arrlist.add(0, atMax);
        
        System.out.println("restored: " + Arrlist);
    }
    
}
public static void main(String[] args)
    {   
        ArrayList<Integer> li = new ArrayList<Integer>();
        li.add(1);
        li.add(2);
        li.add(3);
        li.add(4);
        li.add(5); 
            
        removeMidRecursive(li);
    }

}
output:
recursive: [1, 2, 3, 4, 5]
removed Arrlist[4]=5
removed Arrlist[0]=1
recursive: [2, 3, 4]
removed Arrlist[2]=4
removed Arrlist[0]=2
recursive: [3]
removed mid element: 3
restored: [2, 4]
restored: [1, 2, 4, 5]

Related Solutions

Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
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...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The method finds and returns an array which contains the positive numbers. For example, if the method receives an array with the following elements: 0 3 -1 2 5 1 -5 2 -2 0 the method should return an array with the following elements: 3 2 5 1 2 In the main method, randomly generate an array of 10 random integers between -5 and 5,...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int)...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int) of the longest String in a List of Strings. For each recursive call, remove the first String from the list and return the greater of the length of the String you removed or the result of calling the method on the remainder of the list. The base case should be that the size of the list is 0. Write a driver to verify that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT