// Consider binary trees with integer values at the leaves, 
// and problems of 
//  (i) summing these values,
//  (ii) counting all nodes+leaves.
//
// Procedural ("case-based") approach
//
abstract class Tree {};

class Node extends Tree {
    Tree left, right;
    Node (Tree left, Tree right) {
	this.left = left; this.right = right;
    }
}

class Leaf extends Tree {
    int value;
    Leaf (int value) {
	this.value = value;
    }
}

class TreeOps {
    static int sum(Tree t) {
	if (t instanceof Node) 
	    return sum(((Node) t).left) + sum(((Node) t).right);
	else  // t instanceof Leaf
	    return ((Leaf) t).value;
    }
    static int count(Tree t) {
	if (t instanceof Node) 
	    return count(((Node) t).left) + count(((Node) t).right) + 1;
	else  // t instanceof Leaf
	    return 1;
    }
}

class TestTree {
    public static void main(String argv[]) {
	Tree t = new Node (new Leaf(1), new Node(new Leaf(2), new Leaf(3)));
	int sum = TreeOps.sum(t);
	int count = TreeOps.count(t);
	System.out.println("Sum = " + sum);
	System.out.println("Count = " + count);
    }
}
