【问题标题】:Logic for creating a tree like representation创建树状表示的逻辑
【发布时间】:2018-12-18 03:37:29
【问题描述】:

我需要在网页上显示类似二叉树的结构 用来表示父子关系。与二叉树不同,这棵树可以有多个子节点 并且孩子们可以有更多的孩子,这个过程将一直持续到他们的父母没有孩子为止。

所以,我很困惑我的数据模型应该如何,而且我的想法并没有超出这个范围

public class Parent
{
  public string parentName {get;set;} // As their will be one start for this tree, I will have one parent node that will show the parent
  public List<string> child {get;set;} // As the parent can have multiple children, I can have a list of string 
}

但问题是孩子也可以作为父母,因为他们也可以有孩子。我该怎么办 实现这样的结构。

谢谢

【问题讨论】:

  • 我认为这只是一个tree-view 而不是二叉树。
  • 子节点不要使用左右指针,而是使用数组

标签: c# asp.net asp.net-mvc-4 tree logic


【解决方案1】:

string 不能有自己的子元素,因此它不是一个好的表示形式。

一个简单的方法是使子元素简单地成为与父元素相同类型的元素的List。让我们将它们都称为Node

public class Node
{
  public string Name { get; set; }

  public List<Node> ChildNodes { get; set; }
}

【讨论】:

  • 使用这个答案对recursion 进行一些研究:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 2015-05-08
  • 1970-01-01
相关资源
最近更新 更多