Search code examples
treeparentjstreechildren

Creating multiple parent hierarchy tree from a list of objects with position and depth parameters


I'm trying to iterate on a list which holds just parent ids and their children like below.

A

  • B
  • C

    • D
    • E

G

  • H
  • J

    • K

This tree can be in any depth and can have multiple parent objects. This list is in my hand and i want to use it like a lookup list. I will iterate on it and update the existing tree. While updating these objects i also want to update the depth and the position for all nodes. The node object is like below

public Node node
{
   Id
   List<Node> Children
}

In summary, i want to update my existing tree by iterating through my lookup list recursively. Furthermore I'm using jstree and I'm sending the last state of tree to the server as json and so I want to update my existing tree with the new json result. What i did is like below, altough it provides the parent-child relations it doesn't update the depth and position properties.

 private void TreeRecursively(Tree[] list)
        {
            var orderNum = 0;
            var depth = 0;

            foreach (var category in list)
            {
                var cat = db.Get(category.id.Value);
                cat.OrderNumber = orderNum;
                cat.Depth = depth;
                db.Update(cat);

                orderNum++;
                depth++;

                if (category.children.Any())
                {
                    var childOrderNum = 0;
                    foreach (var child in category.children)
                    {
                        var childCat = db.Get(child.id.Value);

                        childCat.OrderNumber = childOrderNum;
                        childCat.Parent = cat;
                        childCat.Depth = cat.Depth + 1;
                        db.Update(childCat); 
                        childOrderNum++;
                    }
                    TreeRecursively(category.children);
                }
            }
        }

Any suggestion ? Thanks.


Solution

  • I just solved the problem by rethinking with a clear mind and I update my code like below. Hope it helps someone. Take care.

       private void UpdateCategoryRecursively(IList<JsonTreeDto> list, 
    int depth, int pOrderNumber, Category parent = null)
                {
                    foreach (var category in list)
                    {
                        var pCategory = _categoryRepository.Get(category.id.Value);
    
                        pCategory.Parent = parent;
                        pCategory.Depth = depth;
                        pCategory.OrderNumber = pOrderNumber;
                        pCategory.OrderCode = CategoryHelper.CrateHierarchyOrderCode(depth, pOrderNumber);
                        _categoryRepository.Update(pCategory);
    
                        if (category.children != null && category.children.Any())
                        {
                            int childOrderNumber = 0;
                            foreach (var child in category.children)
                            {
                                var cCategory = _categoryRepository.Get(child.id.Value);
    
                                cCategory.Parent = pCategory;
                                cCategory.Depth = pCategory.Depth + 1;
                                cCategory.OrderNumber = childOrderNumber;
                                cCategory.OrderCode = CategoryHelper.CrateHierarchyOrderCode(pCategory.Depth + 1, childOrderNumber);
                                _categoryRepository.Update(cCategory);
                                childOrderNumber++;
    
                                UpdateCategoryRecursively(child.children, cCategory.Depth + 1, 0, cCategory);
                            }
                        }
                        pOrderNumber++;
                    }
                }
    

    Just call it like follow; UpdateCategoryRecursively(yourTree, 0, 0);