【问题标题】:How to find any item from this Hierarchical Parent-Child Structure in C#How to find any item from this Hierarchical Parent-Child Structure in C#
【发布时间】:2022-12-27 20:48:05
【问题描述】:

how to find any item from the list as it is dynamic, it may be in the parent or child in any position of list and need to stop finding when the list of child count will be 0, below is the Model of the list and and example of Hierarchical Parent-Child Structure

example:-

Parent->child->child->child or parent->child->child->child->child->child->child

public class Child
    {
        public string key { get; set; }
        public string title { get; set; }
        public string parent_Category { get; set; }
        public List<Child> children { get; set; }
    }

    public class Parent
    {
        public string key { get; set; }
        public string title { get; set; }
        public string parent_Category { get; set; }
        public List<Child> children { get; set; }
    }

【问题讨论】:

  • why do you have different classes Parent and Child?
  • @viveknuna because data is coming like that only and i have to manage with this classes
  • class Child: Parent is this valid in your case?

标签: c# .net linq xamarin c#-4.0


【解决方案1】:

you can do a recursive search, for example:

Child SearchChild(string title, Child c)
{
    if (c.title == title)
    {
        return c;
    }
    return SearchAmongChildren(title, c.children);
}

Child SearchAmongChildren(string title, List<Child> children)
{
    foreach (var c in children)
    {
        if (c.title == title)
        {
            return c;
        }

        if (c.children != null)
        {
            var _c = SearchAmongChildren(title, c.children);

            if (_c != null)
                return _c;
        }
    }

    return null;
}

Then, initiate the search:

var theChildThatIWant = SearchAmongChildren("the title that I want", parent.children);

var theChildThatIWant = SearchChild("the title that I want", child);

if (theChildThatIWant == null)
{
    throw new ChildNotFoundException("Sorry, the child that you're looking for is not available.");
}

【讨论】:

    【解决方案2】:

    You can use BFS or DFS search.

    bool Exist(Parent parent, string key)
    {
        if (parent.key == key) return true;
    
        if (parent.children == null) return false;
        var queue = new Queue<Child>(parent.children);
        while (queue.Any())
        {
            Child c = queue.Dequeue();
            bool match = c.key == key;
            if (match) return true;
    
            if (c.children == null) continue;
            foreach (Child grandChild in c.children)
            {
                queue.Enqueue(grandChild);
            }
        }
    
        return false;
    }
    

    // TEST SCENERIO

    Parent parent1 = new Parent();
    var result1 = Exist(parent1, "key-1");
    Console.WriteLine($"Actual : {result1}, Expected : False");
    
    Parent parent2 = new Parent { children = new List<Child> { new Child() { key = "key-1" }, new Child() { key = "key-2" } } };
    var result2 = Exist(parent2, "key-2");
    Console.WriteLine($"Actual : {result2}, Expected : True");
    
    Parent parent3 = new Parent { children = new List<Child> { new Child() { key = "key-1" }, new Child() { key = "key-2", children = new List<Child> { new Child() { key = "key-5" } } } } };
    var result3 = Exist(parent3, "key-5");
    Console.WriteLine($"Actual : {result3}, Expected : True");
    
    var result4 = Exist(parent3, "key-21323");
    Console.WriteLine($"Actual : {result4}, Expected : False");
    

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2017-03-11
      • 2022-12-02
      • 2018-09-22
      • 2022-12-28
      • 2022-12-02
      • 2022-12-02
      • 2022-12-27
      • 2022-12-02
      相关资源
      最近更新 更多