Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Is Tree an instance of Node?

I was browsing the web in the hope of finding something that will help me build a HuffmanTree and I stumbled across this code from http://rosettacode.org/wiki/Huffman_coding#Java.

I am new to Java and wasn't able to use this as it is way above my level, but I was still intrigued by it (because it is such short and seemingly effective code) and I tried to read through it hoping to at least understand most of it (note: I failed).
But there was one piece of code that caught my attention: the "instanceof" method.

There are 3 classes in my code.
One superclass (HuffmanTree) and two subclasses (HuffmanNode and HuffmanLeaf) and look like this:

abstract class HuffmanTree implements Comparable<HuffmanTree> {
    public final int frequency; // the frequency of this tree
    public HuffmanTree(int freq) { frequency = freq; }

    // compares on the frequency
    public int compareTo(HuffmanTree tree) {
        return frequency - tree.frequency;
    }
}

class HuffmanLeaf extends HuffmanTree {
    public final char value; // the character this leaf represents

    public HuffmanLeaf(int freq, char val) {
        super(freq);
        value = val;
    }
}

class HuffmanNode extends HuffmanTree {
    public final HuffmanTree left, right; // subtrees

    public HuffmanNode(HuffmanTree l, HuffmanTree r) {
        super(l.frequency + r.frequency);
        left = l;
        right = r;
    }
}

I read a little bit about "instanceof" and as far as I understand it, it tells you if the object is the instance of a certain class (returning a boolean true or false).
And logically a parent can't be the instance of a child.

However, when you write (tree instanceof HuffmanNode), it returns true (tree is a object of the class HuffmanTree) and if I write (tree instanceof HuffmanLeaf) it returns false (logically).
But why does (tree instanceof HuffmanNode) return true when tree is a parent of HuffmanNode?

like image 503
Schytheron Avatar asked May 27 '26 04:05

Schytheron


1 Answers

Logically, a Tree is simply a Node with two Tree references as children.

why does (tree instanceof HuffmanNode) return true when tree is a parent of HuffmanNode? Why?!

tree must be a HuffmanNode (as you saw), and yes, any HuffmanNode is a parent of two other HuffmanTree instances (which themselves can be nodes or leaves).

But a HuffmanNode is a HuffmanTree

class HuffmanNode extends HuffmanTree { // <-----
    public final HuffmanTree left, right; // subtrees

Which explains...

when you write (tree instanceof HuffmanNode)

However, not sure about this...

tree is a object of the class HuffmanTree

Because HuffmanTree is abstract, which means it must have been declared the following way since you cannot new an abstract instance

HuffmanTree tree = new HuffmanNode(...); 
                    ^^^^ This is the object's type

(Personally, I would have the leaf class be a node with two null subtrees)

like image 151
OneCricketeer Avatar answered May 28 '26 16:05

OneCricketeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!