Hey all, I want to create a tree like setup that can be dynamically changed/added to, but I don't know where to start. For eg. I have 4 names in a list (can be any since i want to be able to add to it) - James, Michelle, Adam and Teresa. Each of these lists have another list to add to. So for eg James list has Adam and Michelle. Adams list has Michelle and Teresa. Michelle's list has just Teresa and Teresa's list just nothing. Here is the initial setup: James > Adam, Michelle Adam > Michelle, Teresa Michelle > Teresa Teresa So the tree list should look like: James-Adam-Michelle-Teresa James-Adam-Teresa James-Michelle-Teresa Michelle-Teresa Adam-Michelle-Teresa Adam-Teresa Teresa And if I tree view this: Code (csharp): James ------------------Michelle----------------------Adam----------------------Teresa / \ | / \ Adam Michelle Teresa Michelle Teresa / \ / Michelle Teresa Teresa / Teresa Also if I add/remove names I want that to change the tree setup. Can anyone help with this? Thanks, jrDev.
If I understand you correctly you can use classes. For example: Code (csharp): public class Elders { (here specific functions for that class) } and then just assign classes for specific person.
Hey weither, your response is kind of vague. I've found this: http://stackoverflow.com/questions/9455717/how-to-implement-non-binary-tree-structure-using-c-sharp I am stuck in figuring out how to access the tree lists to see the results though. Any help from this?
The thing is what you want seems to be more along the lines of a Web not a Tree (in the classic sense). I can see that people in one part of the tree are connected to people in another part of the tree, so a web like structure would be better. And for those I would use Custom objects and Maps.
Yeah you can't really set this up like a tree, in a tree kind of hierarchy, any node can only have a single parent, You have some crazy links. T is a 'child' of M, but also a child of A somewhere else. You could do this with classes as shown in the link, but you can only have a single instance of any person. Code (csharp): class Person { List<Person> dependants = new list<Person>(); } now you would make 4 instances of person (one each), and can assign other persons into their dependants, but there is no way to represent this as a tree, a 'web' (as mentioned) is probably a better description Arrows representing who is in whos list Note: You'll need to be careful about creating circular dependencies if you plan in traverse the structure
I've only ever done this in C++ (not C# or other garbage collected languages) So I am not sure if it all applies, but I would have a class called Person Code (csharp): class Person{ string name; List<Person> connectedPeople; } Then you put each person in a map, so you can quickly look them up, then find all the people they are connected with via connectedPeople After a certain point the solution gets heavy when the "web" is extreme but for a couple thousand to a couple hundred thousand connections (depending on ram size etc) it should be relatively quick. I would help more but I don't know specific case you want. Again my knowledge of this is from a CS coding exercise where we had to create a game called 6 degrees of Kevin Bacon (a very narrow range of the game Six Degrees of Separation) and pointers were used, but the basic principle should be the same.
Ok, I am going to try a follow what you are saying. I have the class setup. How would I add the people connected to the person?
Am I to assume what you posted is similar to this Raigex? Code (csharp): public class Node { public string Data; public List<Node> Children = new List<Node>(); } public List<string> GetTraversal(Node root) { var list = new List<string>(); foreach (var child in root.Children()) { GetTraversal(child, "", list); } } private void GetTraversal(Node node, string path, List<string> list) { path = path == "" ? node.Data : path + " " + node.Data; if (node.Children.Count == 0) { list.Add(path); } else { foreach(var child in node.Children) { GetTraversal(child, path, list); } } }
Maps are like lists in the sense that in a list you get something by its position in the list (list.get(0...10...20...etc)) While a map you look for an object by another object. It just saves time on lookups as from what I know maps use some kind of internal hash algorithm to get transfer info Also in you previous post with the code (getTraversal(...)) the only thing i would have added is to store every node inside a map (or dictionary in C#) so that you can look up the starting node quickly. Also I am sorry but I am mostly a Java (Android) and C++ developer and for me it was all a map but as I just found out Map == C++ , Java Dictionary == Python, .NET (C#)