Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Creating Tree like code in C#

Discussion in 'Scripting' started by crafTDev, Aug 29, 2013.

  1. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,809
    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):
    1.                James ------------------Michelle----------------------Adam----------------------Teresa
    2.                /  \                       |                          /  \                              
    3.           Adam    Michelle             Teresa                Michelle   Teresa              
    4.            /          \                                           /              
    5.      Michelle        Teresa                                  Teresa  
    6.         /
    7.    Teresa    
    Also if I add/remove names I want that to change the tree setup. Can anyone help with this?

    Thanks,

    jrDev.
     
    Last edited: Aug 30, 2013
  2. Weither

    Weither

    Joined:
    Aug 29, 2013
    Posts:
    4
    If I understand you correctly you can use classes. For example:

    Code (csharp):
    1.  
    2. public class Elders
    3. {
    4. (here specific functions for that class)
    5. }
    6.  
    and then just assign classes for specific person.
     
  3. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,809
  4. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    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.
     
  5. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,809
    Can you explain with examples?
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,188
    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):
    1. class Person {
    2. List<Person> dependants = new list<Person>();
    3. }
    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
     
    Last edited: Aug 29, 2013
  7. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    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):
    1.  
    2. class Person{
    3.     string name;
    4.     List<Person> connectedPeople;
    5. }
    6.  
    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.
     
  8. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,809
    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?
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,188
    myList.add (...) ...
     
  10. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,809
    Am I to assume what you posted is similar to this Raigex?

    Code (csharp):
    1. public class Node {
    2.   public string Data;
    3.   public List<Node> Children = new List<Node>();    
    4. }
    5.  
    6. public List<string> GetTraversal(Node root) {
    7.   var list = new List<string>();
    8.   foreach (var child in root.Children()) {
    9.     GetTraversal(child, "", list);
    10.   }
    11. }
    12.  
    13. private void GetTraversal(Node node, string path, List<string> list) {
    14.   path = path == "" ? node.Data : path + " " + node.Data;
    15.   if (node.Children.Count == 0) {
    16.     list.Add(path);
    17.   } else {
    18.     foreach(var child in node.Children) {
    19.       GetTraversal(child, path, list);
    20.     }
    21.   }
    22. }
     
  11. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    Yes something similar. Except I had to use maps when I did it.
     
  12. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,809
    You keep saying maps...what is that like and how would I go about doing it?
     
  13. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    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#)