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

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)
[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...
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Write recursive method to return true if a given array has element equal to employee emp,...
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, and returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT