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
Without using extra data structures, write a recursive method recursiveProdcutQueue ( Queue <Integer> q) in Test...
Without using extra data structures, write a recursive method recursiveProdcutQueue ( Queue <Integer> q) in Test class (in stacks_queues package) that receives a queue of integers and return the product of the integers inside the queue. Then don’t forget to test the method in the main. Make sure not to change values in queue after calling the method use java eclipse please
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 sum the values in an array of integers. Create a file...
Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works. ////ArraySumDriver.java/////// public class ArraySumDriver { private final static int ARRAY_SIZE = 6; /** * @param args */ public static void main(String[] args) { int index = 0; Integer[] myArray = new Integer[ARRAY_SIZE]; ArraySum arraySum = new ArraySum(); myArray[index++] =...
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
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT