package binary_tree;

public class LinkedBinaryTree<E> implements BinaryTree<E>{
	
	// VARIABILI D'INSTANZA
	private BinaryNode<E> root;
	private int size;
	
	// Metodi costruttori
	public LinkedBinaryTree() {
		root = null;
		size = 0;
	}
	
	public LinkedBinaryTree(E data) {
		root = new BinaryNode<E>(data);
		size = 1;
	}
	
	public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
		root = new BinaryNode<E>(left.root, data, right.root);
		size = 1 + left.size + right.size;
	}
	
	// METODI
	
	public int size() {
		return size;
	}
	
	@Override
	public boolean isEmpty() {
		return root == null;
	}
	
	public E getRoot() {
		if (isEmpty()) return null;
		return root.getData();
	}
	
	public BinaryNode<E> getRootNode() {
		if (isEmpty()) return null;
		return root;
	}
	
	public void clear() {
		root = null;
		size = 0;
	}
	
	private int getSize(BinaryNode<E> node) {
		if (node == null) return 0;
		
		int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
		int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
		
		return 1 + nLeft + nRight;
	}
	
	public LinkedBinaryTree<E> removeLeft() {
		LinkedBinaryTree<E> leftTree = null;
		if (root.getLeft() == null) return leftTree;
		
		leftTree.root = root.getLeft();
		leftTree.size = getSize(root.getLeft());
		return leftTree;
	}
	
}
