英文:
c# Recursive model with what level you are on
问题
Sure, here's the translated code portion:
这是您要的翻译部分:
原始代码部分:
public IEnumerable<Model> GetItems(string rootLink) {
foreach (var node1 in loadItems(rootLink)) {
yield return new Model(node1, level: 1);
foreach (var node2 in loadItems(node1.link)) {
yield return new Model(node2 , level: 2);
foreach (var node3 in loadItems(node2.link)) {
yield return new Model(node3 , level: 3);
}
}
}
}
尝试使用递归的代码部分:
public IEnumerable<Model> GetItems(string link, int level = 1)
{
foreach (var node in loadItems(link))
{
yield return new Model(node, level);
foreach (var subNode in GetItems(node.link, level + 1))
{
yield return subNode;
}
}
}
希望这对您有所帮助。如果您需要更多帮助,请随时告诉我。
英文:
Say you have a simplified tree like this
- Level 1 item
- Level 2 item
- Level 2 item
- Level 3 item
- Level 1 item
- Level 1 item
- Level 2 item
- Level 3 item
- Level 3 item
- Level 2 item
- Level 2 item
I have this code which works fine and loops through it and creates a list with all the items and which level they are on. But this will only work with 3 levels and it would be nice if this could work with any levels and less repeated code.
public IEnumerable<Model> GetItems(string rootLink) {
foreach (var node1 in loadItems(rootLink) {
yield return new Model(node1, level: 1);
foreach (var node2 in loadItems(node1.link) {
yield return new Model(node2 , level: 2);
foreach (var node3 in loadItems(node2.link) {
yield return new Model(node3 , level: 3);
}
}
}
}
So I am wondering if I can make this a recursive function instead. I tried with something like this, but the level will be wrong here since I am just adding to it.
public IEnumerable<Model> GetItems(string link, int level = 1)
{
foreach (var node in loadItems(link))
{
yield return new Model(category, level);
foreach (var subNode in GetItems(category.link, level++))
{
yield return subNode;
}
}
}
答案1
得分: 1
以下是翻译好的代码部分:
public IEnumerable<Model> GetItems(string rootLink)
{
// 设置工作队列
var queue = new Queue<Item>();
// 从根节点开始
foreach(var level1 in loadItems(rootLink))
queue.Enqueue(new Item(level1, 1));
while(queue.Count > 0)
{
var current = queue.Dequeue();
yield return new Model(current.Node, current.Level);
// 如果当前节点有子节点,则将其加入队列
foreach( var child in loadItems(current.Node.link))
queue.Enqueue(new Item(child, current.Level+1);
}
}
private record Item(YourType Node, int Level);
希望这有所帮助!
英文:
Untested, but an iterative approach would look something like this:
public IEnumerable<Model> GetItems(string rootLink)
{
// Setup a work queue
var queue = new Queue<Item>();
// Start with the root node
foreach(var level1 in loadItems(rootLink))
queue.Enqueue(new Item(level1, 1));
while(queue.Count > 0)
{
var current = queue.Dequeue();
yield return new Model(current.Node, current.Level);
// Enqueue child-nodes of the current node if any
foreach( var child in loadItems(current.Node.link))
queue.Enqueue(new Item(child, current.Level+1);
}
}
private record Item(YourType Node, int Level);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论