I have a list of T objects, it has a parent property, where the parent property of top objects is null. I want to put all the objects in a TreeSet (or TreeMap). Top-level objects will be all root objects that do not have a parent (the parent object is null), and they will have their children under them.
Something like that
o / | \ Ra Rb Rc -- Level Root Objects / | \ | \ Ca1 Ca2 Cb1 Cc1 Cc2 -- Level of First Children / \ Ca11 Ca12.............. -- Level of Second Children
So I can get Ra and find his children (Ca1, Ca2, Ca11, Ca12 ....)
Update: Sorry, it may have been unclear, the nodes point to the parents, and if the parent is null, they are the root nodes. The problem is that parents need to know their children. But relations are in the opposite direction.
class Node { private Node parent; private String name; }
source share