Question

Please help!!!!!!

you work with a partner, only submit one lab project with both of your names in the source code file to Blackboard (BB) for grading; you will each earn the same number of points. What to hand in, and by when, is discussed below Assignment Objectives After completing this assignment the student should be able to . Implement a simple binary search tree class in Java according to Interface specifications given in UML. . Implement an in-order traversal algorithm for a binary tree Implement a generic type in Java Assignment Requirements For this assignment you are given the following Java source code files: CSE205_Assignment04.java Tree.java BSTree.java (This file is complete-you will make no changes to this file) (This file is complete-you will make no changes to this file) (you must complete this file) The specifications for the files are given below (including the UML diagram on the following page) Special requirements BSTree Class 1. insert method a. b. Inserts a new item into the binary search tree in the correct location. There should be no duplicate items in the tree. If an item is inserted and that item is already in the tree then this method should simply return without changing the state of the tree 2. contains method 3. printInOrder method 4. toString method Returns true if the tree contains the specified item; otherwise returns false Prints the items in the tree in a space separated list in ascending order Returns a string containing the items in the tree in ascending order and separated by spaces a. a. a. Node Class 1. Your BSTree class must contain a nested inner class called Node 2. This class must have the following attributes a. left Node- representing the left sub-node to this Node object b. right: Node- representing the right sub-node to this Node object c. data: T- holding the data for this Node object 3. This class must have the following methods a. nsert(item T) -this is a recursive method that finds the insertion point and inserts a Node for the new item in the correct position in the sub-tree for which this Node is the root

import java.util.Random;

public class CSE205_Assignment04 {
        private static Random rnd = new Random();
        
        public static void main(String[] args) {
                Tree_ctor_test();
                Tree_insert1_test();
                Tree_insert3_test();
                Tree_insertMany1_test();
                Tree_insertMany2_test();
                Tree_insertMany3_test();
                Tree_insertMany4_test();
        }
        
        public static void Tree_ctor_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                
                assertEqual("", t.toString(), "Tree_ctor_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_ctor_test: contains", false);
        }
        
        public static void Tree_insert1_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                t.insert(1);
                
                assertEqual("1 ", t.toString(), "Tree_insert1_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_insert1_test: contains", false);
                assertEqual(true, t.contains(1), "Tree_insert1_test: contains", false);
        }
        
        public static void Tree_insert3_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                t.insert(2);t.insert(1);t.insert(3);
                
                assertEqual("1 2 3 ", t.toString(), "Tree_insert3_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_insert3_test: contains", false);
                assertEqual(true, t.contains(1), "Tree_insert3_test: contains", false);
                assertEqual(true, t.contains(2), "Tree_insert3_test: contains", false);
                assertEqual(true, t.contains(3), "Tree_insert3_test: contains", false);
        }
        
        public static void Tree_insertMany1_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                int[] arr = {1,2,3,4,5,6,7,8,9,10};
                for (int n : arr)
                        t.insert(n);
                
                assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany1_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_insertMany1_test: contains", false);
                assertEqual(false, t.contains(11), "Tree_insertMany1_test: contains", false);
                assertEqual(true, t.contains(1), "Tree_insertMany1_test: contains", false);
                assertEqual(true, t.contains(10), "Tree_insertMany1_test: contains", false);
        }
        
        public static void Tree_insertMany2_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                int[] arr = {10,9,8,7,6,5,4,3,2,1};
                for (int n : arr)
                        t.insert(n);
                
                assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany2_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_insertMany2_test: contains", false);
                assertEqual(false, t.contains(11), "Tree_insertMany2_test: contains", false);
                assertEqual(true, t.contains(1), "Tree_insertMany2_test: contains", false);
                assertEqual(true, t.contains(10), "Tree_insertMany2_test: contains", false);
        }
        
        public static void Tree_insertMany3_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                int[] arr = {5,3,7,2,9,1,6,10,4,8};
                for (int n : arr)
                        t.insert(n);
                
                assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany3_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_insertMany3_test: contains", false);
                assertEqual(false, t.contains(11), "Tree_insertMany3_test: contains", false);
                assertEqual(true, t.contains(1), "Tree_insertMany3_test: contains", false);
                assertEqual(true, t.contains(10), "Tree_insertMany3_test: contains", false);
        }
        
        public static void Tree_insertMany4_test()
        {
                Tree<Integer> t = new BSTree<Integer>();
                int[] arr = {5,3,7,2,9,1,6,10,4,8,1,2,3,4,5,6,7,8,9,10};
                for (int n : arr)
                        t.insert(n);
                
                assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany4_test: toString", false);
                assertEqual(false, t.contains(0), "Tree_insertMany4_test: contains", false);
                assertEqual(false, t.contains(11), "Tree_insertMany4_test: contains", false);
                assertEqual(true, t.contains(1), "Tree_insertMany4_test: contains", false);
                assertEqual(true, t.contains(10), "Tree_insertMany4_test: contains", false);
        }
        
        public static void assertEqual(int expected, int actual, String message, boolean silentPass)
        {
                if (expected == actual && ! silentPass)
                        System.out.printf("--- PASSED: %s\n", message);
                if (expected != actual)
                        System.out.printf("*** FAILED: (expected: %d, actual: %d) %s\n", expected, actual, message);

        }
        
        public static void assertEqual(boolean expected, boolean actual, String message, boolean silentPass)
        {
                if (expected == actual && ! silentPass)
                        System.out.printf("--- PASSED: %s\n", message);
                if (expected != actual)
                        System.out.printf("*** FAILED: (expected: %s, actual: %s) %s\n", expected, actual, message);

        }
        
        public static void assertEqual(String expected, String actual, String message, boolean silentPass)
        {
                if (expected.equals(actual) && ! silentPass)
                        System.out.printf("--- PASSED: %s\n", message);
                if (! expected.equals(actual))
                        System.out.printf("*** FAILED: (expected: %s, actual: %s) %s\n", expected, actual, message);

        }
}
public interface Tree <T extends Comparable <T> >{
        public void insert(T item);
        public boolean contains(T item);
        public void printInOrder();
        public String toString();
}
public class BSTree<T extends Comparable <T>> implements Tree<T>{
        
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Collection;

import java.util.Comparator;

import java.util.Random;

public class CSE205_Assignment04 {

private static Random rnd = new Random();

  

public static void main(String[] args) {

Tree_ctor_test();

Tree_insert1_test();

Tree_insert3_test();

Tree_insertMany1_test();

Tree_insertMany2_test();

Tree_insertMany3_test();

Tree_insertMany4_test();

}

  

public static void Tree_ctor_test()

{

Tree<Integer> t = new BSTree<Integer>();

  

assertEqual("", t.toString(), "Tree_ctor_test: toString", false);

assertEqual(false, t.contains(0), "Tree_ctor_test: contains", false);

}

  

public static void Tree_insert1_test()

{

Tree<Integer> t = new BSTree<Integer>();

t.insert(1);

  

assertEqual("1 ", t.toString(), "Tree_insert1_test: toString", false);

assertEqual(false, t.contains(0), "Tree_insert1_test: contains", false);

assertEqual(true, t.contains(1), "Tree_insert1_test: contains", false);

}

  

public static void Tree_insert3_test()

{

Tree<Integer> t = new BSTree<Integer>();

t.insert(2);t.insert(1);t.insert(3);

  

assertEqual("1 2 3 ", t.toString(), "Tree_insert3_test: toString", false);

assertEqual(false, t.contains(0), "Tree_insert3_test: contains", false);

assertEqual(true, t.contains(1), "Tree_insert3_test: contains", false);

assertEqual(true, t.contains(2), "Tree_insert3_test: contains", false);

assertEqual(true, t.contains(3), "Tree_insert3_test: contains", false);

}

  

public static void Tree_insertMany1_test()

{

Tree<Integer> t = new BSTree<Integer>();

int[] arr = {1,2,3,4,5,6,7,8,9,10};

for (int n : arr)

t.insert(n);

  

assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany1_test: toString", false);

assertEqual(false, t.contains(0), "Tree_insertMany1_test: contains", false);

assertEqual(false, t.contains(11), "Tree_insertMany1_test: contains", false);

assertEqual(true, t.contains(1), "Tree_insertMany1_test: contains", false);

assertEqual(true, t.contains(10), "Tree_insertMany1_test: contains", false);

}

  

public static void Tree_insertMany2_test()

{

Tree<Integer> t = new BSTree<Integer>();

int[] arr = {10,9,8,7,6,5,4,3,2,1};

for (int n : arr)

t.insert(n);

  

assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany2_test: toString", false);

assertEqual(false, t.contains(0), "Tree_insertMany2_test: contains", false);

assertEqual(false, t.contains(11), "Tree_insertMany2_test: contains", false);

assertEqual(true, t.contains(1), "Tree_insertMany2_test: contains", false);

assertEqual(true, t.contains(10), "Tree_insertMany2_test: contains", false);

}

  

public static void Tree_insertMany3_test()

{

Tree<Integer> t = new BSTree<Integer>();

int[] arr = {5,3,7,2,9,1,6,10,4,8};

for (int n : arr)

t.insert(n);

  

assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany3_test: toString", false);

assertEqual(false, t.contains(0), "Tree_insertMany3_test: contains", false);

assertEqual(false, t.contains(11), "Tree_insertMany3_test: contains", false);

assertEqual(true, t.contains(1), "Tree_insertMany3_test: contains", false);

assertEqual(true, t.contains(10), "Tree_insertMany3_test: contains", false);

}

  

public static void Tree_insertMany4_test()

{

Tree<Integer> t = new BSTree<Integer>();

int[] arr = {5,3,7,2,9,1,6,10,4,8,1,2,3,4,5,6,7,8,9,10};

for (int n : arr)

t.insert(n);

  

assertEqual("1 2 3 4 5 6 7 8 9 10 ", t.toString(), "Tree_insertMany4_test: toString", false);

assertEqual(false, t.contains(0), "Tree_insertMany4_test: contains", false);

assertEqual(false, t.contains(11), "Tree_insertMany4_test: contains", false);

assertEqual(true, t.contains(1), "Tree_insertMany4_test: contains", false);

assertEqual(true, t.contains(10), "Tree_insertMany4_test: contains", false);

}

  

public static void assertEqual(int expected, int actual, String message, boolean silentPass)

{

if (expected == actual && ! silentPass)

System.out.printf("--- PASSED: %s\n", message);

if (expected != actual)

System.out.printf("*** FAILED: (expected: %d, actual: %d) %s\n", expected, actual, message);

}

  

public static void assertEqual(boolean expected, boolean actual, String message, boolean silentPass)

{

if (expected == actual && ! silentPass)

System.out.printf("--- PASSED: %s\n", message);

if (expected != actual)

System.out.printf("*** FAILED: (expected: %s, actual: %s) %s\n", expected, actual, message);

}

  

public static void assertEqual(String expected, String actual, String message, boolean silentPass)

{

if (expected.equals(actual) && ! silentPass)

System.out.printf("--- PASSED: %s\n", message);

if (! expected.equals(actual))

System.out.printf("*** FAILED: (expected: %s, actual: %s) %s\n", expected, actual, message);

}

}

//BSTree

class BSTree<T extends Comparable <T>> implements Tree<T>{

   private BinaryTreeNode<T> root;

   private Comparator<T> comparator;

   public BSTree() {

       root = null;

       comparator = null;

   }

   private int compare(T x, T y) {

       if (comparator == null)

           return x.compareTo(y);

       else

           return comparator.compare(x, y);

   }

   private BinaryTreeNode<T> insert(BinaryTreeNode<T> root, T toInsert) {

       if (root == null)

           return new BinaryTreeNode<T>(toInsert);

       if (compare(toInsert, root.getData()) == 0)

           return root;

       if (compare(toInsert, root.getData()) <= 0)

           root.left = insert(root.getLeft(), toInsert);

       else

           root.right = insert(root.right, toInsert);

       return root;

   }

   @Override

   public void insert(T item) {

       // TODO Auto-generated method stub

//       if(contains(item))

//           return;

       root = insert(root, item);

      

      

   }

   private boolean find(BinaryTreeNode<T> root, T key) {

       if (root == null)

           return false;

       else if (compare(key, root.data) == 0)

           return true;

       else if (compare(key, root.data) < 0)

           return find(root.left, key);

       else

           return find(root.right, key);

   }

   @Override

   public boolean contains(T item) {

       // TODO Auto-generated method stub

       return find(root, item);

   }

   private void inOrderHelper(BinaryTreeNode<T> root) {

       if (root != null) {

           inOrderHelper(root.getLeft());

           //System.out.print(root.getData() + " ");

           inOrderHelper(root.getRight());

       }

   }

   @Override

   public void printInOrder() {

       // TODO Auto-generated method stub

       inOrderHelper(root);

   }

  

   public static String ret = "";

  

   private void inOrder(BinaryTreeNode<T> root) {

       if (root != null) {

           inOrder(root.getLeft());

           ret = ret + root.getData() + " ";

           inOrder(root.getRight());

       }

   }

   @Override

   public String toString(){

       ret = "";

       inOrder(root);

       return ret;

   }

  

}

//package BinaryTree;

class BinaryTreeNode<T extends Comparable<T>> {

   public BinaryTreeNode<T> left; // the left child

   public BinaryTreeNode<T> right; // the right child

   public T data; // the data in this node

   public BinaryTreeNode() {

       this(null, null, null);

   }

   public BinaryTreeNode(T theData) {

       this(theData, null, null);

   }

   public BinaryTreeNode(T theData, BinaryTreeNode<T> leftChild, BinaryTreeNode<T> rightChild) {

       data = theData;

       left = leftChild;

       right = rightChild;

   }

   public T getData() {

       return data;

   }

   public BinaryTreeNode<T> getLeft() {

       return left;

   }

   public BinaryTreeNode<T> getRight() {

       return right;

   }

   public void setLeft(BinaryTreeNode<T> newLeft) {

       left = newLeft;

   }

   public void setRight(BinaryTreeNode<T> newRight) {

       right = newRight;

   }

   public void setData(T newData) {

       data = newData;

   }

   public void preOrder() {

       System.out.println(data);

       if (left != null) {

           left.preOrder();

       }

       if (right != null) {

           right.preOrder();

       }

   }

   public int height() {

       int leftHeight = 0; // Height of the left subtree

       int rightHeight = 0; // Height of the right subtree

       int height = 0; // The height of this subtree

       // If we have a left subtree, determine its height

       if (left != null) {

           leftHeight = left.height();

       }

       // If we have a right subtree, determine its height

       if (right != null) {

           rightHeight = right.height();

       }

       // The height of the tree rooted at this node is one more than the

       // height of the 'taller' of its children.

       if (leftHeight > rightHeight) {

           height = 1 + leftHeight;

       } else {

           height = 1 + rightHeight;

       }

       // Return the answer

       return height;

   }

   /**

   * @param pathString

   * @return the tree nodes in pre-order traversal

   */

   public String toStringPreOrder(String pathString) {

       String treeString = pathString + " : " + data + "\n";

       if (left != null) {

           treeString += left.toStringPreOrder(pathString + "L");

       }

       if (right != null) {

           treeString += right.toStringPreOrder(pathString + "R");

       }

       return treeString;

   }

}

// Tree

interface Tree <T extends Comparable <T> >{

public void insert(T item);

public boolean contains(T item);

public void printInOrder();

public String toString();

}

======================
Did Everything, All the test passed.
if (root != null) { 199 200 201 202 203 204 205 inOrderHelper(root.getLeftO); //System.out.print(root.getDataO ): inOrderHel


THANKS, please rate . Let me know if there is any doubts. PLEASE COMMENT

Add a comment
Know the answer?
Add Answer to:
Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the...

    Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...

  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...

    Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) ^ rowPuzzle.java:16: error: class, interface, or enum expected } //Java Program import java.util.*; // rowPuzzle helper function implementation // File: rowPuzzle.cpp // Header files section // function prototypes public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) { // base case // return true if the puzzle is solvable if (index == squares.size() - 1) {    return...

  • Complete LinkedListCollection.java with following methods: public LinkedListCollection(); public LinkedListCollection(int size); public boolean isEmpty(); public int size();...

    Complete LinkedListCollection.java with following methods: public LinkedListCollection(); public LinkedListCollection(int size); public boolean isEmpty(); public int size(); // Return number of elements in the Collection. public String toString(); public boolean add(T element); // Add an element into the Collection, return true if successful public boolean remove(T target); // Remove the target from Collection, return true if successful public boolean removeAll(T target); // Remove all occurrences of Target, return if successful public void removeDuplicate(); // Remove duplicated element(s) public boolean equals(LinkedListCollection that);...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT