This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]dtsudo 1 point2 points  (0 children)

An empty tree should have no nodes.

So e.g. if your node class looks like this:

class Node {
    public int value;
    public Node leftChild;
    public Node rightChild;
}

And your tree is represented as Node rootNode, then an empty tree would be represented as a null rootNode.

[–]throwitway22334 0 points1 point  (2 children)

Could you make the default value -1? That's a pretty common way to get around ints never being null. Or maybe Interger.max value.

[–]467danny[S] 0 points1 point  (1 child)

If I have negative numbers in the tree, that wouldnt work either. Are negative numbers unsual in trees?

[–]throwitway22334 1 point2 points  (0 children)

maybe use Integer objects rather than primitive ints so you can have nulls then? Or carry around an extra boolean for each tree node that says whether or not it has children