C# 树状结构的输出列表
我需要用C#编写一个递归函数,它将在控制台中以以下树结构打印数据:C# 树状结构的输出列表,c#,.net,recursion,C#,.net,Recursion,我需要用C#编写一个递归函数,它将在控制台中以以下树结构打印数据: -Continent --Country ---Province ----City1 ----- Suburb4 ------ House3 ----City2 ----- Suburb1 ----- Suburb2 ------ House1 ------ House6 ----- Suburb3 ------ House4 ------ House5 我有一个数据集列表区域,如下所示: ID Description Pa
-Continent
--Country
---Province
----City1
----- Suburb4
------ House3
----City2
----- Suburb1
----- Suburb2
------ House1
------ House6
----- Suburb3
------ House4
------ House5
我有一个数据集列表区域,如下所示:
ID Description ParentID
1 Continent Null
2 Country 1
3 Province 2
4 City1 3
5 Suburb1 6
6 City2 3
7 Suburb2 6
8 Suburb3 6
9 Suburb4 4
10 House1 7
11 House3 9
12 House4 8
13 House5 8
14 House6 7
如何使下面的代码创建该输出
我尝试了以下几点:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to start...");
Console.ReadKey(true);
var area = new List<Areas>();
area = PopulateList(area);
area.Sort((s1,s2) => s1.ParentID.CompareTo(s2.ParentID));
PrintData(area);
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
private static void PrintData(List<Areas> area)
{
Console.WriteLine("-" + area[0].Description);
string[] array = new string[13];
foreach (var item in area)
{
string[] children = GetChildren(area, item);
if (children != null)
{
int count = 0;
foreach (var child in children)
{
if (child != null)
{
for (int i = 0; i <= count +1; i++)
{
Console.Write("-");
}
int count2 = 0;
foreach (string x in array)
{
if ((x != null) && (x.Contains(children[count])))
{
}
else
{
if (children[count2] != null)
{
Console.WriteLine(children[count2]);
array[count2] = children[count2];
count2++;
}
}
}
count++;
}
}
}
}
}
private static string[] GetChildren(List<Areas> areas, Areas item)
{
string[] children = new string[13];
int count = 0;
foreach (var area in areas)
{
if ((item.ID == area.ParentID) && (area.ParentID != 0))
{
children[count] = area.Description;
count++;
foreach (var area2 in areas)
{
if ((area.ID == area2.ParentID) && (area2.ParentID != 0))
{
children[count] = area2.Description;
//count++;
foreach (var area3 in areas)
{
if ((area.ID == area3.ParentID) && (area3.ParentID != 0))
{
children[count] = area3.Description;
//count++;
}
}
}
}
}
}
return children;
}
下面是您想要的。我已经将数据加载到一个通用列表中,但原理是一样的
private List<Entity> _entities = new List<Entity>();
private void Form1_Load(object sender, EventArgs e)
{
_entities.Add(new Entity(1, "Continent", null));
_entities.Add(new Entity(2, "Country", 1));
_entities.Add(new Entity(3, "Province", 2));
_entities.Add(new Entity(4, "City1", 3));
_entities.Add(new Entity(5, "Suburb1", 6));
_entities.Add(new Entity(6, "City2", 3));
_entities.Add(new Entity(7, "Suburb2", 6));
_entities.Add(new Entity(8, "Suburb3", 6));
_entities.Add(new Entity(9, "Suburb4", 4));
_entities.Add(new Entity(10, "House1", 7));
_entities.Add(new Entity(11, "House3", 9));
_entities.Add(new Entity(12, "House4", 8));
_entities.Add(new Entity(13, "House5", 8));
_entities.Add(new Entity(14, "House6", 7));
var parent = _entities.Find((x) => x.ParentId == 0);
EnumerateChildren(parent, 1);
}
private void EnumerateChildren(Entity thisEntity, int level)
{
Debug.WriteLine(new string('-', level) + thisEntity.Name);
foreach (var child in _entities.FindAll((x) => x.ParentId == thisEntity.Id).OrderBy(x => x.Id))
{
EnumerateChildren(child, level + 1);
}
}
private class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Entity(int id, string name, int? parentId)
{
this.Id = id;
this.ParentId = parentId.HasValue ? Convert.ToInt32(parentId) : 0;
this.Name = name;
}
}
private List_entities=new List();
私有void Form1\u加载(对象发送方、事件参数e)
{
_添加(新实体(1,“大陆”,空);
_增加(新实体(2,“国家”,1));
_增加(新实体(3,“省”,2));
_增加(新实体(4,“城市1”,3));
_增加(新实体(5,“郊区1”,6));
_增加(新实体(6,“城市2”,3));
_增加(新实体(7,“郊区2”,6));
_增加(新实体(8,“郊区3”,6));
_增加(新实体(9,“郊区4”,4));
_实体。增加(新实体(10,“1”,7));
_增加(新实体(11,“第3家”,第9家);
_增加(新实体(12,“第4家”,第8家);
_实体。增加(新实体(13,“5”,8));
_增加(新实体(14,“6”,7));
var parent=_entities.Find((x)=>x.ParentId==0);
1名子女(父母,1名);
}
私有子对象(实体thisEntity,int级别)
{
Debug.WriteLine(新字符串('-',level)+thisEntity.Name);
foreach(在_entities.FindAll((x)=>x.ParentId==thisEntity.Id.OrderBy(x=>x.Id))中的var child)
{
枚举子项(子项,级别+1);
}
}
私有类实体
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int ParentId{get;set;}
公共实体(int-id、字符串名称、int-parentId)
{
这个.Id=Id;
this.ParentId=ParentId.HasValue?Convert.ToInt32(ParentId):0;
this.Name=Name;
}
}
以下内容符合您的要求。我已经将数据加载到一个通用列表中,但原理是一样的
private List<Entity> _entities = new List<Entity>();
private void Form1_Load(object sender, EventArgs e)
{
_entities.Add(new Entity(1, "Continent", null));
_entities.Add(new Entity(2, "Country", 1));
_entities.Add(new Entity(3, "Province", 2));
_entities.Add(new Entity(4, "City1", 3));
_entities.Add(new Entity(5, "Suburb1", 6));
_entities.Add(new Entity(6, "City2", 3));
_entities.Add(new Entity(7, "Suburb2", 6));
_entities.Add(new Entity(8, "Suburb3", 6));
_entities.Add(new Entity(9, "Suburb4", 4));
_entities.Add(new Entity(10, "House1", 7));
_entities.Add(new Entity(11, "House3", 9));
_entities.Add(new Entity(12, "House4", 8));
_entities.Add(new Entity(13, "House5", 8));
_entities.Add(new Entity(14, "House6", 7));
var parent = _entities.Find((x) => x.ParentId == 0);
EnumerateChildren(parent, 1);
}
private void EnumerateChildren(Entity thisEntity, int level)
{
Debug.WriteLine(new string('-', level) + thisEntity.Name);
foreach (var child in _entities.FindAll((x) => x.ParentId == thisEntity.Id).OrderBy(x => x.Id))
{
EnumerateChildren(child, level + 1);
}
}
private class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Entity(int id, string name, int? parentId)
{
this.Id = id;
this.ParentId = parentId.HasValue ? Convert.ToInt32(parentId) : 0;
this.Name = name;
}
}
private List_entities=new List();
私有void Form1\u加载(对象发送方、事件参数e)
{
_添加(新实体(1,“大陆”,空);
_增加(新实体(2,“国家”,1));
_增加(新实体(3,“省”,2));
_增加(新实体(4,“城市1”,3));
_增加(新实体(5,“郊区1”,6));
_增加(新实体(6,“城市2”,3));
_增加(新实体(7,“郊区2”,6));
_增加(新实体(8,“郊区3”,6));
_增加(新实体(9,“郊区4”,4));
_实体。增加(新实体(10,“1”,7));
_增加(新实体(11,“第3家”,第9家);
_增加(新实体(12,“第4家”,第8家);
_实体。增加(新实体(13,“5”,8));
_增加(新实体(14,“6”,7));
var parent=_entities.Find((x)=>x.ParentId==0);
1名子女(父母,1名);
}
私有子对象(实体thisEntity,int级别)
{
Debug.WriteLine(新字符串('-',level)+thisEntity.Name);
foreach(在_entities.FindAll((x)=>x.ParentId==thisEntity.Id.OrderBy(x=>x.Id))中的var child)
{
枚举子项(子项,级别+1);
}
}
私有类实体
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int ParentId{get;set;}
公共实体(int-id、字符串名称、int-parentId)
{
这个.Id=Id;
this.ParentId=ParentId.HasValue?Convert.ToInt32(ParentId):0;
this.Name=Name;
}
}
我将在字典中加载区域信息,其中int
键表示ParentID
,而列表
值表示给定ParentID
的所有子区域
例如:
struct区域{
公共int ID;
公共字符串描述;
公共int?ParentID;
}
void outputreastree(){
IEnumerable areas=GetAreasFromDataSource();
OutputAreasTreeFromNode(LoadAreasByParentID(区域),areasByParentID[0][0],1);
}
字典加载区域ByParentID(IEnumerable areas){
Dictionary areasByParentID=新字典();
foreach(面积中面积){
int parentID=area.parentID±0;//使用0作为根的parentID
列出儿童名单;
if(!areasByParentID.TryGetValue(父项ID,输出子项)){
children=新列表();
areasByParentID.Add(parentID,children);
}
添加(面积);
}
返回区比帕雷蒂德;
}
void OutputAreasTreeFromNode(字典区域ByParentId、区域区域、整数深度){
for(int i=0;i
希望有帮助。我会将区域信息加载到字典中,其中int
键表示ParentID
,而列表
值表示给定ParentID
的所有子区域
例如:
struct区域{
公共int ID;
公共字符串描述;
公共int?ParentID;
}
void outputreastree(){
IEnumerable areas=GetAreasFromDataSource();
OutputAreasTreeFromNode(LoadAreasByParentID(区域),areasByParentID[0][0],1);
}
字典加载区域ByParentID(IEnumerable areas){
Dictionary areasByParentID=新字典();
foreach(面积中面积){
int parentID=area.parentID±0;//使用0作为根的parentID
列出儿童名单;
if(!areasByParentID.TryGetValue(父项ID,输出子项)){
奇尔德
struct Area {
public int ID;
public string Description;
public int? ParentID;
}
void OutputAreasTree() {
IEnumerable<Area> areas = GetAreasFromDataSource();
OutputAreasTreeFromNode(LoadAreasByParentID(areas), areasByParentID[0][0], 1);
}
Dictionary<int, List<Area>> LoadAreasByParentID(IEnumerable<Area> areas) {
Dictionary<int, List<Area>> areasByParentID = new Dictionary<int, List<Area>>();
foreach (Area area in areas) {
int parentID = area.ParentID ?? 0; // use 0 as the ParentID of the root
List<Area> children;
if (!areasByParentID.TryGetValue(parentID, out children)) {
children = new List<Area>();
areasByParentID.Add(parentID, children);
}
children.Add(area);
}
return areasByParentID;
}
void OutputAreasTreeFromNode(Dictionary<int, List<Area>> areasByParentID, Area area, int depth) {
for (int i = 0; i < depth; i++)
Console.Write('-');
Console.WriteLine(area.Description);
List<Area> children;
if (areasByParentID.TryGetValue(area.ID, out children)) {
foreach (Area child in children)
OutputAreasTreeFromNode(areasByParentID, child, depth+1);
}
}