Question

Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /**...

Im having trouble implimenting the addAll method..

import java.util.Collection;

public interface Tree<E> extends Collection<E> {
/** Return true if the element is in the tree */
public boolean search(E e);

/** Insert element e into the binary tree
* Return true if the element is inserted successfully */
public boolean insert(E e);

/** Delete the specified element from the tree
* Return true if the element is deleted successfully */
public boolean delete(E e);
  
/** Get the number of elements in the tree */
public int getSize();
  
  
  
/** Inorder traversal from the root*/
public default void inorder() {
}

/** Postorder traversal from the root */
public default void postorder() {
}

/** Preorder traversal from the root */
public default void preorder() {
}
  
/** Return the height of this binary tree */
public int height(); // Left as exercise: implement in BST
]ff /** BreadthFirst traversal from the root */
public default void breadthFirstTreversal() {
// Left as an exercise: implement in BST
}
  
  
  
@Override /** Return true if the tree is empty */
public default boolean isEmpty() {
return this.size() == 0;
}

@Override
public default boolean contains(Object e) {
return search((E)e);
}
  
@Override
public default boolean add(E e) {
return insert(e);
}
  
@Override
public default boolean remove(Object e) {
return delete((E)e);
}
  
@Override
public default int size() {
return getSize();
}
  
@SuppressWarnings("unchecked")
@Override
public default boolean containsAll(Collection<?> c) {
// Left as an exercise

return true;
}

@SuppressWarnings("unchecked")
@Override
public default boolean addAll(Collection<? extends E> c) {
// Left as an exercise
  
return true;
}

@SuppressWarnings("unchecked")
@Override
public default boolean removeAll(Collection<?> c) {
// Left as an exercise
  
return true;
}

@SuppressWarnings("unchecked")
@Override
public default boolean retainAll(Collection<?> c) {
// Left as an exercise
  
return true;

}

@Override
public default Object[] toArray() {
// Left as an exercise
  
return temp;
}

@SuppressWarnings("unchecked")
@Override
public default <T> T[] toArray(T[] array) {
// Left as an exercise
  
return array;
}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi

The addAll() method code is -

public default boolean addAll(Collection<? extends E> c) {
// Left as an exercise
   boolean flag = true;
       for (E element: c) {
           flag &= add(element);
       }
  
return true;
}

Add a comment
Know the answer?
Add Answer to:
Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /**...
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
  • STAGE 1: Implement the following methods in “BST.java” class: /** Return the height of this binar...

    STAGE 1: Implement the following methods in “BST.java” class: /** Return the height of this binary tree 20 points*/ public int height() {    // Left as exercise    return 0; } /** BreadthFirst traversal from the root 20 points */ public void breadthFirstTraversal() {    // Left as an exercise } STAGE 2: Implement the following methods in “Tree.java” class: @SuppressWarnings("unchecked") @Override public default boolean containsAll(Collection<?> c) {    // Left as an exercise 10 points          return true; } @SuppressWarnings("unchecked") @Override public default...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • Test Data would be as follows: Using java language. Build an expression tree. • When you...

    Test Data would be as follows: Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

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
ADVERTISEMENT