Java, Binary Search Tree, Union of two trees methos

Java, Binary Search Tree, Union of two trees methos

 Return the union of this OrderedSet and the other OrderedSet as a new OrderedSet. Do not modify this OrderedSet or the other OrderedSet.

eg) The union of {2, 4, 6} and {2, 5, 9} is {2, 4, 5, 6, 9}

OrderedSet is the class I’m working on, it has a TreeNode inner class.

The answer I have is:

public OrderedSet<E> union(OrderedSet<E> other)

{

OrderedSet<E> result = new OrderedSet<E>();

union(result, other,root);

return result;

}

private void union(OrderedSet<E> result, OrderedSet<E> other, TreeNode t)

{

if (t == null)

return;

if (!other.contains(t.data))

result.insert(t.data);

intersection(result, other, t.left);

intersection(result, other, t.right);          

}

but it only gives me the intersection between the two OrderedSet

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper