Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a TreeView and an algorithm question.

I have a txt file that has a 'Parent Child' data listed on each line. The depth is not fixed.

the txt file looks like this:

Parent1 Child1
Parent1 Child2
Parent1 Child3
Parent1 Chile4
Child2 Child2A
Child1 Child1A
Child2 Child2B
Child2 Child2C
Child1 Child1B
Parent2 Child1
Parent1 Child5
Child2A Child2A1
Child2A Child2A2

. . . . .

the txt file has no hierarchy, only "Parent Child" on each line. I would like to take the txt file and read it into a TreeView to visually display the data.

I am not sure where to even start. I can get hold of the top most Parent because i know what they are, after that i have no idea how to go on.

Any advice would be helpful. Thank you,

-J
Posted

The best way to figure out a problem like this is to think about how you would accomplish the task if you were doing it manually with pen and paper. First you would write down Parent1 and Child1. Then on the next line you would take Parent2 and search what you had already written down...do you find it already in your tree? No. Then you know it's a top node and you write it down and it's child. That process will be repeated as you make your way down the list. You will probably want to keep the search process in a separate method so that you can call it recursively during your search.

Hope this helps.
 
Share this answer
 
Thank you Kschuler,

I have been playing with it. I even tried with a class and a list of children in each class. Not sure if it's overkill. Perhaps just a function will due.

Will keep playing with it.

Thank you,

-J
 
Share this answer
 
I can get hold of the top most Parent...

So there is a single root to this tree?
I'm going to assume that's the case. :)

Sorry, this is in C# (my VB is very weak), but hopefully you can follow what I'm doing:
C#
// scope this appropriately...
Dictionary<string, List<string>> Hierarchy = new Dictionary<string, List<string>>();
// open the file and read it line by line:
foreach (string line in File.ReadLines(filepath))
{
  if (string.IsNullOrWhiteSpace(line))
    continue;

  string[] parentChild = line.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries);
  if (parentChild.Length != 2)
  {
    // bad input line, report appropriately
    continue;  // next
  }
  string parent = parentChild[0];
  List<string> children;
  // This is where using the Dictionary is the big win:
  // Finding existing parents to add children to is essentially constant time,
  // not a search with time depending on how many items have already been seen.
  if (!Hierarchy.TryGetValue(parent, out children))
  {
    children = new List<string>();
    Hierarchy[parent] = children;
  }
  children.Add(parentChild[1]);
}

Now you can recursively traverse the Hierarchy starting with your known root parent.
Walk down the list of children of that parent recursively getting them from the Hierarchy using the Hierarchy.TryGetValue(...) as above. If that returns false, then that name is a "leaf".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900