
public class SetImpl 
implements SimpleSet {

	private BTNode m_root;
	private int m_count;
	
	public SetImpl() {
		m_root = null;
	}

	public void add(int element) {
		if (m_root == null) 
			m_root = new BTNodeImpl(element);
		else
			m_root.addInOrder(element);
		m_count++;
	}

	public void removeAny() {
		m_count--;
		BTNode ptr;
		BTNode lag;
		
		if (m_root.getLeft() != null) 
			ptr = m_root.getLeft();
		else
			ptr = m_root.getRight();
		
		lag = null;
		while ((ptr.getLeft() != null) || (ptr.getRight() != null)) {
			// ptr has a left OR right child
			lag = ptr;
			if (ptr.getLeft() != null)
				ptr = ptr.getLeft();
			else
				ptr = ptr.getRight();
		}
		
		// ptr has no left AND no right
		// lag is parent OR null
		if (lag == null)
			m_root = null;
		else {
			if (lag.getLeft() != null)
				lag.setLeft(ptr);
			else
				lag.setRight(ptr);
		}
	}

	public boolean isIn(int element) {
		if (m_root == null)  
			return (false);
		return (m_root.containedIn(element));
	}

	public void clear() {
		m_root = null;
		m_count = 0;
	}

	public int getSize() {
		return (m_count);
	}
}
