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
[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...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
Question - Write a Client class with a main method that tests the data structures as...
Question - Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue, LinkedQueue, and ArrayList: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add and remove N Integers from the structure. N should vary from 10 to 1,000,000,000 increasing N by a factor of 10 for each test. Depending on your system you may run out...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT