简体   繁体   中英

How to find any item from this Hierarchical Parent-Child Structure in C#

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; }
    }

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.");
}

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM