“First Problem”
Magrathea has just woken up after the recent mishap with the hyper-intelligent pan-dimensional beings whose protrusion into our dimension is not entirely unlike mice. They have decided to restart their highly profitable specialist industry of manufacturing custom-made luxury planets.
After reasoning with the finance department of the organization, the board of directors have decided to increase the burden on the economy by creating theme parks at each of the planets and charging an exorbitant entrance fee to the customers. They think the sooner the economy collapses the faster they can go back to sleep. After all there is no reason to keep the company running when they own all of the money in the galaxy.
The only problem is the people in charge of the planet database are highly adamant about the storage of planet names. They would like to keep track of the planets made in a lexicographic order so that they are easily traceable in case of tax audits by the broke intergalactic government (they just need a reason to waste everyones time). They have hired you to build the database of the planets for fast query.
There will be two types of queries to the database.
• insert planets with a name and entrance fee into the database
• return the entrance fee for the planet name from the database
Input Format
The first line has a number .
The next lines are queries which can be of the following 2 types.
• , the insert query
• , the entrance fee query
Output Format
For each query of type print the entrance fee of the planet in a new line.
If there is no associated planet name in the database print "-1", without the quotes.

“Second Problem”
While Arthur is spending time trying to get back to Earth in different dimensions, Ford has been thinking of crashing the different planets made by the Magrathean staff. Ford is too much of a frood to not have fun visiting any of them in the name of research for the guide.
He found you sitting in a bar drinking a pan galactic gargle blaster, blown out of your mind and talked to you about the planet database of the Magratheans. He would like to know the entrance fees of all the planets between some planet names. Help him get the information when the hangover passes.
There will be a single type of query to the database
• given two planet names return the list of planet names and entrance fees of all planets between the two queried names, in lexicographic order
Input Format
The first line contains a number , which is the size of the database.
The next lines are of the form
• the planet name, - the entrance fee of the planet, separated by a single space.
The next line contains a number the number of queries to the database.
The next lines are of the form
• planet names.
Output Format
For each query write down the names of the planets and the entrance fees, separated by a single space, in between (inclusive) , in separate lines.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.*;

public class Problem1 {

public static void main(String[] args) throws IOException {
    TwoThreeTree tr = new TwoThreeTree();
    BufferedReader s = new BufferedReader(new InputStreamReader(;
    //Scanner s = new Scanner(;
    int n = Integer.parseInt(s.readLine());
    for (int i = 0; i < n; i++) {
      String input = s.readLine();
      String[] params = input.split("\\s+");
      if (params[0].equals("1") && params.length == 3) {
       String key = params[1];
       int value = Integer.parseInt(params[2]);
       insert(key, value, tr);
      } else if (params[0].equals("2") && params.length == 2) {
       String key = params[1];
       search(key, tr);

static void search(String key, TwoThreeTree tr) {
    boolean result = doSearch(key, tr.root);
    if (result == false) {

static boolean doSearch(String key, Node n) {
    boolean result = false;
    if (n instanceof InternalNode) {
      InternalNode q = (InternalNode) n;
      if (q.child0 != null && key.compareTo( <= 0) {
       if (doSearch(key, q.child0)) {
          result = true;
      if (q.child1 != null && key.compareTo( <= 0) {
       if (doSearch(key, q.child1)) {
          result = true;
      if (q.child2 != null && key.compareTo( <= 0) {
       if (doSearch(key, q.child2)) {
          result = true;
    } else if (n instanceof LeafNode) {
      LeafNode q = (LeafNode) n;
      if ( {
       result = true;

By purchasing this solution you'll be able to access the following files: and

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats