0

I have this class

class Node {
    Node parent;
    int rank;

    public Node() {
        this.parent = this;
        this.rank = 0;
    }
}

I would like to create subclasses of Node, but I want parent to be the same type as it's corresponding type.

How can I do this?

Ultimately, I am trying to create a special UnionFind data structure. I implemented the find and merge functions for the data structures. However, I want to extend this and add more functionality. I would like to add the member V value to Node and record the minimum value of each set. For me, the best way to do this is to create a subclass of Node. Would there be a better way to this?

mtber75
  • 938
  • 1
  • 8
  • 15
  • 3
    This has been answered plenty of times. Literally answered this a few days ago. Use generics for a quick-fix: `Node>` – Vince May 02 '18 at 22:54
  • Corollary: [Don't use raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – azurefrog May 02 '18 at 22:55

0 Answers0