Question

In: Computer Science

Submit:    SearchTree.java  (again) with the following method added: countSameData(SearchTree other); a public method that counts the...

Submit:    SearchTree.java  (again) with the following method added:

countSameData(SearchTree other); a public method that counts the number of tree nodes in "this" tree that are also contained in the "other" tree as determined by compareTo()==0. See example below.

You can either use your existing SearchTree from your homework exercises. Or you can start from scratch with this file that you should have started with days ago:  SearchTree.java

Further explanation:  I accomplished this by building upon many things we've done this quarter, recursion, tree traversals, and even java.util.* (yes import that if needed).

Since this BST requires Comparable, you must rely on .compareTo in case someone wants to use CalendarDate or String data for <E>

Order of data in the tree does not matter, just counting same data nodes, also knowing that trees can be empty, or compared to itself, as example below demonstrates:

SearchTree<Integer> zero = new SearchTree<Integer>();
SearchTree<Integer> one = new SearchTree<Integer>();
SearchTree<Integer> two = new SearchTree<Integer>();
one.add(9); one.add(8); one.add(7); // all left nodes
two.add(7); two.add(8); two.add(9); // all right nodes

System.out.println(one.equals(two)); // false
System.out.println(one.countSameData(two)); // 3
System.out.println(one.countSameData(one)); // 3
System.out.println(zero.countSameData(two)); // 0
two.remove(8);
System.out.println(one.countSameData(two)); // 2

public class SearchTree<E extends Comparable<E>> {
private SearchTreeNode<E> overallRoot; // root of overall tree

// post: constructs an empty search tree
public SearchTree() {
overallRoot = null;
}
  
// WRITE ADDITIONAL METHODS HERE:
  

  
  
  
  
  
  

// post: value added to tree so as to preserve binary search tree
public void add(E value) {
overallRoot = add(overallRoot, value);
}

// post: value added to tree so as to preserve binary search tree
private SearchTreeNode<E> add(SearchTreeNode<E> root, E value) {
if (root == null) {
root = new SearchTreeNode<E>(value);
} else if (root.data.compareTo(value) > 0) {
root.left = add(root.left, value);
} else if (root.data.compareTo(value) < 0) {
root.right = add(root.right, value);
}
return root;
}

// post: returns true if tree contains value, returns false otherwise
public boolean contains(E value) {
return contains(overallRoot, value);
}   

// post: returns true if given tree contains value, returns false otherwise
private boolean contains(SearchTreeNode<E> root, E value) {
if (root == null) {
return false;
} else {
int compare = value.compareTo(root.data);
if (compare == 0) {
return true;
} else if (compare < 0) {
return contains(root.left, value);
} else { // compare > 0
return contains(root.right, value);
}
}
}

// post: prints the data of the tree, one per line
public void print() {
printInorder(overallRoot);
}

// post: prints the data of the tree using an inorder traversal
private void printInorder(SearchTreeNode<E> root) {
if (root != null) {
printInorder(root.left);
System.out.println(root.data);
printInorder(root.right);
}
}

private static class SearchTreeNode<E> {
public E data; // data stored in this node
public SearchTreeNode<E> left; // left subtree
public SearchTreeNode<E> right; // right subtree

// post: constructs a leaf node with given data
public SearchTreeNode(E data) {
this(data, null, null);
}

// post: constructs a node with the given data and links
public SearchTreeNode(E data, SearchTreeNode<E> left,
SearchTreeNode<E> right) {
this.data = data;
this.left = left;
this.right = right;
}
}
}

Solutions

Expert Solution

//SearchTree.java

public class SearchTree<E extends Comparable<E>> {

      

      

       private SearchTreeNode<E> overallRoot; // root of overall tree

       // post: constructs an empty search tree

       public SearchTree() {

             overallRoot = null;

       }

      

            

       // post: value added to tree so as to preserve binary search tree

       public void add(E value) {

       overallRoot = add(overallRoot, value);

       }

       // post: value added to tree so as to preserve binary search tree

       private SearchTreeNode<E> add(SearchTreeNode<E> root, E value) {

       if (root == null) {

       root = new SearchTreeNode<E>(value);

       } else if (root.data.compareTo(value) > 0) {

       root.left = add(root.left, value);

       } else if (root.data.compareTo(value) < 0) {

       root.right = add(root.right, value);

       }

       return root;

       }

      

      

       // post: returns true if tree contains value, returns false otherwise

       public boolean contains(E value) {

       return contains(overallRoot, value);

       }

       // post: returns true if given tree contains value, returns false otherwise

       private boolean contains(SearchTreeNode<E> root, E value) {

       if (root == null) {

       return false;

       } else {

       int compare = value.compareTo(root.data);

       if (compare == 0) {

       return true;

       } else if (compare < 0) {

       return contains(root.left, value);

       } else { // compare > 0

       return contains(root.right, value);

       }

       }

       }

      

       // post: prints the data of the tree, one per line

       public void print() {

            

             if(overallRoot != null)

                    System.out.println("Root : "+overallRoot.data);

             printInorder(overallRoot);

       }

       // post: prints the data of the tree using an inorder traversal

       private void printInorder(SearchTreeNode<E> root) {

             if (root != null) {

      

                    printInorder(root.left);

            

                    System.out.println(root.data);

            

                    printInorder(root.right);

      

             }

       }

      

       // method that counts the number of tree nodes in "this" tree that are also contained in the "other" tree

       public int countSameData(SearchTree<E> other)

       {

             return(countSameDate(overallRoot,other));

       }

      

       // helper method to countSameDate

       private int countSameDate(SearchTreeNode<E> root, SearchTree<E> other)

       {

             // if the root is null

             if(root == null)

                    return 0;

             else if(other.contains(root.data)) // if root.data is present is other tree

             {

                    return 1+countSameDate(root.left,other)+countSameDate(root.right,other);

             }else // if root.data is not present is other tree

                    return countSameDate(root.left,other)+countSameDate(root.right,other);

       }

      

      

       // method that returns if this tree is equal to t2

       public boolean equals(SearchTree<E> t2) {

             return equals(overallRoot, t2.overallRoot);

       }

       private boolean equals(SearchTreeNode<E> n1, SearchTreeNode<E> n2) {

             if(n1 == null && n2 == null)

                    return true;

      

             if(n1 == null && n2 != null)

                    return false;

      

      

             if(n1 != null && n2 == null)

                    return false;

       return (n1.data == n2.data && equals(n1.left, n2.left) && equals(n1.right, n2.right));

       }

      

       // Removes the given value from this BST, if it exists.

       public void remove(E value) {

            

             overallRoot = remove(overallRoot, value);

       }

            

       private SearchTreeNode<E> remove(SearchTreeNode<E> root, E value) {

             if (root == null) {

                    return null;

             } else if (root.data.compareTo(value) > 0) {

                    root.left = remove(root.left, value);

             } else if (root.data.compareTo(value) < 0) {

                    root.right = remove(root.right, value);

             } else { // root.data == value; remove this node

                    if (root.right == null) {

                    return root.left; // no R child; replace w/ L

                    } else if (root.left == null) {

                    return root.right; // no L child; replace w/ R

                    } else {

                           SearchTreeNode<E> p = root.right;

                           SearchTreeNode<E> p_parent = root;

                           while(p.left != null)

                           {

                                 p_parent= p;

                                 p = p.left;

                           }

                          

                           if(p_parent != root)

                           {

                                 p_parent.left = null;

                                 p.left = root.left;

                                 p.right = root.right;

                           }else

                           {

                                 p.left = root.left;

                           }

                          

                           return p;

                    }

             }

                    return root;

       }

      

       private static class SearchTreeNode<E> {

             public E data; // data stored in this node

             public SearchTreeNode<E> left; // left subtree

             public SearchTreeNode<E> right; // right subtree

             // post: constructs a leaf node with given data

             public SearchTreeNode(E data) {

                    this(data, null, null);

             }

             // post: constructs a node with the given data and links

             public SearchTreeNode(E data, SearchTreeNode<E> left,SearchTreeNode<E> right) {

      

                    this.data = data;

      

                    this.left = left;

      

                    this.right = right;

      

                    }

             }

      

}

//end of SearchTree.java

//MainSearchTree.java

public class MainSearchTree {

      

      

       public static void main(String[] args)

       {

             SearchTree<Integer> zero = new SearchTree<Integer>();

             SearchTree<Integer> one = new SearchTree<Integer>();

             SearchTree<Integer> two = new SearchTree<Integer>();

             one.add(9); one.add(8); one.add(7); // all left nodes

             two.add(7); two.add(8); two.add(9); // all right nodes

             System.out.println(one.equals(two)); // false

             System.out.println(one.countSameData(two)); // 3

             System.out.println(one.countSameData(one)); // 3

             System.out.println(zero.countSameData(two)); // 0

             two.remove(8);

             System.out.println(one.countSameData(two)); // 2

       }

}

//end of MainSearchTree.java

Output:


Related Solutions

Take the following gedankenexperiment in which two astronauts meet each other again and again in a...
Take the following gedankenexperiment in which two astronauts meet each other again and again in a perfectly symmetrical setting - a hyperspherical (3-manifold) universe in which the 3 dimensions are curved into the 4. dimension so that they can travel without acceleration in straight opposite directions and yet meet each other time after time. On the one hand this situation is perfectly symmetrical - even in terms of homotopy and winding number. On the other hand the Lorentz invariance should...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
2 a. Write a new method for the Greeter class,    public void swapNames(Greeter other) {...}...
2 a. Write a new method for the Greeter class,    public void swapNames(Greeter other) {...} that swaps the names of this greeter and another instance. b. write a new method for the Greeter class:     public Greeter createQualifiedGreeter(String qualifier) { ..... } that returns a new Greeter object with its name being the qualifier string followed by " " and the executing greeter's name (i.e. this.name). For example:    Greeter g = new Greeter("world");    Greeter g2 = g.createQualifiedGreeter("beautiful");...
Is an auditor required to observe inventory counts if the company is a public U.K. corporation...
Is an auditor required to observe inventory counts if the company is a public U.K. corporation and what is the IAASB reference citation?
Is an auditor required to observe inventory counts of a public U.S. corporation and what is...
Is an auditor required to observe inventory counts of a public U.S. corporation and what is the PCAOB reference citation?
In a statement of cash flows (indirect method), which of the following are added to net...
In a statement of cash flows (indirect method), which of the following are added to net income to determine net cash flow from operating activities? Select one: a. Amortization of Premium on Bond Investments, but not Increase in Prepaid Expenses b. Increase in Prepaid Expenses, but not Amortization of Premium on Bond Investments c. Both Amortization of Premium on Bond Investments and Increase in Prepaid Expenses d. Neither Amortization of Premium on Bond Investments nor Increase in Prepaid Expenses
Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods: public static...
Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods: public static int reverse(int number) This method returns the positive integer obtained when the digits of the parameter is reversed. For example, when called with the parameter 12345, this method would return 54321. (Do this with proper integer arithmetic instead of just simply converting to String, reversing that and then using parseInt to extract the result). Hint: The number of digits of a number can be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT