Question

Java Program: You will be inserting values into a generic tree, then printing the values inorder,...

Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete.

Ex: If the input is

like
ferment
bought
tasty
can
making
apples
super
improving
juice
wine
-1

the output should be:

Enter the words on separate lines to insert into the tree, enter -1 to stop

The Elements Inorder: apples - bought - can - ferment - improving - juice - like - making - super - tasty - wine - 

The minimum element in the tree is apples

The maximum element in the tree is wine

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());
       System.out.println("\nThe maximum element in the tree is " + tree.findMax());

   }

}

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;
   }
      
}

BSTree.java

public class BSTree<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

   // 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


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

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

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

}

Homework Answers

Answer #1
//BSTree.java


public class BSTree<T> {

private BSTreeNode<T> root = null;

@SuppressWarnings("unchecked")

//Defining addElement function

public void addElement(String word) {

if (root == null) { // if root is null
root = new BSTreeNode<T>((T)word);
return;

}

BSTreeNode<T> loc = root; // start searching downward at root

while (true) {

if (word.compareTo(loc.getElement().toString()) < 0) { // look towards left
if (loc.getLeft()!= null) loc = loc.getLeft();
else
{
   BSTreeNode<T> leftNode = new BSTreeNode<T>((T) word);
   loc.setLeft(leftNode);
   break;
}
}
else if (word.compareTo(loc.getElement().toString()) > 0) { // look towards right
if (loc.getRight() != null) loc = loc.getRight();
else
{
   BSTreeNode<T> rightNode = new BSTreeNode<T>((T) word);
   loc.setRight(rightNode);
   break;
}
}
else break; // found! Don't insert
}
   }

// 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("The Tree is empty");
   }
   else
   {
       System.out.println("The Elements Inorder: ");
       inorder();
   System.out.println();
   }
         
}
  
// TODO: write the inorder method that traverses
// the generic tree and prints the data inorder
public void inorder()
{
   inorderTraversal(root);
}

// inorderT: recursive function that does the work
private void inorderTraversal(BSTreeNode<T> t) {
if (t != null) {
   inorderTraversal(t.getLeft());
System.out.print(t.getElement() + " ");
inorderTraversal(t.getRight());
}
}

// TODO: write the findMin method that returns the
// minimum value element in the tree
public String findMin()
{
   BSTreeNode<T> currentNode = root;
   while(currentNode.getLeft()!=null)
   {
       currentNode = currentNode.getLeft();
   }
     
   return currentNode.getElement().toString();
}

// TODO: write the findMin method that returns the
// maximum value element in the tree
public String findMax()
{
   BSTreeNode<T> currentNode = root;
   while(currentNode.getRight()!=null)
   {
       currentNode = currentNode.getRight();
   }
     
   return currentNode.getElement().toString();
}

}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them. #include<iostream> using namespace std; /*Define the class for BST*/ class BST_TREE {    /*Structure of the tree*/    struct tree    {        int value;        tree* left;        tree* right;    };    tree* root;    /*Method to clear the tree*/    tree* Empty(tree* t)    {        if (t == NULL)       ...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
(Java) Write the missing equals method needed for the Point class shown on the screen to...
(Java) Write the missing equals method needed for the Point class shown on the screen to work with this program. The missing method would be added to the Point class. class PointTestP3 {   public static void main(String[] args) {    Point p1 = new Point(5, 6);     Point p2 = new Point(5, 6);        System.out.println(p1.equals(p2)); // prints "true"   } }
JAVA use the concepts in concurrent programming to write a basic multi-threaded program. You will be...
JAVA use the concepts in concurrent programming to write a basic multi-threaded program. You will be creating a program that simulates the sound of soldiers marching: “Left, Left, Left, Right, Left”. You will need one class to print “Left” and one to print “Right”, as well as a class to drive the program. LeftThread.java public class LeftThread extends Thread { public void run() { for(int i = 0; i < 10; i++) { //TODO: Print "Left" //TODO: Print "Left" //TODO:...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT