Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

develop a java program using binary search tree to find the largest and the smal

ID: 3805953 • Letter: D

Question

develop a java program using binary search tree to find the largest and the smallest numbers from the input file.

Input data file is as follows:

29755 49953 10773 48214 10121 32881 4101 8807 25671 46882 39403 37011 36156 12194 45373 3868 12228 31972 25012 30794 45518 18249 14013 49786 37593 2529 117 42511 46920 20876 23626 19824 5480 47782 18773 26007 25685 26582 46698 38729 3835 6723 13427 36627 38543 49831 10420 21237 21250 7160 11020 9002 21239 775 3439 2085 29419 9599 39805 3787 43536 19324 31218 15538 22456 42105 20794 37604 8684 47949 20194 44940 16398 24756 312 22951 6911 48079 33451 25293 24712 44842 42581 7331 45023 27501 23085 44372 38126 6423 13784 13359 36673 8618 6954 394 49604 40395 1565 44111 375 29976 20032 36735 27785 23687 26394 30463 24823 35915 3030 36707 4713 18725 13758 9689 23603 21755 3509 23908 6167 6798 8607 37061 13853 14372 27157 26152 15415 1450 26019 44731 15947 30571 39440 23474 23721 40545 44510 24623 33771 18119 30319 13239 33415 18122 10951 24626 37667 46839 17223 37849 41849 29692 38526 9815 34612 29630 23978 33049 47061 17084 41683 2981 22693 15810 35889 17363 28467 11500 14516 31215 39473 11658 43668 5647 18401 35893 21536 17809 26283 45540 1395 42274 42821 40306 32258 21227 43047 7496 36231 11961 22005 6312 43570 35416 30685 5686 10046 48740 34558 19177 46310 28192 30671 44555 45685 26090 10541 18881 5013 6985 33819 42121 43053 23725 32060 3639 13685 42871 16260 22719 29062 32113 22044 1575 12848 45947 32364 47227 43858 45035 2468 5344 23 10431 17272 32825 29042 24352 30858 26643 19460 19371 6835 26793 33235 6360 556 6480 42952 34954 7024 38066 12978 10266 20270 362 24067 36127 10931 21356 15165 28189 28847 30642 4540 39941 26583 27466 4698 20855 27959 3594 12065 28686 32918 44982 32864 47313 6346 22649 1262 17376 43829 21048 4449 19470 41755 41762 2061 8674 22648 11421 14091 34787 29189 15322 2279 44812 1955 22489 34882 2943 46623 13582 3626 28584 26094 20462

Explanation / Answer

Java Program

package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

class Node {

int data;
Node left, right;

Node(int d) {
data = d;
left = right = null;
}
}

class BinaryTree {

static Node head;
Node insert(Node node, int data) {
if (node == null) {
return (new Node(data));
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
}
int minvalue(Node node) {
Node current = node;

while (current.left != null) {
current = current.left;
}
return (current.data);
}
int maxvalue(Node node) {
Node current = node;
while (current.right != null) {
current = current.right;
}
return (current.data);
}
  
public static void main(String[] args) throws IOException {
   InputStream is = new FileInputStream("src/input.txt");
   BufferedReader buf = new BufferedReader(new InputStreamReader(is));
   String line = buf.readLine();
   StringBuilder sb = new StringBuilder();
   while(line != null){
       sb.append(line).append(" ");
       line = buf.readLine();
       }
   String fileAsString = sb.toString().trim();
   String numArray[] = fileAsString.split(" ");
   BinaryTree tree = new BinaryTree();
Node root = null;
int num1=Integer.parseInt(numArray[0]);
root = tree.insert(root, num1);
int num2;
for(int i=1;i<numArray.length;i++){
   num2=Integer.parseInt(numArray[i]);
   tree.insert(root, num2);
}
System.out.println("The minimum value of BST is " + tree.minvalue(root));
System.out.println("The maximum value of BST is " + tree.maxvalue(root));
}
}

OUTPUT:-

The minimum value of BST is 23
The maximum value of BST is 49953

Note:-

In Statement InputStream is = new FileInputStream("src/input.txt");

mention path, where your data file is store.

And secondly, in statement  String numArray[] = fileAsString.split(" ");

i used split method to split String by double space character. you can change accordingly