Problems with an Algorithm
I have the following 5 lines of string:
A.B.C.D.E
A.B
A.B.C
A
A.B.C.D
This are hierarchical relationships. The second row for example means that B is a child of A.
Now I want to parse these lines into a Class Structure. So that each letter is represented by an instance of a class and points to its parent (or null if top level (A)). However, every letter should only be addded once.
I started the following:
    String[] hierarchical = Name.Split('.');
        if (hierarchical.Count() > 1)
        {
            Console.WriteLine("Package '" + Name + "' is not top level and has to be parsed");
            Console.WriteLine("Find parent for '" + hierarchical[hierarchical.Count() - 1] + "'");
            findParent(Name);
        }
        else
        {
            Console.WriteLine("Package '" + Name + "' is top level and is added to the manager");
            if (!manager.isAlreadyAdded(Name))
            {
                Package p = new Package(null, hierarchical[0], Name);
                manager.add(p);
            }
        }
    }
This means if it is a top level name (A in the example above) then add it to the manager if it is not already there. The problem lies in the findParent() method where I try to find the parent of a subpackage:
private void findParent(String path)
        {
            String originalPath = path;
            bool found = false;
            while (!found)
            {
                int position = path.LastIndexOf(".");
                if (position == -1)
                {
                    Console.WriteLine("Top level reached: " + path);
                    if (!manager.isAlreadyAdded(path))
                    {
                        Package p = new Package(null, path, path);
                        manager.add(p);
                    }
                    fo开发者_运维问答und = true;
                }
                else
                {
                    path = path.Substring(0, position);
                    Console.WriteLine("Path: " + path);
                    if (!manager.isAlreadyAdded(path))
                    {
                        Package p = new Package(null,getName(path), path);
                        manager.add(p);
                    }
                }
            }
        }
        private string getName(string path)
        {
            int position = path.LastIndexOf(".");
            if (position == -1)
            {
                return path;
            }
            else
            {
                return path.Substring(position+1, path.Length - position - 1);
            }
        }
As expected this works not as I wanted, since it adds all packages as top levels. How can I correct this?
Is the first argument of the Package constructor by any chance the parent? If so, you are always passing null there which could be your issue.
Btw, I think you may be able to simplify your algorithm.
Can't something like this work:
String[] hierarchical = Name.Split('.');
foreach (String node in hierarchical){
 if(!manager.Contains(node)){
      manager.AddToEnd(node); //adds node who's parent is the last node added
  }
}
Since each line is guaranteed to be "sorted" you know if you get a package that hasn't been added yet, its parent is the last node already added.
I'm a little unclear on what you're trying to do, but this will set parent to the second to last entry (node) in your list:
        string[] nodes = path.Split(".".ToCharArray());
        string parent = nodes[nodes.Length - 2];
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论