Question

1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example:

if inpu File has numbers like 4787 79434 4326576 65997 4354

the output file must have result of 4788 79435 4326577 65998 4355

Note: there is no commas in between the numbers.

dont give images or theory please.

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

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

class BinarySearchTree{
  
public static Node root;
public BinarySearchTree(){
this.root = null;
}
  
public void insert(int id){
Node newNode = new Node(id);
if(root==null){
root = newNode;
return;
}
Node current = root;
Node parent = null;
while(true){
parent = current;
if(id<current.data){
current = current.left;
if(current==null){
parent.left = newNode;
return;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
return;
}
}
}
}
  
public void write(Node root){
try{
File file = new File("numbers_output.txt");
FileOutputStream fos = new FileOutputStream(file,true);
String tempNumbers = "";
if(root!=null){
write(root.right);
System.out.print(" " + root.data);
tempNumbers = (Integer.toString(root.data+1))+" ";
write(root.left);
}
fos.write(tempNumbers.getBytes());
fos.close();
}
catch(IOException ioe){
System.out.println("ioe");
}
}
  
public static void main(String[] args){
BinarySearchTree b = new BinarySearchTree();
FileInputStream in = null;
BufferedReader br = null;
  
try{
in = new FileInputStream("numbers.txt");
br = new BufferedReader(new InputStreamReader(in));
String line;
while((line = br.readLine()) != null){
           String[] splitted = line.split("\\s+");
           for(String i : splitted){
               b.insert(Integer.valueOf(i.trim()));
           }
//inserting into the binary search tree

}
//creating output file and erasing it's contents
File file = new File("numbers_output.txt");
file.createNewFile();
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
//writing the result into the output file
b.write(b.root);
  
}catch(IOException ioe){}
  
}
  
}

class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
left = null;
right = null;
}
}

Add a comment
Know the answer?
Add Answer to:
1. write a java program using trees(binary search treee) to read integers from input file( .txt)...
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
  • Write a java program to read from a file “input4_02.txt” an integer N (0? N ?...

    Write a java program to read from a file “input4_02.txt” an integer N (0? N ? 100) and then read N double numbers from this file to an array. Write to file “output4_02.txt” this array but in the reverse order and also the sum of all elements in the array. For example File: “input4_02.txt” 5 1.30 2.22 4.00 17.60 3.14 File “output4_02.txt” 3.14 17.60 4.00 2.22 1.30 Sum = 28.26

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • Binary trees - C programming; Write a program in C, you must have as input a...

    Binary trees - C programming; Write a program in C, you must have as input a .txt file (start.txt) which represents a binary tree, Edges as pairs (x, y), x and y refer to the names of the nodes , and have as output answering questions about the tree.EX Input (start.txt): (1,2) (1,3) (2,4) (2,5) (3,6) (3,7) (4,8) Output(output.txt): Is the tree complete? YES What is the weight of the tree? 4 Is it balanced? YES

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • Binary trees - C programming; Write a program in C, you must have as input a...

    Binary trees - C programming; Write a program in C, you must have as input a .txt file (start.txt), and have as output answering questions about the tree. Edges as pairs (x, y), x and y refer to the names of the nodes Input (start.txt): (1,2) (1,3) (2,4) (2,5) (3,6) (3,7) (4,8) Output(output.txt): Is the tree complete? NO What is the weight of the tree? 4 Is it balanced? YES

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • Java Create an object that will read from a file: The file name is: yourNameln.txt Example:...

    Java Create an object that will read from a file: The file name is: yourNameln.txt Example: JeanIn.txt Your input file contains integers. 3/3 You will read in the numbers until end of input file; and calculate the average of all the numbers. 10/10

  • Using Java how would I write a program that reads and writes from binary or text...

    Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this? Example Output: --------------------Configuration: <Default>-------------------- Enter the file name: kenb Choose binary or text file(b/t): b Choose read or write(r/w): w Enter a line of information to write to the file: lasdklj Would you like to enter another line? Y/N only n Continue? (y/n)y Enter the file name: kenb Choose binary or text file(b/t): b Choose...

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