从txt文件填充TreeView [英] Populating a TreeView from a txt file

查看:48
本文介绍了从txt文件填充TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个TreeView和一个算法问题。



我有一个txt文件,每行都列有'Parent Child'数据。深度不固定。



txt文件如下所示:



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


。 。 。 。 。



txt文件没有层次结构,每行只有Parent Child。我想把txt文件读入TreeView以直观地显示数据。



我不确定从哪里开始。我可以抓住最顶级的父母,因为我知道他们是什么,之后我不知道如何继续。



任何建议都会有所帮助。谢谢,



-J

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

推荐答案

找出这样的问题的最好方法是想想如果你用笔和纸手动完成任务,你将如何完成任务。首先,你要记下Parent1和Child1。然后在下一行,您将获取Parent2并搜索您已经写下的内容...您是否已在树中找到它?不,那么你知道它是一个顶级节点,你把它写下来并且它是孩子。当您沿着列表向下移动时,将重复该过程。您可能希望将搜索过程保持在一个单独的方法中,以便在搜索过程中递归调用它。



希望这会有所帮助。
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.


谢谢Kschuler,



我一直在玩它。我甚至尝试过每节课的班级和孩子名单。不确定它是否有点过分。也许只是一个功能将到期。



将继续玩它。



谢谢你,



-J
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


I can get hold of the top most Parent...



所以这棵树有一个根?

我会假设是这样的。 :)



对不起,这是在C#(我的VB非常弱),但希望你能按照我正在做的事情:


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:

// 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]);
}



现在,您可以从已知的根父级开始递归遍历层次结构

使用 Hierarchy.TryGetValue(...)<,沿着 Hierarchy 递归获取父级的子级列表/ code>如上所述。如果返回false,则该名称为leaf。


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".


这篇关于从txt文件填充TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆