You are to write a program name BSTree.java that will: Generate 100 random integ
ID: 3761574 • Letter: Y
Question
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.
What to turn in
Turn in BSTree.java, BStree.class OR BSTree.jar
Explanation / Answer
import java.util.*;
public class BSTree
{
public static void main(String[]args){
Tree bt = new Tree ();
Random number = new Random();
int i;
for(i = 1; i<100; i++){
int randNum = number.nextInt(101);
if(randNum == 0){
i--;
}
else{
bt.insert(randNum);
}
}
bt.calculateHeights();
}
}
Node makeTree(int head, int tail){
nodeMid = (head+tail)/2;
this = new Node();
if(head < nodeMid-1)
{
this.left = makeTree(head, nodeMid-1);
}
if(nodeMid < tail)
{
this.right = makeTree(nodeMid, tail);
}
this.setValue(list[nodeMid]);
return this;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.