package brstruct.visitors;

import brstruct.BRStruct;
import brstruct.BRStruct.IAlgo;

/**
 * TODO: Define a visitor for a BRStruct<Integer> which returns, as an Integer,
 * the sum of all the Integers contained in the BRStruct.
 *
 * @author <a href="mailto:alphonce@cse.buffalo.edu">Carl G. Alphonce</a>
 *
 * Created on: Apr 23, 2007
 *
 */
public class QuizVisitor2 implements IAlgo<Object, Integer, Integer> {

	public Integer emptyCase(BRStruct<Integer> host, Object _) {
		// identity element of addition operation
		return 0;
	}

	public Integer nonEmptyCase(BRStruct<Integer> host, Object _) {
		// answer is datum
		//   plus the sum of the Integers in the left subtree (calculated recursively)
		//   plus the sum of the Integers in the right subtree (calculated recursively)
		return host.getDatum() + host.getLeft().execute(this,_) + host.getRight().execute(this,_);
	}

}
