Object oriented design for a Tree I am implementing a Tree in Java. It must be easy to add a new type of tree e.g. Binary tree and Tertiary tree. Each node can have at least one value. It must be easy to make a new type of node and make a tree of that node.

interface Node&lt;T&gt; {

    T getVal();

    boolean equals(Node&lt;T&gt; node);

    String toString();
}


interface Tree&lt;T extends Node&gt; {
    boolean addNode(T node);

    boolean removeNode(T node);

    String toString();
}


public class BinaryTreeNode&lt;T&gt; implements Node&lt;T&gt; {

    T value;
    BinaryTreeNode&lt;T&gt; firstChild, secondChild;

    BinaryTreeNode(T value) {
        this.value = value;
        firstChild = null;
        secondChild = null;
    }

    public T getVal() {
        return value;
    }

    public boolean equals(Node&lt;T&gt; node) {
        return this.value.equals(node.getVal());
    }

    public String toString() {
        return "" + value;
    }

    boolean setFirstChild(BinaryTreeNode&lt;T&gt; node) {
        firstChild = node;
        return true;
    }

    BinaryTreeNode&lt;T&gt; getFirstChild() {
        return firstChild;
    }

    boolean setSecondChild(BinaryTreeNode&lt;T&gt; node) {
        secondChild = node;
        return true;
    }

    BinaryTreeNode&lt;T&gt; getSecondChild() {
        return secondChild;
    }
}


import java.util.*;

public class BinaryTree&lt;T extends BinaryTreeNode&gt; implements Tree&lt;T&gt; {

BinaryTreeNode root;

    BinaryTree() {
        root = null;
    }

    public boolean addNode(T node) {

        if(root == null) {
            root = node;
        } else {
            addToLast(root, node);
        }

        return true;
    }

    boolean addToLast(BinaryTreeNode root, T node) {

        Deque&lt;BinaryTreeNode&gt; queue = new LinkedList&lt;&gt;();

        queue.addLast(root);
        while(!queue.isEmpty()) {
            BinaryTreeNode cur = queue.pollFirst();

            if(cur.getFirstChild() == null) {
                cur.setFirstChild(node);
                break;
            } else if(cur.getSecondChild() == null) {
                cur.setSecondChild(node);
                break;
            } else {
                queue.addLast(cur.getFirstChild());
                queue.addLast(cur.getSecondChild());
            }
        }

        return true;
    }

    public boolean removeNode(BinaryTreeNode node) {

        throw new UnsupportedOperationException();
    }

    public String toString() {

        Deque&lt;BinaryTreeNode&gt; queue = new LinkedList&lt;&gt;();

        if(root == null) {
            System.out.println("root is null");
            return "";
        }

        StringBuffer ans = new StringBuffer("");
        queue.addLast(root);
        while(!queue.isEmpty()) {
            BinaryTreeNode cur = queue.pollFirst();
            ans.append(" " + cur);

            if(cur.getFirstChild() != null)
                queue.addLast(cur.getFirstChild());

            if(cur.getSecondChild() != null)
                queue.addLast(cur.getSecondChild());

        }

        return ans.toString();
    }

    public static void main(String args[]) {

        BinaryTree&lt;BinaryTreeNode&gt; btree = new BinaryTree&lt;&gt;();

        BinaryTreeNode&lt;Integer&gt; node1 = new BinaryTreeNode&lt;&gt;(1);
        btree.addNode(node1);

        System.out.println(btree);
    }
}


Following this design pattern, I think it is easier to implement different types of binary trees like binary search tree, heap, red-black tree, etc. Also, one can easily add more information to a node while implementing own implementation of tree.

Is this a good design?
Short answer: no.  

But you'll probably need some explanation to believe me.



The whole Node thing you got there should be internal information to the Tree class, not part of the type. Let's look at the following example method to show why it's a bad idea:  

private static void addARandomNumber(Tree&lt;Node&lt;Number&gt;&gt; tree) {
    int randomNumber = generateRandomNumber();
    Node&lt;Number&gt; = new ... ???
    tree.addNode(node);
}


What class should we use to create a new Node here?

Had the Node been completely internal to the Tree implementation then the interface would look like this:

interface Tree&lt;T&gt; {
    boolean add(T node);

    boolean remove(T node);

    String toString();
}


And now we can also write the add random number method easily:

private static void addARandomNumber(Tree&lt;Number&gt; tree) {
    int randomNumber = generateRandomNumber();
    tree.add(randomNumber);
}




I also got a feeling that forcing some ordering on T, as required in most search trees, will be rather hard in your current design. But that might just be because I didn't really try it myself yet.

Defining the type of the search tree correctly would probably work, but I expect some type erasure issues with the addNode and removeNode methods.