I'm trying to practice generics and polymorphism. I have a Node class as follows:
public class Node<T> {
public T name;
public Node[] children;
}
Then a generic Tree class:
public class Tree<T> {
public Node<T> root;
}
Now, I'm trying to implement a binary tree. A binary tree is a tree, thus satisfying is a rule of polymorphism. I'm struggling with how to enforce that nodes in binary trees can have a maximum of two children.
Should I extend node class to Binary node and children array is always initialized to size 2? If BinaryTree extends Tree, how do I place the restriction on the children member variable?
It's like I am trying to make:
BinryTree extends Tree
BinaryTreeNode extends Node
Where Node is a member variable in Tree and BinaryTreeNode is a member variable in BinaryTree. What would be a proper design?
Nodeshouldn't have children at all.