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
Related
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.
I'm sorry if it seems that I'm coming off as 100% completely clueless; it is not my intention... I truly have tried everything I've learnt so far in my Data Structures course to attempt this project but have ended up getting nowhere and am now seeking help as to how I should go about attempting this.
My final project is to develop the logical & implementation layer of a new data structure that we'll be calling "Family Tree".
In this tree-type data structure each node is a simple node that has the following data values assigned to it: Name, Gender, Date of Birth, Date of Death and Parent (which is a special type of node)
The parent nodes are a special node that contain two sub-nodes, 1 for the father and 1 for the mother, and contain links to 0 or more children and only parent nodes can contain children.
At the very least, I should be able to implement the following methods:
Create Parent Node: Combine two nodes into one parent node.
Add Node: Add a new node (family member) to the tree, under a parent.
Print: Print out the family tree in an informative format.
I have tried everything from adding on to an already existing Tree data structure & creating an object of type Person to store values to be read by each node from the tree to working up my own data structure from scratch.
Out of sheer frustration I've deleted most of my work because it wasn't really going anywhere fruitful.
Any advice on how I can tackle this particular problem?
I wouldn't normally, under any circumstances, post a question to this forum without leaving some of my work but I'm at wits end.
What you are looking for is maybe more of a Graph. A node should have two parent nodes and 0-n child nodes. The node itself contains the personal info you want. No need for special "parent nodes". Search a bit around the place, there seem to be a lot of similar questions
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)
I have a Tree structure on my swing UI. Each object in the Tree represents a element in the network. The elements in the network have alarms raised on it. Alarms (one or more) are represented in the UI with a bell image besides the network element. I use a Cache (TreeMap) to store the network elements with the name of the network element as the KEY and Alarms as Value. However, traversing the TreeMap is pretty in-efficient. The names of the network element are Strings. I would like to store the elements in a parent-child relation (like the real UI) and the STRING names fail to do that.
I would like to create a custom data-structure which would emulate my UI hierarchy. I would think that customising linked list would do the job for me. Is there any tried and tested data-structure which I can use? Any other opinions highly appreciated.
Use domain objects for your data structure ("model"), with classes like Site, Host, Application, NetworkLink etc. Then you can have methods like clearAlarms() and properties like getState() and getName().
i want to use a list which will store objects of some type (lets say for simplicity - books) so i can show them in a listview object.
im kinda new to this, so i ask for the help of more advanced and experienced users about the following debates -
which one to use? linkedlist is something im familiar with. however, how do i make the app maintain the list? should i save the details of each object in a XML? if i do that, isnt it just better to use Arraylist? (please exclude in your answer things related to proccessing time).
if not via xml - how do i 'store' a list for later use even when the app is shut down and later on activated?
Thanks!
ArrayLists are good to use when you want random access via an indexed lookup. They're just as well suited for iterating through as LinkedLists.
OTOH, LinkedList doesn't need to be resized, it only runs out of room when you run out of memory to hold more nodes. If you have lots of data growth, or you're doing lots of sequential add/removes, then LinkedLists will win out in performance.
Sometimes you need both random access and growth, in those cases you need to make a judgment call on which criteria you want to be more performant.
In your current use case, I'd probably choose an ArrayList, you'll likely know how big the list should be, it won't be changing in size that often, and if you want to display this thing in a GUI, you're likely to need to do indexed lookups.
As far as storing the list, XML is as good a means as any, CSV files (or plain line-delimited text files), YAML, JSON and even class serialization are some alternatives, choose what's easiest and most convenient for you.
You must storage your data into SQLite. Android provides a very easy way. Look at this tutorial: http://www.vogella.de/articles/AndroidSQLite/article.html
I would prefer ArrayList over LinkedList because it has methods to manipulate the size of the array that is used internally to store the list
If i am going to use it as a stack, queue, or double-ended queue then I would use a LinkedList