Calculating the average of all nodes in a Binary Search Tree using visitors
You are doing it somewhat right, somewhat wrong.

Your visitor is more of a Mapping transformer: it is converting Node into an int.  How it gets that int value from the Node is left up to the implementation.

Unfortunately, all of your average calculation is still inside inOrder().  Worse, your sum, count and average values are static members of Node, so you cannot do two parallel average calculations.  There is no resetting of the sum or count so a subsequent average calculation is going to compute the average of the aggregate of all data, not just the most recent tree.

Your Visitor visit() method should return nothing:

public interface Visitor {
    void visit(Node node);
}


Then, you can create an AveragingVisitor which averages the data it extracts from Node objects:

static class AveragingVisitor implements Visitor {
    int sum = 0;
    int count = 0;

    @Override
    public void visit(Node node) {
        sum += node.getVal();
        count++;
    }

    public double average() {
        return (double) sum / count;
    }
}


Each time you create a new AveragingVisitor, it will start new count &amp; running total at zero.

To compute the average, you would create this averaging visitor, and perform some kind of traversal (inOrder is fine) of the tree:

AveragingVisitor visitor = new AveragingVisitor();

root.inOrder(visitor);

System.out.println(visitor.average());


Of course, Node::inOrder(Visitor vistor) will need to be rewritten as non-static, and call the visitor.visit(node) method on each node:

public void inOrder(Visitor visitor) {
    if (left != null)
        left.inOrder(visitor);
    visitor.visit(this);
    if (right != null)
        right.inOrder(visitor);
}


With this visitor, you could easily make a PrintingVisitor to print all of the nodes.  No change would need to be made to inOrder().