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

Java Program : You are to write a program name BSTree.java that will: Generate 1

ID: 3858534 • Letter: J

Question

Java Program :

You are to write a program name BSTree.java that will:

Generate 100 random integer numbers ranging from 1 – 99.

Store these numbers in a data structure of your choice and display the data on the screen. DO NOT SORT THIS DATA STRUCTURE.

Now build a Binary Search Tree using this set of numbers. You MUST insert the values into the tree starting from the first element of your Data Structure and go sequentially.

After building the tree, use an infix recursive method to display the data on the screen.

To build the Binary Search Tree, you must create your own Linked List.

Can you please tell me correct my code to where it runs?

package bstree;

/**
*
* @author hgnel
*/
public class Node {
int data ;
Node left;
Node right;
  
  
public Node (int input)
{
this.data = input;
  
  
  
  
}
  
public void setleft(Node input){
left = input;
  
  
}
public void setright(Node input){
  
right = input;
}
  
}

package bstree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
import static jdk.nashorn.tools.ShellFunctions.input;
public class BSTree {
  
  

  

public static void main(String[] args)
  
{
ArrayList<Integer> Array = new ArrayList<Integer>();
LinkedList<Integer> List= new LinkedList<Integer>();
Random list = new Random();
  
System.out.println("random numbers generated");
System.out.println("");
// for loop to generate random numbers   
for (int i = 0; i < 100; i++ )
{
  
Array.add(list.nextInt(99));
System.out.println(Array.get(i));

}
Node tree = new Node();
{
  
}
// this is the for loop that adds to the link list
//] for (int value : Array) {

  

}
  
  
  
public Node leave (Node input, int value, null)
{
if ( input.data <= value )
{
input.data = value(input.left,);
}
if (input.data >= value )
{
  
}
  
}
if(input.data == null)
  

{
return null;   
}

  
  

  
  
  
  

Explanation / Answer



import java.util.*;

public class BST {

/* Nested Class containing left and right child of current node and key value*/
class Node {
int val;
Node left, right;

public Node(int val_) {
val = val_;
left = right = null;
}
}

// Root of BST
Node root;

// Construct values here
BST() {
root = null;
}

// This method mainly calls insertHelper()
void insert(int val) {
root = insertHelper(root, val);
}

/* A recursive function to insert a new key in BST */
Node insertHelper(Node root, int key) {

/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}

/* Otherwise, recur down the tree with left and right childs*/
if (key < root.val)
root.left = insertHelper(root.left, key);
else if (key > root.val)
root.right = insertHelper(root.right, key);

return root;
}

ArrayList<Integer> inorder() {// Will inorderHelper here
ArrayList<Integer> a = new ArrayList<Integer>();
inorderHelper(root, a);
return a;
}

// A function to do inorder traversal of BST
void inorderHelper(Node root, ArrayList<Integer> a) {
if (root != null) {
inorderHelper(root.left, a);// visit left node
System.out.print(root.val + " ");
a.add(root.val);// Add to list
inorderHelper(root.right, a);// visit right node
}
}
  
public static BST getBST(ArrayList<Integer> a)
{
BST bst = new BST();
for(int val: a)
{
bst.insert(val);
}
return bst;
}

public static void main(String[] args) {
// Now let's do some testing;
// make a arraylist of numbers
ArrayList<Integer> a = new ArrayList<Integer>();// Defina a list of int here
for(int i=0;i < 100;i++)
{
Random rand = new Random();
a.add(rand.nextInt(99));// Add 100 random numbers to list
}
BST tree = getBST(a);// get tree
tree.inorder();// Display data here
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote