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.
Related
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
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.
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.
I have to represent tree data having number of levels in android app. As per native API, using list view we can provide only two levels. It is also reasonable because using fat fingers we can't expand or shrink tree properly.
So is there any alternative way to represent this tree like data?
Can you not 'drill-down' by clicking on the second level. The second level then becomes the first level (visually) and you then add in the third level as the second level (visually). You will need a 'back' button of some sort to move back up.
Or do you need to show the whole tree at once? What is in your tree? Just labels/names, or multiple data values per node?
Also, a long shot - but for ideas (if you like writing your own components):
http://www.cs.umd.edu/hcil/treemap-history/
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.