Question

In: Computer Science

Write a Java program to read a set of integers from a file, dataX, and a...

Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file rangeX and query the binary search tree to report the sum.

For instance, if file dataX has the integers (3, −2, 2, 5, 2, −6, 1) and file rangeX has the ranges ([−3, 4], [0, 5]) then the output should be:

Range [-3,4]. Sum = 6.

Range [0,5]. Sum = 13.

Solutions

Expert Solution

Program:

import java.util.*;
import java.io.*;

//Node class
class Node
{
int key;
Node left, right;

public Node() {}

public Node(int num) {
key = num;
left = null;
right = null;
}
}

//BinarySearchTree class
class BinarySearchTree
{
   Node root;
   //constructor
   public BinarySearchTree()
   {
       root = null;
   }
   //Helper method: insert to the BinarySearchTree
   private Node insert(Node root, int data)
   {
       if(root==null){
           root = new Node(data);
           return root;
       }
       else if(root.key > data)
       {
           Node left = insert(root.left, data);
           root.left = left;
       }
       else
       {
           Node right = insert(root.right, data);
           root.right = right;
       }
  
       return root;
   }
   //insert to the BinarySearchTree
   public void insert(int data)
   {
       root = insert(root, data);
   }
   //Helper method: sum the numbers within the range [a, b]
   private int sumOfNumbers(Node root, int a, int b)
   {
       if(root==null)
           return 0;
       if(root.key>=a && root.key<=b)
           return root.key + sumOfNumbers(root.left, a, b) + sumOfNumbers(root.right, a, b);
       return sumOfNumbers(root.left, a, b) + sumOfNumbers(root.right, a, b);
   }
   //sum the numbers within the range [a, b]
   public int sumOfNumbers(int a, int b)
   {
       return sumOfNumbers(root, a, b);
   }

}


//RangeOfData class
class RangeOfData
{  
   //main method
   public static void main (String[] args) throws IOException
   {
       //open the file
       Scanner sc = new Scanner(new File("dataX.txt"));
      
       BinarySearchTree bst = new BinarySearchTree();
      
       //read data and insert to the BinarySearchTree
       while(sc.hasNextInt())
       {
           int data = sc.nextInt();
           bst.insert(data);
       }
      
       //open the file
       sc = new Scanner(new File("rangeX.txt"));
      
       //read the range and sum of the
       while(sc.hasNextInt())
       {
           int a = sc.nextInt();
           int b = sc.nextInt();
           //sum the numbers within the range [a, b]
           int sum = bst.sumOfNumbers(a, b);
           //display the sum
           System.out.println ("Range["+a +","+b+"]. Sum = " + sum);
       }
   }
}

dataX.txt

3 -2 2 5 2 -6 1

rangeX.txt

-3 4
0 5

Output:

Range[-3,4]. Sum = 6
Range[0,5]. Sum = 13

Note: Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.


Related Solutions

Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT