JAVA Write a java program named BSTree.java that will: 1. Generate 100 random in
ID: 3571237 • Letter: J
Question
JAVA Write a java program named BSTree.java that will:
1. Generate 100 random integer numbers ranging from 1 – 99.
2. Build a Binary Search Tree using this set of numbers.
3. After building the tree, display the data into three formats: prefix order, infix order and postfix order.
"Note: (1) For all assignments, always use comments to explain your program.
Documentation:
20% Explain the purpose of the program as detail as possible 3%.
Develop a solution for the problem and mention algorithm used -10%
List data structures to be used in the solution. - 2%.
Give a description of how to use the program and expected input/output - 3%
Explain the purpose of each class you develop in the program - 2%.
Programming:
80% For each method, give the pre and post conditions and invariants, if any - 5%
Program execution according to the requirements 50%
Naming of program as required 5%
Useful comments and readability: 20%"
Explanation / Answer
Answer:
/*
JAVA Write a java program named BSTree.java that will:
1. Generate 100 random integer numbers ranging from 1 – 99.
2. Build a Binary Search Tree using this set of numbers.
3. After building the tree, display the data into three formats: prefix order, infix order and postfix order.*/
import java.util.*;
public class BSTree {
static LinkedList<Integer> treeList = new LinkedList<Integer>();
private int data;
private int root;
private BSTree left;
private BSTree right;
static int c = 0;
// This is the actual binary tree
static BSTree tree = new BSTree(100);
public BSTree(int data){
this.data = data;
}
public void setData(int data){
this.data = data;
}
public int getData(){
return data;
}
public void setRoot(int root){
this.root = root;
}
public int getRoot(){
return root;
}
public void setLeft(BSTree left){
this.left = left;
}
public BSTree getLeft(){
return left;
}
public void setRight(BSTree next){
this.right = next;
}
public BSTree getRight(){
return right;
}
public void setLeft(int data){
BSTree temp = new BSTree(data);
temp.left = null;
temp.right = null;
temp.data = data;
this.left = temp;
}
public void setRight(int data){
BSTree temp = new BSTree(data);
temp.left = null;
temp.right = null;
temp.data = data;
this.right = temp;
}
//-----------------------------------------------------------------------------
// MAIN CLASS
// 1) Generate 100 random integer numbers ranging from 1 - 99 [CHECK]
//Precondition: No inputs.
//Postcondition: Prints out the layout of the output
// as well as the 100 random integers, console log, and sorted BSTree
public static void main(String args[]){
int count = 0;
System.out.println();
System.out.println(" [BINARY SEARCH TREE] ");
System.out.println("[---- 100 Random Unsorted Integers (Between 0 - 99) ----] ");
System.out.println(" Random Number Count ");
for(int i = 0; i <= 100; i ++){
int random = (int)(Math.random() * 100);
treeList.add(random);
System.out.println(" " + treeList.get(i) + " " + count);
count++;
}
System.out.println("-----------------------------------------------------");
System.out.println("[Console]:");
// Initialized the root as the very first value in the Linked List
BSTree myBinarySearchTree = new BSTree(treeList.get(0));
myBinarySearchTree.setRoot(treeList.get(0));
// Adds sequentially the values in the treeList (linked list)
// into the BSTree tree that was initialized to hold the values
// as a binary search tree would
for(int i = 0; i < treeList.size(); i++){
//System.out.println(treeList.get(i));
myBinarySearchTree.add(treeList.get(i));
}
System.out.println("-----------------------------------------------------");
System.out.println(" [Infix Recursive Method print out]"
+ " (Duplicates are not included): ");
BSTree.printInfix(myBinarySearchTree);
System.out.println("-----------------------------------------------------");
}
//Precondition: Takes in BSTree root to use root value
//Postcondition: No return. Prints the root's left and right values or return nothing
private static void printInfix(BSTree root){
if(root == null){
//System.out.println(" if");
return;
} else{
c++;
printInfix(root.getLeft());
System.out.println(" " + root.getData());
printInfix(root.getRight());
}
}
//----------------------------------------------------------------------------
//Precondition: Takes in an integer
//Postcondition: returns false if data == data; otherwise returns true plus how the node is inserted
public boolean add(int data) {
if (data == this.data){
System.out.println("Inserting this.data " + data);
return false;
}
else if (data < this.data) {
if (left == null) {
System.out.println("Inserting new node " + data + " to left");
left = new BSTree(data);
return true;
} else{
return left.add(data);
}
} else if (data > this.data) {
if (right == null) {
System.out.println("Inserting new node " + data + " to right");
right = new BSTree(data);
return true;
} else
System.out.println("Inserting " + data + " to right");
return right.add(data);
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.