Home > Net >  Is there a way to implement a LinkedList with multiple child-nodes in Java?
Is there a way to implement a LinkedList with multiple child-nodes in Java?

Time:09-22

I am trying to implement a package-system which is commonly present in object-oriented programming language:

Image

But I am not really sure which datastructure I am supposed to use, I thought about a directed graph at first. Nethertheless I think that some kind of LinkedList, which allows the addition of multiple childnodes would be a better and easier solution. At least I guess so.... Can someone please tell me which datastructure may be the best solution for my problem?

CodePudding user response:

I would just use a nonbinary tree like this:

public class Pck{
    private List<Pck> children;
    private Pck parent;//optional
    private String name;//optional
    //Also include a variable for your data
}

Each package has a reference to each subpackage.

If you want to get the name of a specific package, you would also need to maintain references to the parent package (and the name of each package within its layer). You could iterate over the parent to the next parent etc. and build the name.

Similarily, you could get the concrete package by splitting the name and recursively finding the next child.

  • Related