Question

In: Computer Science

BSTree.java:99: error: reached end of file while parsing } I get this error when i run...



BSTree.java:99: error: reached end of file while parsing }

I get this error when i run this code. can someone help me out?

I can't figure out how to make this work.

public class BSTree<T extends Comparable<T>> {

private BSTreeNode<T> root = null;

// TODO: Write an addElement method that inserts generic Nodes into
// the generic tree. You will need to use a Comparable Object
public boolean isEmpty(){
return root == null;
}

public int size(){
return node;}

public void addElement(T data){
if(isEmpty()){
root = new BSTreeNode(data);
}
else{
BSTreeNode pos = root;
while(pos != null){
if(value < pos.getElement()){
if(pos.getLeft() == null){
pos.setLeft(new BSTreeNode(data));
pos = null;
}
else{
pos = pos.getLeft();
}
}
else{
if(pos.getRight() == null){
pos.setRight(new BSTreeNode(data));
pos = null;
}
else{
pos = pos.getRight();
}
}
}
}
}

// TODO: write the method printElements
// It should check that the tree isn't empty
// and prints "The Tree is empty" if it is
// otherwise prints "The Elements Inorder: "
// and calls the inorder method
public void printElements(){
if(root == null) {
System.out.println("Tree is empty");
}
else
inorder(root);
}


// TODO: write the inorder method that traverses
// the generic tree and prints the data inorder

public void inorder(BSTreeNode element){
if(element != null){
inorder(element.getLeft());
System.out.print(element.getElemnt()+ " - ");
inorder(element.getRight());
}
}


// TODO: write the findMin method that returns the
// minimum value element in the tree

public BSTreeNode findMin(){
BSTreeNode node = root;
while(node.getLeft != null){
node = node.getLeft;
}
return node;
}

// TODO: write the findMin method that returns the
// maximum value element in the tree
public BSTreeNode findMax(){
BSTreeNode node = root;
while(node.getRight != null){
node=node.getRight;}

return node;

}



public class BSTreeNode<T> {

private T element;
private BSTreeNode<T> left, right;

public BSTreeNode(T element) {
this.element = element;
this.left = null;
this.right = null;
}

public T getElement() {
return element;
}

public void setElement(T element) {
this.element = element;
}

public BSTreeNode<T> getLeft() {
return left;
}

public void setLeft(BSTreeNode<T> left) {
this.left = left;
}

public BSTreeNode<T> getRight() {
return right;
}

public void setRight(BSTreeNode<T> right) {
this.right = right;
}

}import java.util.Scanner;






public class Main {

public static void main(String[] args) {

BSTree<String> tree = new BSTree<String>();

Scanner scrn = new Scanner(System.in);
System.out.println("Enter the words on separate lines to insert into the tree, enter -1 to stop");

String word = scrn.nextLine();
while(!word.equals("-1")) {
tree.addElement(word);
word = scrn.nextLine();
}
System.out.println();

tree.printElements();

System.out.println("\nThe minimum element in the tree is " + tree.findMin());
System.out.println("\nThe maximum element in the tree is " + tree.findMax());

}

}

Solutions

Expert Solution

//Resolved all errors. Code is working. Attaching the output screenshot.

//BSTree.java
public class BSTree<T extends Comparable<T>> {

private BSTreeNode<T> root = null;

// TODO: Write an addElement method that inserts generic Nodes into
// the generic tree. You will need to use a Comparable Object
public boolean isEmpty(){
return root == null;
}

public int size(){
return size(root);
}
private int size(BSTreeNode node) { 
          if (node == null) return(0); 
          else { 
            return(size(node.getLeft()) + 1 + size(node.getRight())); 
          } 
        } 
public void addElement(T data){
    Comparable<T> c = (Comparable<T>)data;

if(isEmpty()){
root = new BSTreeNode(data);
}
else{
BSTreeNode pos = root;
while(pos != null){
if(c.compareTo( (T) pos.getElement())<1)
{
if(pos.getLeft() == null){
pos.setLeft(new BSTreeNode(data));
pos = null;
}
else{
pos = pos.getLeft();
}
}
else{
if(pos.getRight() == null){
pos.setRight(new BSTreeNode(data));
pos = null;
}
else{
pos = pos.getRight();
}
}
}
}
}


// TODO: write the method printElements
// It should check that the tree isn't empty
// and prints "The Tree is empty" if it is
// otherwise prints "The Elements Inorder: "
// and calls the inorder method
public void printElements(){
if(root == null) {
System.out.println("Tree is empty");
}
else
inorder(root);
}



// TODO: write the inorder method that traverses
// the generic tree and prints the data inorder

public void inorder(BSTreeNode element){
if(element != null){
inorder(element.getLeft());
System.out.print(element.getElement()+ " - ");
inorder(element.getRight());
}
}



// TODO: write the findMin method that returns the
// minimum value element in the tree

public BSTreeNode findMin(){
BSTreeNode node = root;
while(node.getLeft() != null){
node = node.getLeft();
}
return node;
}


// TODO: write the findMin method that returns the
// maximum value element in the tree
public BSTreeNode findMax(){
BSTreeNode node = root;
while(node.getRight() != null){
node=node.getRight();
}

return node;

}
}

//BSTreeNode.java
public class BSTreeNode<T> {

private T element;
private BSTreeNode<T> left, right;

public BSTreeNode(T element) {
this.element = element;
this.left = null;
this.right = null;
}

public T getElement() {
return element;
}

public void setElement(T element) {
this.element = element;
}

public BSTreeNode<T> getLeft() {
return left;
}

public void setLeft(BSTreeNode<T> left) {
this.left = left;
}

public BSTreeNode<T> getRight() {
return right;
}

public void setRight(BSTreeNode<T> right) {
this.right = right;
}
}
//Main.java
import java.util.Scanner;


public class Main {

public static void main(String[] args) {

BSTree<String> tree = new BSTree<String>();

Scanner scrn = new Scanner(System.in);
System.out.println("Enter the words on separate lines to insert into the tree, enter -1 to stop");

String word = scrn.nextLine();
while(!word.equals("-1")) {
tree.addElement(word);
word = scrn.nextLine();
}
System.out.println();

tree.printElements();

System.out.println("\nThe minimum element in the tree is " + tree.findMin().getElement());
System.out.println("\nThe maximum element in the tree is " + tree.findMax().getElement());

}

}

Related Solutions

I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
1. a. Why is it necessary to get rid of Left-Recursive when using the Top-Down Parsing...
1. a. Why is it necessary to get rid of Left-Recursive when using the Top-Down Parsing method? Explain your answer completely b. Why is there no left recursion and left factoring in top down parsing? Explain your answer completely NOTE : Explain the answers of number 1 and number 2 with CASE EXAMPLES and their solutions! Course : Compilation Technique
This Compilation Errors Detected File: ALIEN_23547/source/domain/World.java Line: 153 while expected File: ALIEN_23547/source/domain/World.java Line: 158 reached end...
This Compilation Errors Detected File: ALIEN_23547/source/domain/World.java Line: 153 while expected File: ALIEN_23547/source/domain/World.java Line: 158 reached end of file while parsing WHERE IS THE ERROR? HOW TO CORRECT IT? PLEASE PINPOINT THE ERROR import java.util.Scanner; public class World { public static void main (String [] args){ Scanner scan = new Scanner (System.in); double masse = 80.0; do{ System.out.print("masse du parachutiste(>=40)?"); masse =scan.nextDouble(); }while (masse<40.0); double h0= 39000.0; do{ System.out.print("hauter de depart du parachutiste(>=250)?"); h0 = scan.nextDouble(); }while(h0<250.0); final double g =9.81;...
Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
What is the margin of error for a 99% CI for a mean, when the sample...
What is the margin of error for a 99% CI for a mean, when the sample size is 8, the sample average is 152, the sample std dev is 30, and the sample was drawn from a Normal population? (Enter your answer to two decimals: ex: 12.34). A 90% confidence interval for the population mean is 30 < mu < 50. What is the sample mean? A sample of 8 female college students consumes an average of 10 liters of...
when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
I keep on get a redefinition error and an undefined error. Customer List (1) CustomerList() and...
I keep on get a redefinition error and an undefined error. Customer List (1) CustomerList() and ~CustomerList() - default constructor and destructor. (2) bool addStore(Store*s) - Add an instance of store to the linked list. Return true if successful. (3) Store *removeStore(int ID) - Locate a Store in the list if it exists, remove and return it. (4) Store *getStore(int ID) - Locate a Store in the list and return a pointer to it. (5) bool updateStore(int ID, char *name,...
Determine the margin of error for a 99​% confidence interval to estimate the population mean when...
Determine the margin of error for a 99​% confidence interval to estimate the population mean when s​ = 43 for the sample sizes below. ​a) n=12 ​b) n=25 ​c) n=46 ​a) The margin of error for a 99​% confidence interval when n=12 is _.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT