Question: Need help with java code simple tree set, The problem to solve: Tree Sets give us a tiny (but very helpful) simplified tree where the

Need help with java code simple tree set,

The problem to solve:

Tree Sets give us a tiny (but very helpful) simplified tree where the key and value are always the same; thus, we can omit the value for our assignment. For the Binary Search Tree, build the following functionalities within the SimpleSet interface provided:

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

My code is failing Says length missmatch please assist.

package trees;

/** * * */ @SuppressWarnings("rawtypes") public class SimpleTreeSet implements SimpleSet { class Node { Comparable value; private Node left; private Node right; Node(Comparable value) {this.value = value; } } private Node root; int n = 0 ; int size; public int size() { return size; } @Override public boolean isEmpty() { if(root == null) { return true; } return false; }

@Override public void clear() { root = null; size = 0; }

@Override public boolean contains(Comparable o) { if(o == null) throw new IllegalArgumentException("argument to get is null"); if(root.value == o) { return true;} 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) {

if (root == null) { root = new Node(e); size = size +1; return true; } else if(root.value.compareTo(e) < 0) { //add to the left root.left = add(root.left, e); size = size + 1; return true; } else if (root.value.compareTo(e) > 0) { //add to right root.right = add(root.right, e); size = size + 1; return true; } return false; } private Node add(Node subtree, Comparable e) { return subtree; }

@Override public boolean remove(Comparable o) { if( root == o) { size = size-1; root.value = null; return true;} return false; } @Override public int height() { return size -1; }

@Override public void printStructure() { int h = height() ; for(int i = 1; i <= h; i++) { printGivenLevel(root, i); System.out.println(); } } private void printGivenLevel(Node subtree, int level) { if(subtree == null) { return; } if(level == 1) { System.out.print(subtree.value + " "); } else { printGivenLevel(subtree.left, level-1); printGivenLevel(subtree.right, level-1); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!