3

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Schytheron
  • 715
  • 8
  • 28
  • The `tree` object could have had the static type of `HuffmanTree`, but that can't have been it's dynamic type (which is what `instanceof` uses), since `HuffmanTree` is `abstract`. – Jorn Vernee Feb 14 '17 at 23:46
  • Pay attention carefully to this pattern. This is the immense power of a recursive definition blowing your mind. Every node in a binary tree is itself a binary tree because it has two children. A leaf node is just a tree with no children (yet). Thus every node in a tree is both a node (it holds a value) and a tree at the same time. A very large chunk of software development (and even Mathematics) rests on this foundation. – Jim Garrison Feb 15 '17 at 00:15
  • However, `instanceof` is a code smell, all too frequently used in lieu of a proper type taxonomy. – Lew Bloch Feb 15 '17 at 01:00

1 Answers1

3

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)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Interesting... however I do not understand how abstract classes work or why they are used (should probably read up on that). Would the statement have returned true if HuffmanTree class wasn't abstract? Also found the code at the bottom of your post to be interesting. How come that you can declare it as "new HuffmanNode()" if the previous statement (before "tree") says that it should be/be of the type HuffmanTree? – Schytheron Feb 15 '17 at 19:37
  • Yes, please read up on `abstract`. There are enough resources to help you with that. Abstract does not control the return value of `instanceof`, then `extends` keyword does. My point was that you cannot `new` any `abstract class`, and therefore it **must be** `new HuffmanNode()` in the code that you failed to show in your question. Hopefully these help http://stackoverflow.com/a/9552547/2308683 and http://stackoverflow.com/questions/11007459/why-assign-a-new-arraylist-to-a-list-variable – OneCricketeer Feb 15 '17 at 20:24