Menu

[Solved]Task Take Files Inttreebagjava Intbtnodejava Fill Following Methods O Add O Remove Create Q37212678

Your task is to take the files IntTreeBag.java andIntBTNode.java and fill in the following methods. o add o remove •Then create a driver file that creates an IntTreeBag object andfills it up using add and removes values using remove. Use themethod print from the IntBTNode class to take a snapshot of thetree at any given time. • The complete IntBTNode class and anincomplete IntTreeBag class are left for you in the class examplesfolder for chapter 9. Attach a printout of these files along withyour driver to the answers for the questions above.

here is the IntBTNode.java class

https://www.cs.colorado.edu/~main/edu/colorado/nodes/IntBTNode.java

and here is the incomplete IntTreeBag.java I added the add andremove methods I just need the driver program to add andremove.

// File: IntTreeBag.java from the packageEDU.colorado.linked
// Complete documentation is available from the IntTreeBag linkin:
// http://www.cs.colorado.edu/~main/docs/

//package edu.colorado.collections;
//import edu.colorado.nodes.IntBTNode;

/******************************************************************************
* An <CODE>IntTreeBag</CODE> is a collection of intnumbers.
*
* <dl><dt><b>Limitations:</b><dd>
* Beyond <CODE>Integer.MAX_VALUE</CODE> elements,<CODE>countOccurrences</CODE>,
* and <CODE>size</CODE> are wrong.
*
* <dt><b>Java Source Code for thisclass:</b><dd>
* <AHREF=”../EDU/colorado/collections/IntTreeBag.java”>
*http://www.cs.colorado.edu/~main/EDU/colorado/collections/IntTreeBag.java
* </A>
*
* @author Michael Main
* <A HREF=”mailto:main@colorado.edu”> (main@colorado.edu)</A>
*
* @version
* Nov 11, 2000
*
* @see IntArrayBag
* @see IntLinkedBag
******************************************************************************/
public class IntTreeBag implements Cloneable
{
   // Invariant of the IntTreeBag class:
   // 1. The elements in the bag are stored in a binarysearch tree.
   // 2. The instance variable root is a reference to theroot of the
   // binary search tree (or null for an emptytree).
   private IntBTNode root;

   /**
   * Insert a new element into this bag.
   * @param <CODE>element</CODE>
   * the new element that is being inserted
   *<dt><b>Postcondition:</b><dd>
   * A new copy of the element has been added to thisbag.
   * @exception OutOfMemoryError
   * Indicates insufficient memory a new IntBTNode.
   **/
   public void add(int element)
   {
IntBTNode cursor;
boolean done;

if (root == null)
{ // Add the first element to the binary search tree
   root = new IntBTNode(element, null, null);
}
else
{ // Add a new leaf to the binary search tree
   cursor = root;
   done = false;
   do
   {
if (cursor.getData( ) >= element)
{ // Go left
   if (cursor.getLeft( ) == null)
   {
cursor.setLeft( new IntBTNode(element, null, null) );
done = true;
   }
   else
cursor = cursor.getLeft( );
}
else
{ // Go right
   if (cursor.getRight( ) == null)
   {
cursor.setRight( new IntBTNode(element, null, null) );
done = true;
   }
   else
cursor = cursor.getRight( );
}
   } while (!done);
}
   }

   /**
   * Add the contents of another bag to this bag.
   * @param <CODE>addend</CODE>
   * a bag whose contents will be added to this bag
   *<dt><b>Precondition:</b><dd>
   * The parameter, <CODE>addend</CODE>, isnot null.
   *<dt><b>Postcondition:</b><dd>
   * The elements from <CODE>addend</CODE>have been added to this bag.
   * @exception IllegalArgumentException
   * Indicates that <CODE>addend</CODE> isnull.
   * @exception OutOfMemoryError
   * Indicates insufficient memory to increase the sizeof the bag.
   **/
   public void addAll(IntTreeBag addend)
   {
IntBTNode addroot;

if (addend == null)
{
   throw new IllegalArgumentException(“Nulladdend”);
}
if (root == addend.root)
{ // Addend is the same as the bag that activated this method
   addroot = IntBTNode.treeCopy(addend.root);
   addTree(addroot);
}
else
addTree(addend.root);
   }

   /**
   * Add the contents of another binary search tree tothis bag.
   * @param <CODE>addroot</CODE>
   * the root of a binary search tree whose contents willbe added to this bag
   *<dt><b>Postcondition:</b><dd>
   * The elements from the binary search tree specifiedby
   * <CODE>addroot</CODE> have been added tothis bag.
   * @exception OutOfMemoryError
   * Indicates insufficient memory to increase the sizeof the bag.
   **/
   private void addTree(IntBTNode addroot)
   {
// implement this method

   }
  
  
   /**
   * Generate a copy of this bag.
   * @param – none
   * @return
   * The return value is a copy of this bag. Subsequentchanges to the
   * copy will not affect the original, nor vice versa.Note that the return
   * value must be type cast to an<CODE>IntTreeBag</CODE> before it can be used.
   * @throw OutOfMemoryError
   * Indicates insufficient memory for creating theclone.
   **/
   public Object clone( )
   { // Clone a nIntTreeBag object.
IntTreeBag answer;

try
{
   answer = (IntTreeBag) super.clone( );
}
catch (CloneNotSupportedException e)
{ // This would indicate an internal error in the Java runtimesystem
   // because super.clone always works for anObject.
   throw new InternalError(e.toString( ));
}

answer.root = IntBTNode.treeCopy(root);

return answer;
   }
  

   /**
   * Accessor method to count the number of occurrencesof a particular element
   * in this bag.
   * @param <CODE>target</CODE>
   * the element that needs to be counted
   * @return
   * the number of times that<CODE>target</CODE> occurs in this bag
   **/
   public long countOccurrences(int target)
   {
long answer;
IntBTNode cursor;

answer = 0;
cursor = root;
while (cursor != null)
{
   if (cursor.getData( ) < target)
cursor = cursor.getRight( );
   else
   {
if (cursor.getData( ) == target)
   answer++;
cursor = cursor.getLeft( );
   }
}
return answer;
   }
  
  
   /**
   * Remove one copy of a specified element from thisbag.
   * @param <CODE>target</CODE>
   * the element to remove from the bag
   *<dt><b>Postcondition:</b><dd>
   * If <CODE>target</CODE> was found in thebag, then one copy of
   * <CODE>target</CODE> has been removed andthe method returns true.
   * Otherwise the bag remains unchanged and the methodreturns false.
   **/
   //private boolean remove(int target)
   public boolean remove(int target)
   {
IntBTNode parentOfCursor = null;
IntBTNode cursor = root;

while (cursor != null && target != cursor.getData( ))
{
   parentOfCursor = cursor;
   if (target < cursor.getData( ))
cursor = cursor.getLeft( );
   else
cursor = cursor.getRight( );
}

if (cursor == null)
   return false;
else if (cursor.getLeft( ) == null)
{
   if (parentOfCursor == null)
root = cursor.getRight( );
   else if (cursor == parentOfCursor.getLeft( ))
parentOfCursor.setLeft(cursor.getRight( ));
   else
parentOfCursor.setRight(cursor.getRight( ));
   return true;
}
else
{
   cursor.setData(cursor.getLeft( ).getRightmostData());
   cursor.setLeft(cursor.getLeft( ).removeRightmost());
   return true;
}
   }
  

   /**
   * Determine the number of elements in this bag.
   * @param – none
   * @return
   * the number of elements in this bag
   **/
   public int size( )
   {
// implement this method
return -1; // replace this place holder

   }

   public void print( )
   {
root.print(0);

   }
  

   /**
   * Create a new bag that contains all the elements fromtwo other bags.
   * @param <CODE>b1</CODE>
   * the first of two bags
   * @param <CODE>b2</CODE>
   * the second of two bags
   *<dt><b>Precondition:</b><dd>
   * Neither b1 nor b2 is null.
   * @return
   * the union of b1 and b2
   * @exception IllegalArgumentException
   * Indicates that one of the arguments is null.
   * @exception OutOfMemoryError
   * Indicates insufficient memory for the new bag.
   **/
   public static IntTreeBag union(IntTreeBag b1,IntTreeBag b2)
   {
// implement this method
return null; // replace this place holder

   }

   public int height() {
return IntBTNode.treeHeight(root);
   }
}

Expert Answer


Answer to Your task is to take the files IntTreeBag.java and IntBTNode.java and fill in the following methods. o add o remove • … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *