Unknown Data Structure?

250 Views Asked by At

I had a question about whether or not Java has its own data structure for what I am looking for. It is something like a combination of an array, linked list and a tree.

If it is not in Java, but exists already as a concept in computer science/other languages, that is also an acceptable answer so I can research it more and find out how to implement it myself.

Here is a picture to better illustrate what I am looking for. Excuse the lack of professionalism; I made it as best as I could:

enter image description here

I am looking for something that starts with several indexed starting elements, that eventually link to other elements and end in a convergence of sorts (one final element). In the end, each index has its corresponding starting element, which is linked all the way to the final converged element.

It should be the case that asking for unknownStructure[i] or something should grab an object that is a representation of the ith starting element linked all the way to the final converged element. (This thing to be grabbed is outlined in various bright colors in the picture).

2

There are 2 best solutions below

2
On BEST ANSWER

There is no "name" for this that I know of, but an array of linked list nodes would work quite well for this.

Traditionally linked lists are separate and simply a row of items pointing to the next. However, there is no reason why certain linked list nodes cannot point to the same child node. After all, trees and linked lists are essentially created the same way in Java.

The only foreseeable problem would be if you want to traverse this "tree" back to the starting node in the array. (Which could still be achieved with multiple parent support.)

To implement your linked-list array, simply created a Node class as for a linked list and then created an array of these elements:

Node[] myTreeArray = new Node[];

Then simply fill this array with your "base" nodes and link them to their appropriate children (eventually leading the the "end" node, which has a child of null)

5
On

It seems to me that you are looking for a directed Graph data structure.
You may need to use a list of graphs if needed.

See this page for algorithms and this for implementation.