This code which defines a complete Set implementation using

This code which defines a complete Set implementation using a binary search tree. Unfortunately, its Iterator does not throw any exceptions when its results may not be correct. Fix this by updating the class so the Iterator is a fail-fast Iterator.

Code:

Solution

public boolean add(E element) { if (root == null) { if (element == null) { throw new NullPointerException(); } root = new Entry<>(element, null); size++ ; return true; } else { Entry temp = root; int comp; while (true) { comp = element.compareTo(temp.element); if (comp == 0) { return false; } if (comp < 0) { if (temp.left != null) { temp = temp.left; } else { temp.left = new Entry<>(element, temp); size++ ; return true; } // temp.left == null } else if (temp.right != null) { temp = temp.right; } else { temp.right = new Entry<>(element, temp); size++ ; return true; } // temp.right == null } // while } // root not null } // method add
This code which defines a complete Set implementation using a binary search tree. Unfortunately, its Iterator does not throw any exceptions when its results may

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site