Question

Since we do not want to have to rewrite this code when the element type changes,...

Since we do not want to have to rewrite this code when the element type changes, this classes uses a Comparator to assist in ordering the elements. You will need to complete the siftUpComparator() and siftDownComparator() methods in the LinkedHeap Class.

siftUp

§Added element may violate heap-order property

§siftUp() restores order starting at added node

§Processing will continue working up the tree until:

úFinds properly ordered node & parent

úReaches the root (reaches node without parent)

siftDown

§Restores heap’s order as last step of remove()

úProcess of restoring order starts at root

úCompare childs' elements to find which more important

úSmaller child element compared with node's element

úSwap when out of order to regain balance in this node

§But method must continue with swapped child

úStop when node has no children (siftDown() hits leaf)

úsiftDown() also stops when elements not unordered

----------------------------------

LinkedHeap

package edu.buffalo.cse116;

import java.util.Comparator;
import java.util.NoSuchElementException;

/**
* This uses a heap to implement
*
* @author Lewis and Chase
* @author Matthew Hertz
* @version 4.0
* @param Type of (Comparable) element to be stored in this priority queue.
*/
public class LinkedHeap extends BinaryTree {
/** Stores the last node that we added to this structure. */
private BTNode lastNode;

/** Comparator used to organize and order the elements in the heap. */
private Comparator comparator;

/** Number of nodes/elements within this binary tree. */
private int size;

/**
* Creates a new (empty) heap which will uses the specified Comparator to order its elements.
*
* @param comp Comparator that this heap will use to order its elements.
*/
public LinkedHeap(Comparator comp) {
comparator = comp;
}

/**
* Returns the size of this BinarySearchTree object.
*
* @return the size of this BinarySearchTree object.
*/
@Override
public int size() {
return size;
}

/**
* Adds the specified element to this heap in the appropriate position according to its key value.
*
* @param obj the element to be added to the heap
* @return Since this method will always succeed, it only returns true.
*/
@Override
public boolean add(T obj) {
BTNode node = new BTNode<>();
node.setElement(obj);

if (getRootNode() == null) {
setRootNode(node);
} else {
BTNode nextParent = getNextParentAdd();
if (nextParent.getLeft() == null) {
nextParent.setLeft(node);
} else {
nextParent.setRight(node);
}
node.setParent(nextParent);
}
lastNode = node;
size += 1;
siftUpComparator(node, obj);
return true;
}

/**
* Returns the node that will be the parent of the new node
*
* @return the node that will be the parent of the new node
*/
private BTNode getNextParentAdd() {
BTNode result = lastNode;

while ((result != getRootNode()) && (result.getParent().getLeft() != result)) {
result = result.getParent();
}

if (result != getRootNode()) {
if (result.getParent().getRight() == null) {
result = result.getParent();
} else {
result = result.getParent().getRight();
while (result.getLeft() != null) {
result = result.getLeft();
}
}
} else {
while (result.getLeft() != null) {
result = result.getLeft();
}
}

return result;
}

/**
* Reorders this heap after creating the space for an element in the tree.
*
* @param node BTNode in our linked tree that needs to be filled
* @param elem Element that we are adding to the tree
*/
private void siftUpComparator(BTNode node, T elem) {

}

/**
* Remove the element with the lowest value in this heap and returns a reference to it. Throws an
* EmptyCollectionException if the heap is empty.
*
* @return the element with the lowest value in this heap
*/
public T remove() {
if (isEmpty()) {
throw new NoSuchElementException("Cannot call remove on an empty LinkedHeap");
}

BTNode rootNode = getRootNode();
T minElement = rootNode.getElement();

if (size() == 1) {
setRootNode(null);
lastNode = null;
} else {
BTNode nextLast = getNewLastNode();
if (lastNode.getParent().getLeft() == lastNode) {
lastNode.getParent().setLeft(null);
} else {
lastNode.getParent().setRight(null);
}
T shuffleElem = lastNode.getElement();
rootNode.setElement(shuffleElem);
lastNode = nextLast;
siftDownComparator(rootNode, shuffleElem);
}
size -= 1;
return minElement;
}

/**
* Reorders this heap after unlinking the node that is actually removed.
*
* @param node BTNode in our linked tree from which our down shifting need to start
* @param elem Element that was in the (now removed) node to be shifted into the tree.
*/
private void siftDownComparator(BTNode node, T elem) {

}


/**
* Returns the node that will be the new last node after a remove.
*
* @return the node that willbe the new last node after a remove
*/
private BTNode getNewLastNode() {
BTNode result = lastNode;

while ((result != getRootNode()) && (result.getParent().getLeft() == result)) {
result = result.getParent();
}

if (result != getRootNode()) {
result = result.getParent().getLeft();
}

while (result.getRight() != null) {
result = result.getRight();
}

return result;
}

/**
* Returns the element with the lowest value in this heap. Throws an EmptyCollectionException if the heap is empty.
*
* @return the element with the lowest value in this heap
*/
public T element() {
if (isEmpty()) {
throw new NoSuchElementException("Cannot call remove on an empty LinkedHeap");
}

BTNode rootNode = getRootNode();
T minElement = rootNode.getElement();
return minElement;
}
}

------------------------------------

*Class that LinkedHeap needs, no need to change*

BTNode

package edu.buffalo.cse116;

public class BTNode {
/** Tree's element which is stored within this Node. */
private E element;

/** Left child of the current Node. */
private BTNode left;
/** Right child of the current Node. */
private BTNode right;
/** Parent in the binary tree for the current Node. */
private BTNode parent;

/**
* Initializes this Entry object. This default constructor is defined for future expansion purposes.
*/
public BTNode() {}

/**
* Initializes this Entry object from element and parent.
*/
public BTNode(E element, BTNode parent) {
this.element = element;
this.parent = parent;
}

/** Return the element stored in this node. */
public E getElement() {
return element;
}

/** Specify a new element to be stored at this node. */
public void setElement(E element) {
this.element = element;
}

/** Get the node's left child. */
public BTNode getLeft() {
return left;
}

/** Specify a node to be the left child of the current node. */
public void setLeft(BTNode left) {
this.left = left;
}

/** Get the node's right child. */
public BTNode getRight() {
return right;
}

/** Specify a node to be the right child of the current node. */
public void setRight(BTNode right) {
this.right = right;
}

/** Get the node's parent in the tree. This is null if the node is a root. */
public BTNode getParent() {
return parent;
}

/** Specify a node to be the parent of the current node. */

public void setParent(BTNode parent) {
this.parent = parent;
}

/** Provides a String representation of the node for use in debugging. */
@Override
public String toString() {
return "BTNode: " + element;
}
}

--------------------

*Class that LinkedHeap needs, no need to change*

BinaryTree

package edu.buffalo.cse116;

import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Instances of this class represent a vanilla binary tree (e.g., and not a binary search tree).
*
* @author Matthew Hertz
* @param Type of data stored in each node of this tree. Since this is not a BST, E can be anything.
*/
public class BinaryTree extends AbstractCollection {

/** Root node of this binary tree */
private BTNode root;


/**
* Initializes this BinarySearchTree object to be empty, to contain only elements of type E, to be ordered by the
* Comparable interface, and to contain no duplicate elements.
*/
public BinaryTree() {
root = null;
}


/**
* Returns a reference to the node at the root or null if the tree is empty. This is needed for future uses of a
* linked binary tree.
*
* @return Root node for this tree or null if the tree is empty.
*/
protected BTNode getRootNode() {
return root;
}

/**
* Specifies a new root node for the tree. This is designed to allow future subclasses to make these changes.
*
* @param newRoot The new node to be used as the root for this tree.
*/
protected void setRootNode(BTNode newRoot) {
root = newRoot;
}

/**
* Iterator method that has, at last, been implemented.
*
* @return an iterator positioned at the smallest element in this BinarySearchTree object.
*/
@Override
public Iterator iterator() {
return new TreeIterator();
}

/**
* Determines if there is at least one element in this binary tree object that equals a specified element.
*
* @param obj - the element sought in this binary tree
* @return true if the object is an element in the binary tree; false othrwise.
*/
@Override
public boolean contains(Object obj) {
return getBTNode(obj) != null;
}


/**
* Finds the BTNode object that houses a specified element, if there is such an BTNode.
*
* @param obj - the element whose BTNode is sought.
* @return the BTNode object that houses obj - if there is such an BTNode; otherwise, return null.
*/
protected BTNode getBTNode(Object obj) {
return getBTNode(root, obj);
}

/**
* Finds the BTNode object that houses a specified element in the subtree rooted at subtreeRoot, if there is such an
* BTNode.
*
* @param obj - the element whose BTNode is sought.
* @return the BTNode object that houses obj - if there is such an BTNode in the subtree at subtreeRoot; otherwise,
* return null.
*/
protected BTNode getBTNode(BTNode subtreeRoot, Object obj) {
if (subtreeRoot == null) {
return null;
} else if ((subtreeRoot.getElement() == obj) ||
((subtreeRoot.getElement() != null) && subtreeRoot.getElement().equals(obj))) {
return subtreeRoot;
}
if (subtreeRoot.getLeft() != null) {
BTNode retVal = getBTNode(subtreeRoot.getLeft(), obj);
if (retVal != null) {
return retVal;
}
}
if (subtreeRoot.getRight() != null) {
BTNode retVal = getBTNode(subtreeRoot.getRight(), obj);
if (retVal != null) {
return retVal;
}
}
return null;
}

/**
* Finds the successor of a specified Entry object in this BinarySearchTree. The worstTime(n) is O(n) and
* averageTime(n) is constant.
*
* @param e - the Entry object whose successor is to be found.
* @return the successor of e, if e has a successor; otherwise, return null.
*/
protected BTNode successor(BTNode e) {
if (e == null) {
return null;
} else if (e.getRight() != null) {
// successor is leftmost Entry in right subtree of e
BTNode p = e.getRight();
while (p.getLeft() != null) {
p = p.getLeft();
}
return p;

} // e has a right child
else {

// go up the tree to the left as far as possible, then go up
// to the right.
BTNode p = e.getParent();
BTNode ch = e;
while ((p != null) && (ch == p.getRight())) {
ch = p;
p = p.getParent();
} // while
return p;
} // e has no right child
} // method successor

private class TreeIterator implements Iterator {

private BTNode cursor;

/**
* Positions this TreeIterator to the left-most element in the tree.
*/
public TreeIterator() {
cursor = root;
if (cursor != null) {
while (cursor.getLeft() != null) {
cursor = cursor.getLeft();
}
}
}

/**
* Determines if there are still some elements that have not been accessed by this TreeIterator object.
*
* @return true - if there are still some elements that have not been accessed by this TreeIterator object;
* otherwise, return false.
*/
@Override
public boolean hasNext() {
return cursor != null;
}

/**
* Returns the element in the BTNode this TreeIterator object was positioned at before this call, and advances this
* TreeIterator object.
*
* @return the element this TreeIterator object was positioned at before this call.
* @throws NoSuchElementException - if this TreeIterator object was not positioned at a BTNode before this call.
*/
@Override
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E retVal = cursor.getElement();
cursor = successor(cursor);
return retVal;
}

/**
* Not implemented.
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

@Override
public int size() {
throw new UnsupportedOperationException();
}
}

-------------------------

Test code for LinkedHeap

package edu.buffalo.cse116;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Comparator;

import org.junit.Before;
import org.junit.Test;

/**
* JUnit test cases for your heap class.
*
* @author Matthew Hertz
*/
public class LinkedHeapTest {
/** Values which we will be adding to the list. */
private static final String[] DATA = { "Alpha", "Bravo", "Charlie", "DeltaForce", "Echo", "Foxtrot", "Golf",
"HotelCalifornia" };

/** "Random" ordering in which data is added to the list. */
private static final int[] PRIORITY = { 5, 3, 1, 6, 4, 2, 0, 7 };

/** Ordering in which each element will be removed from the list using our special comparator. */
private static final int[] REMOVAL = { 6, 4, 1, 0, 5, 2, 3, 7 };

/** Dictionary instance we will test. */
private LinkedHeap pq;

/** Setup the heap before each test */
@Before
public void setUp() {
pq = new LinkedHeap<>(new WeirdStringComparator());
}

/**
* Test whether this node obeys the completeness property.
*
* @param node Node we want to check w.r.t. its children.
*/
private void checkHeapCompleteness(BTNode node) {
if (node.getLeft() != null) {
BTNode left = node.getLeft();
if ((left.getLeft() != null) && (left.getRight() != null)) {
assertNotNull("Need to complete one level before starting the next.", node.getRight());
} else if (node.getRight() != null) {
BTNode right = node.getRight();
assertTrue("Need to add nodes from left to right.", (right.getLeft() == null) && (right.getRight() == null));
}
} else {
assertNull("Need to add nodes from left to right!", node.getRight());
}
}

/**
* Test whether this node obeys the heap-order property.
*
* @param node Node we want to check w.r.t. its children.
*/
private void checkLegalBinaryTreeNode(BTNode node) {
String parentEnt = node.getElement();
assertNotNull("Node has null for its element!", parentEnt);
if (node.getLeft() != null) {
String leftEnt = node.getLeft().getElement();
assertNotNull("Oops, left child's element is null.", leftEnt);
assertTrue("Oops. Child (" + leftEnt + ") is smaller than its parent (" + parentEnt + ")!",
(leftEnt.length() >= parentEnt.length()));
}
if (node.getRight() != null) {
String rightEnt = node.getRight().getElement();
assertNotNull("Oops, right child's element is null.", rightEnt);
assertTrue("Oops. Child (" + rightEnt + ") is smaller than its parent (" + parentEnt + ")!",
(rightEnt.length() >= parentEnt.length()));
}
}

/**
* Traverse through all the nodes in the tree and check each for heap order and completeness properties.
*/
private void traverseNodesAndCheck() {
ArrayList> nodeList = new ArrayList<>();
BTNode pos = pq.getRootNode();
nodeList.add(pos);
while (!nodeList.isEmpty()) {
BTNode node = nodeList.remove(0);
checkHeapCompleteness(node);
checkLegalBinaryTreeNode(node);
if (node.getLeft() != null) {
nodeList.add(node.getLeft());
}
if (node.getRight() != null) {
nodeList.add(node.getRight());
}
}
}

@Test
public void testUpHeapWhenEmpty() {
pq.add(DATA[0]);
traverseNodesAndCheck();
}

@Test
public void testChainInsert() {
for (int i = 0; i < DATA.length; i++) {
pq.add(DATA[PRIORITY[i]]);
assertEquals("Not enough nodes in the heap?", i + 1, pq.size());
traverseNodesAndCheck();
}
}

@Test
public void testChainInsertRemoveMin() {
for (int i = 0; i < DATA.length; i++) {
pq.add(DATA[PRIORITY[i]]);
}
for (int i = 0; i < DATA.length; i++) {
String removeEnt = pq.remove();
assertEquals("Did not find the value I expected after " + (i + 1) + " calls to removeMin()", DATA[REMOVAL[i]],
removeEnt);
assertEquals("Incorrect number of nodes after calling removeMin()", DATA.length - (i + 1), pq.size());
if (i != (DATA.length - 1)) {
traverseNodesAndCheck();
}
}
}

private class WeirdStringComparator implements Comparator {

@Override
public int compare(String o1, String o2) {
return (o1.length() - o2.length());
}

}
}

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

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Comparator;

import java.util.NoSuchElementException;

/**

* This uses a heap to implement

*

* @author Lewis and Chase

* @author Matthew Hertz

* @version 4.0

* @param Type of (Comparable) element to be stored in this priority queue.

*/

public class LinkedHeap<T> extends BinaryTree<T> {

   /** Stores the last node that we added to this structure. */

   private BTNode<T> lastNode;

   /** Comparator used to organize and order the elements in the heap. */

   private Comparator<T> comparator;

   /** Number of nodes/elements within this binary tree. */

   private int size;

   /**

   * Creates a new (empty) heap which will uses the specified Comparator to order its elements.

   *

   * @param comp Comparator that this heap will use to order its elements.

   */

   public LinkedHeap(Comparator<T> comp) {

       comparator = comp;

   }

   /**

   * Returns the size of this BinarySearchTree object.

   *

   * @return the size of this BinarySearchTree object.

   */

   @Override

   public int size() {

       return size;

   }

   /**

   * Adds the specified element to this heap in the appropriate position according to its key value.

   *

   * @param obj the element to be added to the heap

   * @return Since this method will always succeed, it only returns true.

   */

   @Override

   public boolean add(T obj) {

       BTNode<T> node = new BTNode<>();

       node.setElement(obj);

       if (getRootNode() == null) {

           setRootNode(node);

       } else {

           BTNode<T> nextParent = getNextParentAdd();

           if (nextParent.getLeft() == null) {

               nextParent.setLeft(node);

           } else {

               nextParent.setRight(node);

           }

           node.setParent(nextParent);

       }

       lastNode = node;

       size += 1;

       siftUpComparator(node, obj);

       return true;

   }

   /**

   * Returns the node that will be the parent of the new node

   *

   * @return the node that will be the parent of the new node

   */

   private BTNode<T> getNextParentAdd() {

       BTNode<T> result = lastNode;

       while ((result != getRootNode()) && (result.getParent().getLeft() != result)) {

           result = result.getParent();

       }

       if (result != getRootNode()) {

           if (result.getParent().getRight() == null) {

               result = result.getParent();

           } else {

               result = result.getParent().getRight();

               while (result.getLeft() != null) {

                   result = result.getLeft();

               }

           }

       } else {

           while (result.getLeft() != null) {

               result = result.getLeft();

           }

       }

       return result;

   }

   /**

   * Reorders this heap after creating the space for an element in the tree.

   *

   * @param node BTNode in our linked tree that needs to be filled

   * @param elem Element that we are adding to the tree

   */

   private void siftUpComparator(BTNode<T> node, T elem) {

       //int p = parent(i);

       // comp - differentiate MaxHeap and MinHeap

       // percolates up

       if((node.getParent()!= null) && comparator.compare(node.getParent().getElement(), node.getElement()) > 0 )

       {

           //Exch(A[i], A[p]);

           T item = node.getParent().getElement();

           node.getParent().setElement(node.getElement());

           node.setElement(item);

           siftUpComparator(node.getParent(), node.getParent().getElement());

       }

   }

   /**

   * Remove the element with the lowest value in this heap and returns a reference to it. Throws an

   * EmptyCollectionException if the heap is empty.

   *

   * @return the element with the lowest value in this heap

   */

   public T remove() {

       if (isEmpty()) {

           throw new NoSuchElementException("Cannot call remove on an empty LinkedHeap");

       }

       BTNode<T> rootNode = getRootNode();

       T minElement = rootNode.getElement();

       if (size() == 1) {

           setRootNode(null);

           lastNode = null;

       } else {

           BTNode<T> nextLast = getNewLastNode();

           if (lastNode.getParent().getLeft() == lastNode) {

               lastNode.getParent().setLeft(null);

           } else {

               lastNode.getParent().setRight(null);

           }

           T shuffleElem = lastNode.getElement();

           rootNode.setElement(shuffleElem);

           lastNode = nextLast;

           //System.out.println("*****"+rootNode.getElement());

           siftDownComparator(rootNode, shuffleElem);

       }

       size -= 1;

       return minElement;

   }

   /**

   * Reorders this heap after unlinking the node that is actually removed.

   *

   * @param node BTNode in our linked tree from which our down shifting need to start

   * @param elem Element that was in the (now removed) node to be shifted into the tree.

   */

   private void siftDownComparator(BTNode<T> node, T elem) {

      

       if(node != null)

       {

           System.out.println("parent: "+node.getElement());

           System.out.println("Left: "+node.getLeft().getElement());

           System.out.println("Right: "+node.getRight().getElement());

           BTNode<T> smallest = node;

           if(node.getLeft()!= null && comparator.compare(smallest.getElement(), node.getLeft().getElement()) > 0){

               //System.out.println("Left: "+node.getLeft().getElement());

               smallest = node.getLeft();

           }

           if(node.getRight()!= null && comparator.compare(smallest.getElement(), node.getRight().getElement()) > 0){

               //System.out.println("Right: "+node.getRight().getElement());

               smallest = node.getRight();

           }

           if(smallest != node){

               T item = node.getElement();

               node.setElement(smallest.getElement());

               smallest.setElement(item);

               siftUpComparator(smallest, smallest.getElement());

           }

       }

   }

   /**

   * Returns the node that will be the new last node after a remove.

   *

   * @return the node that willbe the new last node after a remove

   */

   private BTNode<T> getNewLastNode() {

       BTNode<T> result = lastNode;

       while ((result != getRootNode()) && (result.getParent().getLeft() == result)) {

           result = result.getParent();

       }

       if (result != getRootNode()) {

           result = result.getParent().getLeft();

       }

       while (result.getRight() != null) {

           result = result.getRight();

       }

       return result;

   }

   /**

   * Returns the element with the lowest value in this heap. Throws an EmptyCollectionException if the heap is empty.

   *

   * @return the element with the lowest value in this heap

   */

   public T element() {

       if (isEmpty()) {

           throw new NoSuchElementException("Cannot call remove on an empty LinkedHeap");

       }

       BTNode<T> rootNode = getRootNode();

       T minElement = rootNode.getElement();

       return minElement;

   }

}

Add a comment
Know the answer?
Add Answer to:
Since we do not want to have to rewrite this code when the element type changes,...
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
  • Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap...

    Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...

  • For the code below complete the preOrder() method so that it performs a preOrder traversal of...

    For the code below complete the preOrder() method so that it performs a preOrder traversal of the tree. For each node it traverses, it should call sb.append() to add a "[" the results of traversing the subtree rooted at that node, and then a "]". You should add a space before the results of the left and right child traversals, but only if the node exists. code: package edu.buffalo.cse116; import java.util.AbstractSet; import java.util.Iterator; public class BinarySearchTree<E extends Comparable<E>> extends AbstractSet<E>...

  • 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...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general...

    Does not pass the testcase testDTreeAdvancedReRootchangesParent Java I'm trying to extend the implementation of a general tree with new operations that change the structure of the tree. I've created 5 classes: Node.java, SimpleNode.java, Tree.java, RerootableTree.java and SimpleTree.java(which is the main java class that I need to change). The code does not pass ONE TESTCASE : testDTreeAdvancedReRootchangesParent The code passes all the other testcases except theone mentioned above. This is because in the SimpleTree.java the method "reRoot(Node newRoot)" is most likely...

  • 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; }...

  • 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...

  • 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...

  • I have almost done with this code to Implement Huffman Coding. However I have an error...

    I have almost done with this code to Implement Huffman Coding. However I have an error at the "Heap<Tree>". Could anyone help with solving this issue, really appreciated. Thank you!! import java.util.Scanner; public class HuffmanEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine();    int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s\n", "ASCII Code", "Character", "Frequency", "Code");    Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[]...

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