c# - Problems with an Algorithm -


i have following 5 lines of string:

a.b.c.d.e a.b a.b.c a.b.c.d 

this hierarchical relationships. second row example means b child of a.

now want parse these lines class structure. each letter represented instance of class , points parent (or null if top level (a)). however, every letter should addded once.

i started following:

    string[] hierarchical = name.split('.');          if (hierarchical.count() > 1)         {             console.writeline("package '" + name + "' not top level , has parsed");              console.writeline("find parent '" + hierarchical[hierarchical.count() - 1] + "'");              findparent(name);          }         else         {             console.writeline("package '" + name + "' top level , added manager");             if (!manager.isalreadyadded(name))             {                 package p = new package(null, hierarchical[0], name);                 manager.add(p);             }         }      } 

this means if top level name (a in example above) add manager if not there. problem lies in findparent() method try find parent of 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);                     }                     found = 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 works not wanted, since adds packages top levels. how can correct this?

is first argument of package constructor chance parent? if so, passing null there issue.

btw, think may able simplify algorithm.

can't work:

string[] hierarchical = name.split('.'); foreach (string node in hierarchical){  if(!manager.contains(node)){       manager.addtoend(node); //adds node who's parent last node added    }  } 

since each line guaranteed "sorted" know if package hasn't been added yet, parent last node added.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -