package trees;


public class BinaryNode<K extends Comparable<K>, V> implements INode<K, V> {
	private K m_key;
	private V m_value;
	private INode<K, V> m_left;
	private INode<K, V> m_right;
	
	
	public BinaryNode(K m_key, V m_value, INode<K, V> m_left,
			INode<K, V> m_right) {
		this.m_key = m_key;
		this.m_value = m_value;
		this.m_left = m_left;
		this.m_right = m_right;
	}

	public BinaryNode(K m_key, V m_value) {
		this.m_key = m_key;
		this.m_value = m_value;		
		this.m_left = new Leaf<K, V>();
		this.m_right = new Leaf<K, V>();

	}

	public V search(K key) {
		int cmp = key.compareTo(m_key);
		if(cmp == 0) return m_value;
		if(cmp < 0) return m_left.search(key);
		else return m_right.search(key);
	}

	public INode<K, V> insert(K key, V value) {
		int cmp = key.compareTo(m_key);
		if(cmp == 0) return this;
		if(cmp < 0) m_left = m_left.insert(key, value);
		else m_right = m_right.insert(key,  value);
		
		return this;
	}
	
	public String toString()
	{
		return "Binary[" + m_key + " -> " + m_value + "](" + m_left + ", " + m_right + ")";
	}

}
