package binaryTree.visitor;
import binaryTree.*;
/**
* Replaces the root of the input tree with the maximum of the host, or the minimum of the
* input's right subtree, if any such values exists, otherwise removes the input's root.
*/
public class LeftDeleter implements IVisitor
{
public final static LeftDeleter Singleton = new LeftDeleter ();
private LeftDeleter()
{
}
/**
* Asks the input's right subtree to delete the input's root.
* @param host the left subtree of input.
* @param input a BiTree with the binary search property.
* @return null.
*/
public Object emptyCase(BiTree host, Object input)
{
return ((BiTree)input).getRightSubTree().execute(RightDelDontAsk.Singleton, input);
}
/**
* Asks the input to set its root to the maximum value in the host, and removes
* this maximum value from the host.
* @param host the left subtree of input.
* @param input a BiTree with the binary search property.
* @return null.
*/
public Object nonEmptyCase(BiTree host, Object input)
{
BiTree parent = (BiTree)input;
BiTree maxTree = (BiTree)host.execute (MaxTreeFinder.Singleton, null);
Object max = maxTree.getRootDat();
parent.setRootDat (max);
maxTree.execute (OOBSTDeleter.Singleton, max);
return null;
}
}