rohan
rohan

Reputation: 557

Java - Polymorphism and object class design

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?

Upvotes: 1

Views: 116

Answers (2)

rohan
rohan

Reputation: 557

Here is what I ended up with from the above suggestions. Node class:

public abstract class Node<T> {
    protected T value;
    protected Node[] children;
}

BinaryTreeNode

public class BinaryTreeNode<T> extends Node {
    public BinaryTreeNode() {
        children = new BinaryTreeNode[2];
    }

    public BinaryTreeNode(T value) {
        this.value = value;
        children = new BinaryTreeNode[2];
    }

    public void setValue(T value) {
        this.value = value;
    }

    public T getValue() {
        return (T)value;
    }

    public BinaryTreeNode left() {
        return (BinaryTreeNode)children[0];
    }

    public BinaryTreeNode right() {
        return (BinaryTreeNode) children[1];
    }

    public void setLeft(BinaryTreeNode<T> leftNode) {
        children[0] = leftNode;
    }

    public void setRight(BinaryTreeNode<T> rightNode) {
        children[1] = rightNode;
    }
}

Tree Class:

public abstract class Tree<T> {
    public Node<T> root;
}

BinaryTree:

public class BinaryTree<T> extends Tree<T> {

    public BinaryTree() {
        root = new BinaryTreeNode<>();
    }

    public void visit(Node node) {
        System.out.println(node.value);
    }

    public void inOrderTraversal(BinaryTreeNode node) {
        if(node != null) {
            inOrderTraversal(node.left());
            visit(node);
            inOrderTraversal(node.right());
        }
    }
}

I apologize for lengthy comment. Thankful for any feedback on improving design.

Upvotes: 0

Uri Loya
Uri Loya

Reputation: 1361

What about:

public class Node<T> {
public T name;
public Node[] children;
public Node(int numOfChildren) { children = new Node[numOfChildren]; }
}

If you would like the node to hold 2 nodes call the constructor with 2, That way children will hold up to 2 nodes. Also it would be better to use encapsulation and make children private and provide accessibility with getters and setters:

public class Node<T> {
public T name;
private Node[] children;

public Node(int numOfChildren) { children = new Node[numOfChildren]; }

public Node[] getChildren() { return children; }
}

Upvotes: 2

Related Questions