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)
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
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 am fairly new to programming and am unfamiliar with certain terminology and references within Java. Although I believe I have effectively utilized google, I find that asking you guys to dumb it down for me will help me more efficiently.
My question is, what specifically are nodes? And what are they used for? Additionally, what are Stingy Linked Structures used for?
A linked structure is a data structure that consists of a bunch of smaller elements (called cells or nodes) that are linked together to form a larger structure. This is similar to how molecules are formed - you have a bunch of smaller atoms that are then connected together to form a molecule. Many important data structures, such as linked lists or binary search trees - are linked structures.
Linked structures are usually contrasted with array-based structures. Arrays have a fixed size and are "rigid" - you can't efficiently break them into smaller pieces - so typically growing or shrinking an array-based structure takes time. Linked structures, being made of smaller pieces, can easily be divided up into smaller pieces or built up out of new pieces. For example, to append an element to an array, you may have to allocate a giant new array, copy over all the old elements, then append the new element. With a linked list or linked structure, you could just add another piece onto the end, which can be a lot more efficient. Similarly, if you have a sorted array and need to insert an element, you may have to shuffle down all of the other elements in the array, since there is no way to "splice" something into the array. If the sorted sequence is stored in a binary search tree, the new element can be added in in the proper place without moving any other elements around, which makes insertions more efficient.
I don't believe there is anything called a "stingy linked list." I think you mean singly-linked list, which is a linked list in which each cell (piece) stores only one link, usually to the next element in the sequence. This makes it easy to scan forward in the list from one element to the next, but makes it difficult to back up one position in the list.
Quite honestly, there is no simple way to enumerate all the cases where you'd want to use a linked structure because so many structures are linked structures. I would suggest picking up a book on fundamental data types (lists, stacks, queues, trees, etc.) to learn more about this. I just finished teaching a quarter-long programming class dedicated to this topic, and I doubt it's possible to condense into a single SO answer. :-)
Hope this helps!
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.
This shouldn't be a difficult question, but I'd just like someone to bounce it off of before I continue. I simply need to decide what data structure to use based on these expected activities:
Will need to frequently iterate through in sorted order (starting at the head).
Will need to remove/restore arbitrary elements from the/a sorted view.
Later I'll be frequently resorting the data and working with multiple sorted views.
Also later I'll be frequently changing the position of elements within their sorted views.
This is in Java, by the way.
My best guess is that I'll either be rolling some custom Linked Hash Set (to arrange the links in sorted order) or possibly just using a Tree Set. But I'm still not completely sure yet. Recommendations?
Edit: I guess because of the arbitrary remove/restore, I should probably stick with a Tree Set, right?
Actually, not necessarily. Hmmm...
In theory, I'd say the right data structure is a multiway tree - preferably something like a B+ tree. Traditionally this is a disk-based data structure, but modern main memory has a lot of similar characteristics due to layers of cache and virtual memory.
In-order iteration of a B+ tree is very efficient because (1) you only iterate through the linked-list of leaf nodes - branch nodes aren't needed, and (2) you get extremely good locality.
Finding, removing and inserting arbitrary elements is log(n) as with any balanced tree, though with different constant factors.
Resorting within the tree is mostly a matter of choosing an algorithm that gives good performance when operating on a linked list of blocks (the leaf nodes), minimising the need to use leaf nodes - variants of quicksort or mergesort seem like likely candidates. Once the items are sorted in the branch nodes, just propogate the summary information back through the leaf nodes.
BUT - pragmatically, this is only something you'd do if you're very sure that you need it. Odds are good that you're better off using some standard container. Algorithm/data structure optimisation is the best kind of optimisation, but it can still be premature.
Standard LinkedHashSet or LinkedMultiset from google collections if you want your data structure to store not unique values.