Resolve dependency among custom objects - java

I have a List<Object> and each Object has integer 'id' and integer 'depends' parameter. This list then goes for some processing such that the objects which are not dependent on anyone will do the processing first and then next group of objects go ahead.
I have implemented this using topology sort along with adjacency list which does the job beautifully, but I am being told to use tree (TreeModel) as the previous approach is difficult to understand. The independent objects will be at root level, the object depending on it will be its children and so on. So, all objects at root level will go first, then object at level 2 and then 3...
I am a little confused as to how I would proceed with the implementation. I am thinking I would start with List<TreeModel<Object>>, so each index represents a tree and then form the trees accordingly. And while parsing, I will parse all the root level first, then the 2nd level and then 3rd.. Though I think there is an elegant way.
Any hint/ help would be appreciated. Thanks.

Related

Which Java node-based data structure to use?

I have to create a star shape (like a star topology in networking) with nodes in java for homework and I'm not sure which data structure to use. There should be a central node with periheral nodes pointing to it that are not linked directly to each other but are all linked through the central node. I am to achieve this using three classes apart from the class with the main method. I'm thinking a linked list or a stack or queue would not accomplish this since they all have a particular order which I don't know how to manipulate (but they're all I've been taught about thus far). Any suggestions or links or comments on where I could begin would be very appreciated!
I think in your case you need just to use Graph data structure, or to be more specific, if you are sure that you just need one node to be connected to all other nodes, you can use Tree.
Finally I think you need to look how to implement those data structures in JAVA, so look at this:
https://www.geeksforgeeks.org/graph-and-its-representations/
https://www.javatpoint.com/tree

Unknown Data Structure?

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:
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).
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.
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)

Implementing a traversal with dynamic depth

Is it possible to create a traversal in java using neo4j that keeps a state for the duration of the traversal?
For example, I need an Evaluator that is almost identical to toDepth(), except that the depth at the current node is based on another comparison. Say that you had a linked list with 20 items, and you wanted the 10th [valid] one, meaning that some of the items had a particular property flag excluding them from the count. So the final returned item might actually be the 12th in the Path.
The only efficient way I can think of doing this is being able to store some state variable that is accessible to each individual evaluation. Is that possible?
I understand that I could write my own custom traversing functions to do this, but it would be nice if I could build it into the Traversal Framework.

To ArrayList once, or to ArrayList each time?

I have a program which has a structure like this.
Document which contains (up to 20)
Chapters which contain (up to 100)
Pages which contain (up to 20)
Elements
This structure is represented by JPanels in my program. Meaning this structure has to be visually represented, and I'd rather not make a whole complex of ArrayList (unless absolutely neccessary) since each JPanels have a ZOrder of component, and a getParent() method.
This structure is one-dimenisonal, meaning that the parent has an one-dimensional array (and when I say array, it's purely descriptive, I don't mean ArrayList or anything similar) of its children. Each individual element has an index which represents it's location in(on?) it's parent. Number of elements in a page, and pages in a chapter is inconsistent.
It's easy to get the child's index within it's parent, but what about it's grandparents?
Since the elements can be (and usually are) numbered, having a one numbered list per chapter, I'd have to know the index of element in the Chapter, so I can adjust the numbers when a new element is added to the list (it doesn't have to be added in the end).
This can be solved in two ways (that I know of, that is):
Have an ArrayList in each chapter that keeps all the elements. This would require me that, everytime I add a new element to any page, to add it to the chapter array too.
To accomplish that I'd have to go trough all the previous pages, add up all the elements on them and add index of the new element on the present page to that number, the result being the index of the new element in the chapter, and therfore, in the array. And do that each time I add a new element.
Recreate the arrayList each time I need to get the order of elements in the chapter. Which again means going trought each page and adding each element one after another until I reach the end of chapter. And I'd need it each time a new element is added.
So the question is, which of this two methods is better (more efficient memory or processor time wise)? Which is more in the spirit of Java and programming altogether? Is there a third option that I am unaware of??
Chapter example:
Page one {
1. something
2. more something
3. nothing
.
.
.
16. still nothing
}
Page two {
17. maybe something
18. nope, still nothing
.
.
.
21. giberish
}
etc.
The question is: Which way of doing it is better? If you have a better idea, you can tell me, but I want to know which way of the above two is better non the less.
You need to make a tree. For some reason, programmers want to flatten everything out into tabular structures. You are talking about a tree, you need to either use one or make one.
Sadly, there is nothing in the Java Collections for implementing Trees. You can make them fairly easily.
If you have things that are different contained in the tree, but that need to be treated similarly (as nodes), then do a simple implementation of the Composite Pattern. A good example is a filesystem tree: each node is either a Folder or a File. If you both have them implement an interface called FilesystemItem, then you can put them into their tree structure.
Since you are doing a Document, I would recommend Composite.

making a binary tree

Hi
I want to make a binary tree which is based -level,which means that nodes of first level will be create and then all the nodes of second level will be creat and then all the nodes of third level will be create ,...
for making a binary tree like above ,I used a list(data structure)
I want to know that JVM can remember the order of nodes or I must use list?
thanks
You would need to use a data structure that maintains the links between your elements. If you just had an array and created everything at level 1, then created a second array at level 2, the JVM won't remember for you the order that things were created.
In short, you're likely going to want to use a Linked List for something like that.

Categories

Resources