I have to write a method that returns a set of all elements in a bag with no duplicate entries allowed. The data is held in a BST, and I'm trying to do this by using a TreeSet, but I'm having trouble working out how to traverse the tree and add each node's data to the TreeSet. I can only manage the following:
Set<E> uniqueSet() {
Node current = root;
TreeSet t1 = new TreeSet();
t1.add(root);
for(Node current = root.getFirstChild(); node!=null; current = current.getNextSibling()) {
t1.add(current);
}
and assuming I have getNextSibling() and getFirstChild() methods, have I got the right sort of idea?