Calculating the average of all nodes in a Binary Search Tree using visitors I'm required to calculate the average value of all nodes in a BST using a Visitor, I'm not sure if I did this correctly or if i could just calculate the average with inOrder() and therefore I'm not really utilizing the visitor.

import java.util.*;

public class Node{

    int val;
    Node left;
    Node right;
    private static int sum;
    private static int count;
    private static double average;

    public Node(int val){
        this.val = val;
    }

    public int getVal(){
        return val;
    }

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

    static class NodeVisitorImpl implements Visitor{
        @Override
        public int visit(Node node) {
            return node.getVal();
        }
    }


    public void insert(int toInsert) {
        if (toInsert &lt; this.val) {
            if (this.left == null) {
                this.left = new Node(toInsert);
            } else {
                this.left.insert(toInsert);
            }
        } else if (toInsert &gt; this.val) {
            if (this.right == null) {
                this.right = new Node(toInsert);
            } else {
                this.right.insert(toInsert);
            }
        } else {
            System.out.println("Shouldn't have the same value");
        }

    }

    public static void inOrder(Node root){
        if (root == null) {
            return;
        }
        Visitor visitor = new NodeVisitorImpl();
        sum += visitor.visit(root);
        count++;
        average = sum/count;
        inOrder(root.left);
        inOrder(root.right);
    }

    public static void main(String[] args){
        Node root = new Node(4);
        root.insert(2);
        root.insert(1);
        root.insert(3);
        root.insert(6);
        root.insert(5);
        root.insert(7);

        inOrder(root);
        System.out.println(average);
    }

}
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().