Question
P1
Suppose we want to create a method for the class BinaryTree (file BinaryTree.java) that
counts the number of times an object occurs in the tree.
a. Write the method
public int count1(T anObject)
which calls the private recursive method
private int count1(BinaryNode<T> rootNode, T anObject)
to count the number of occurrences of anObject
b. Write the method
public int count2(T anObject)
that counts the number of occurrences of anObject and that uses one of the iterators of the
binary tree.
Compare the efficiencies of the previous the two methods count1 and count2 using big O
notation. Add your answer as a comment before the function definition
Suppose we want to create a method for the class BinaryTree (file BinaryTree.java) that
counts the number of times an object occurs in the tree.
a. Write the method
public int count1(T anObject)
which calls the private recursive method
private int count1(BinaryNode<T> rootNode, T anObject)
to count the number of occurrences of anObject
b. Write the method
public int count2(T anObject)
that counts the number of occurrences of anObject and that uses one of the iterators of the
binary tree.
Compare the efficiencies of the previous the two methods count1 and count2 using big O
notation. Add your answer as a comment before the function definition
P2
Suppose we want to create a method for the class BinaryTree that decides whether two trees
have the same structure. Two trees t1 and t2 have the same structure if:
- If one has a left child, then both have left children and the left children are isomorphic,
AND
- if one has a right child, then both have right children and the right children are
isomorphic
The header of the method is:
public boolean isIsomorphic(BinaryTree<T> otherTree)
Write this method, using a private recursive method of the same name.
P3
Design an algorithm that produces a binary expression tree from a given infix expression. You
can assume that the infix expression is a string that has only the binary operators +, -, *, /,
parantheses, and one-letter operands. Implement the solution as a construction in
ExpressionTree that takes a string argument:
public ExpressionTree(String infix)
that calls the private method
private ExpressionTree formTree(String expr, int first, int last)
to construct the tree. formTree() builds the tree recursively where first and last are the first and
last index in the string corresponding to the tree you want to construc
isomorphic
The header of the method is:
public boolean isIsomorphic(BinaryTree<T> otherTree)
Write this method, using a private recursive method of the same name.
P3
Design an algorithm that produces a binary expression tree from a given infix expression. You
can assume that the infix expression is a string that has only the binary operators +, -, *, /,
parantheses, and one-letter operands. Implement the solution as a construction in
ExpressionTree that takes a string argument:
public ExpressionTree(String infix)
that calls the private method
private ExpressionTree formTree(String expr, int first, int last)
to construct the tree. formTree() builds the tree recursively where first and last are the first and
last index in the string corresponding to the tree you want to construc
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps
Knowledge Booster
Similar questions
- Write the code in C++arrow_forwardPlease do not change any of the method signatures in either class. Implement the methods described below. RadixSort.java RadixSort.java contains two different RadixSort implementations each using a different version of Counting Sort that you will implement. As a reminder the pseudocode for CountingSort discussed in class is as follows. countingSort(arr, n, k) 1. let B[1 : n] and C[0 : k] be new arrays 2. for i = 0 to k 3. C[i] = 0 4. for j = 1 to n 5 . C[arr[j]] = C [arr[j]] + 1 6. for i = 1 to k 7. C[i] = C[i] + C[i – 1] 8. for j = n downto 1 9. B[C[arr[j]]] = arr[j] 10 C[arr[j]] = C[arr[j]] – 1 11. return B private static void countingSort2(int[] arr, int k) Now you implement almost the same method, but instead of iterating from the end of arr, you will instead iterate from the beginning. So line 8 becomes for j=1 to n. You should think about why this alternative does not impact the correctness of CountingSort. You don’t need to write this down, just think about it.…arrow_forwardImplement given operations in java : * Removes the first instance of the specified element from this * list and returns it. Throws an EmptyCollectionException * if the list is empty. Throws a ElementNotFoundException if the * specified element is not found in the list. * param targetElement the element to be removed from the list * return a reference to the removed element * throws EmptyCollectionException if the list is empty * throws ElementNotFoundException if the target element is not foundarrow_forward
- We have a parking office class for an object-oriented parking management system using java Implement the following methods for our class Add equals and hashCode methods to any class used in a List. Add a method to the Parking Office to return a collection of customer ids (getCustomerIds) Add a method to the Parking Office to return a collection of permit ids (getPermitIds) Add a method to the Parking Office to return the collection of permit ids for a specific customer (getPermitIds(Customer)) The above methods are not included in the parking office class of our class diagram. I have attached class diagrams with definitions of related classes in our system (i.e car, customer, .....)arrow_forwardGiven a stack of positive integers, write a Java method called movePrimes that rearranges elements of the stack such that all prime numbers are below none primes regardless to the order of primes. Note: You are allowed to create only one other stack. You can not use other data structures such as array, arraylist, queue, ....etc. Consider the following example:arrow_forwardQuestion 3arrow_forward
- Please answer the question in the screenshot. The language used here is in Java.arrow_forwardwrite a java method : Write a recursive private method called countDegree to be included in class BinaryTree as discussed in the lectures. If a node is having two child nodes, then its degree is two, if it is having one child node, its degree is one and leaf nodes have degree 0. The method counts and returns the total degree of all the nodes in the binary tree. Example: If a binary tree is having 9 nodes such that 3 nodes, each have 2 child nodes, 2 nodes each have only one child and there are 4 leaf nodes. So, the total degree of the binary tree = 3x2 + 2x1 + 0 = 8. This method is called from a public method countDegreeBT, given as follows: public int countDegreeBT() { return countDegree(root); } Method heading: private int countDegree(Node<E> node)arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forward
- Given the doubly linked list data structure discussed in the lecture, implement a subclass “DuplicateManipuationList” that has a new function for removing duplicates froma list.- Add a new method to your DuplicateManipuationList called removeDuplicates(…). Themethod takes as an argument called ListToRemove that is a singly linked list thatcontains some values. The method should scan the linked list for each member of theListToRemove, and remove all occurrences (not only the duplicates) of that member.Besides updating the DuplicateManipulationList, the removeDuplicates(…) functionshould return a new list that shows the number of duplicates for each deleted member.Note: That with inheritance:- You have a parent class called “Node” that contain [data, *next, *prev] and a sub classcalled “DuplicateManipuationList”arrow_forwardComplete the below function (using recursion)arrow_forwardCreate a nested class called DoubleNode that allows you to create doubly-linked lists with each node containing a reference to the item before it and the item after it (or null if neither of those items exist). Then implement static methods for the following operations: insert before a given node, insert after a given node, remove a given node, remove from a given node, insert at the beginning, insert at the end, remove from a given node, and remove a given node.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios