In: Computer Science
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());
}
}
//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());
}
}