Question: For the Binary Search Tree, build the following functionalities within the SimpleSet interface provided Using JAVA and Recusrion: size returns the size of the set
For the Binary Search Tree, build the following functionalities within the SimpleSet interface provided Using JAVA and Recusrion:
size returns the size of the set
isEmpty return true if the set is empty
add Add an item to the set (returns true if added)
remove removes an item from the set (returns true if removed)
clear clears all values from the set
contains returns true if the provided value is in the set
toArray returns an array containing all items on the list least to greatest
height computes the height of the tree
printStructure a helper method that prints each level of the tree
File:SimpleTreeSet.java
package trees;
@SuppressWarnings("rawtypes") public class SimpleTreeSet implements SimpleSet { private int nodeCount = 0; class Node { Comparablevalue; Node left; Node right; int size; Node(Comparablevalue) {this.value = value; size=1; right = null; left = null; }
} private Node root; public int size() { return nodeCount; } @Override public boolean isEmpty() { return size() == 0; }
@Override public void clear() { }
@Override public boolean contains(Comparable o) { return false; }
@Override public Object[] toArray() { Object[] returnMe = new Object[size()]; fillArray(returnMe, root, 0); return returnMe; } private int fillArray(Object[] fillMe, Node subtree, int currentIndex) { if(subtree==null) { return currentIndex; } int nextIndex = fillArray(fillMe, subtree.left, currentIndex); fillMe[nextIndex] = subtree.value; return fillArray(fillMe, subtree.right, nextIndex+1); }
@Override public boolean add(Comparable e) { return false; }
@Override public boolean remove(Comparable o) { return false; } @Override public int height() { return 0; } private int height(Node node) { return 0; }
@Override public void printStructure() { root = new Node((Comparable)"5"); root.left = new Node((Comparable)"15"); root.right = new Node((Comparable)"25"); root.left.left = new Node((Comparable)"20"); root.left.right = new Node((Comparable)"30"); root.right.left = new Node((Comparable)"2"); root.right.right = new Node((Comparable)"10"); } }
File SimpleSet.java
package trees;
/** * @author */ @SuppressWarnings("rawtypes") public interface SimpleSet { public int size(); public boolean isEmpty(); public boolean contains(Comparable o); public Object[] toArray(); public boolean add(Comparable e); public boolean remove(Comparable o); public void clear(); public int height();
public void printStructure(); }
Please Comment what extra information you need and i will gladly provide
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
