我有一些工作代码在其他人的帮助下创建了一个3深的N树(或只有3个链接节点),但我不知道如何遍历节点列表来查找数据 - 例如旅行CarNo = 6的距离,Driver = Thor4和TripNo = 38。很抱歉,如果这是微不足道的,我是数据结构的新手,extensive网络搜索没有帮助。谢谢你的帮助。
public class Node
{
public List<Node> Children = new List<Node>();
public Node Parent = null;
public Node(Node fromParent = null)
{
if (fromParent != null)
{
Parent = fromParent;
fromParent.Children.Add(this);
}
}
}
public class Root : Node
{
public int PoolNo;
public int CarNo;
string Make;
public Root(int _PoolNo, int _CarNo, string _Make) : base(null)
{
PoolNo = _PoolNo;
CarNo = _CarNo;
Make = _Make;
}
}
public class Parameter : Node
{
public string Driver;
public Parameter(string _Driver, Node fromParent = null) : base(fromParent)
{
Driver = _Driver;
}
}
public class Value : Node
{
public int TripDistance;
public int TripNo;
public Value(int _TripDistance, int _TripNo, Node fromParent = null) : base(fromParent)
{
TripDistance = _TripDistance;
TripNo = _TripNo;
}
}
List<Node> CarPool = new List<Node>();
public void IDTree()
{
Random Rnd = new Random();
for (int K = 1; K <= 20; K++)
{
var Car = new Root(6,K, "Ford" + K); // PoolNo,CarNo,Make
for (int KK = 1; KK <= 30; KK++)
{
var Driver = new Parameter("Thor" + KK, Car); // Driver
for (int KKK = 1; KKK <= 58; KKK++)
{
int Distance = Rnd.Next(1, 1000);
var Details = new Value(Distance, KKK, Driver); // TripDistance,TripNo
}
}
CarPool.Add(Car);
}
// traverse CarPool list to find trip distance for Carno 6, driver Thor4, tripno 38
foreach(var T in CarPool)
{
// not sure how to do this...
}
}