Question

programming language is JAVA: We wish to implement a method  String insert(char c, String s) that returns...

programming language is JAVA:

We wish to implement a method  String insert(char c, String s) that returns a string with c inserted in the correct position in already sorted String s. To do so we will implement insert as follows:

static  String insert(char c, String s) {

return insertHelper(c, "", s);

}

static  String insertHelper(char c, String left, String right) {} // strip leading characters from right, & append to left 'till insertion point found. Now return left + c + right.

For example, insert('e', "bcdfg") would call

insertHelper('e', "", "bcdfg") which would recursively call

insertHelper('e', "b" , "cdfg") followed by

insertHelper('e', "bc" , "dfg") followed by

insertHelper('e', "bcd", "fg).

This last call would then return "bcd" + 'e' + "fg" which is gives the final result "bcdefg".

IMPLEMENT insertHelper. (Don't implement insert() )

Your implementation of insertHelper must be tailRecursive.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class SortedInsertion{
   //main method
   public static void main(String args[]){
  
       System.out.println("insert(d,bdefg) = "+insert('d',"bdefg"));
       System.out.println("insert(a,bdefg) = "+insert('a',"bdefg"));
       System.out.println("insert(h,bdefg) = "+insert('h',"bdefg"));
       System.out.println("insert(c,bdefg) = "+insert('c',"bdefg"));

   }
   //insert function
   public static String insert(char c,String s){
       return insertHelper(c,"",s);
   }
   //insertHelper function
   public static String insertHelper(char c,String left,String right){
       //base conditions
       //if right string is null then we have to return left stirng
       if(right.equals("")){
           return left+c;
       }
       //if there is know left string
      
       else if(left.equals("")){
           //condtion to insert c at beggining of string
           if(c<=right.charAt(0)){
               return c+right;
           }
           else{
               left=left+right.charAt(0); //adding first charecter of right string to left string end
               right=right.substring(1,right.length()); //removing frist charecter of right string
               return insertHelper(c,left,right);//tail recursive calling
           }
       }
       //inserting c at porper position
       else if(left.charAt(left.length()-1)<=c && c<=right.charAt(0)){  
          
           return left+c+right;
       }
       else{
           left=left+right.charAt(0); //adding first charecter of right string to left string end
           right=right.substring(1,right.length());//removing frist charecter of right string
           return insertHelper(c,left,right);//tail recursive calling
       }
   }
}

C:\Windows system32\cmd.ex insert (d.bdefg bddefg insert(a,bdef g abdefg insert(h.bdef gbdefgh insert<c.bdefg> - bcdefg Press
Add a comment
Know the answer?
Add Answer to:
programming language is JAVA: We wish to implement a method  String insert(char c, String s) that returns...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node...

    C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...

  • C programming Let's try to use the template below to implement a Set with double hashing....

    C programming Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set. Let's reuse the exact hash functions we have seen in example in Part 7 of the lecture notes, page 3. As...

  • I need this in C not C++ just C SET-151 C Programming #1 Homework: 7 String...

    I need this in C not C++ just C SET-151 C Programming #1 Homework: 7 String Basics – Death by a thousand strings.                                                                                                                                                                         Reading(s): Chapter(s) in C Primer Plus 1-7, 9, 10 please use this format // ------------------------------------------------------------------------------------------ // Name: Your name here // Class: C Programming // Abstract: Homework 1. // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Includes // ------------------------------------------------------------------------------------------ #include<stdio.h> #include<stdlib.h> // ------------------------------------------------------------------------------------------ // Constants // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Prototypes // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Name: main //...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can...

    C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can be used to implement a Set ADT. Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set....

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT