
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 int getLowest() {	
		BTNode ptr;
		BTNode lag = null;
		
		if (m_root.getLeft() != null) {
			ptr = m_root.getLeft();
			lag = m_root;
		}
		else {
			ptr = m_root;
		}
		
		while (ptr.getLeft() != null) {
			lag = ptr;
			ptr = ptr.getLeft();
		}
		
		// ptr is the lowest value
		// lag is parent, or NULL if ptr is the root 
		if (lag == null)
			return m_root.getData();
		else 
			return ptr.getData();
	}

	public void removeLowest() {
		m_count--;
		BTNode ptr;
		BTNode lag = null;
		
		if (m_root.getLeft() != null) {
			ptr = m_root.getLeft();
			lag = m_root;
		}
		else {
			ptr = m_root;
		}
		
		while (ptr.getLeft() != null) {
			lag = ptr;
			ptr = ptr.getLeft();
		}
		
		// ptr is the lowest value
		// lag is parent, or NULL if ptr is the root 
		if (lag == null)
			m_root = ptr.getRight();
		else {
			if (ptr.getRight() == null) {
				ptr = lag;
				lag.setLeft(null);
			}
			else {
				lag.setLeft(ptr.getRight());
				ptr = lag;
			}
		}
	}
	
	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);
	}

}

