I want to create information about the tree structure.

Asked 2 years ago, Updated 2 years ago, 35 views

Enter a description of the image here

I'm trying to make some data into a tree structure.
I am not good at logic.
The image is
I would like to have A-F data like the image in the tree.

Data array with A to F as data and
There is an array of data in the arrow lines.
Each of the data from A to F has data on a line that connects
The data in the arrow line has information about which data A to F is connected at the end of the arrow.

As shown in the figure, it would be a little easier if you could see IN and OUT in the arrow data.
There is no such thing.
The data from A to F will only show that A is the top data.

List <Item>items;A to F list information
List<Arrow>arrows;arrow line list information


    public class element
    {
            public int Id;
            public int Parent;
            public IList Children;
    }

~

ObservableCollection<Element>tree=newObservableCollection<Element>();

Element element = new Element();
element.Id = items[0].Id;
Obtain A (top) information from items and list of connected item data
Set the retrieved list to element.Children
// Add Elements to Tree Information
tree.Add(element);

I think the same process will be done in a loop for the list of connected item data, but I am not sure how to separate the methods.

I'm sorry for the poor explanation, but I'd appreciate it if you could give me some advice.

c#

2022-09-30 18:29

1 Answers

They only know if there are root nodes and edges, so

//Element root;
// Element [] nodes;
var done = new List <Element >() {root};
varundone=nodes.Where(e=>e!=root).ToList();

I think it would be better to prepare a collection that has already been processed and has not been processed yet, and move it around while corresponding to the parent-child relationship.

//Repeat while raw node is present
while(undone.Any())
{
    // retrieve raw nodes one at a time
    for (vari=0;i<undone.Count;i++)
    {
        varn = nodes [i];

        var parent=done.FirstOrDefault(e=>Determine if there is an edge (e,n));

        if(parent!=null)
        {
            // Set each property if a parent is found on a processed node
            parent.Children.Add(n);
            n. Parent = parent;

            // move to a processed node
            done.Add(n);
            undone.RemoveAt(i);

            break;
        }
    }
}

In addition, ElementParent of type Element is of type Element and Children is of type Collection<Element> and other methods are overrided and Parent is automatically set.

public class Element
{
    public int Id;
    public element parent {get;private set;}

    private Collection <Element >_Children;
    public Collection <Element>Children
        =>_Children??(_Children=newElementCollection(this)));

    private class ElementCollection—Collection <Element >
    {
        private readonly Element Parent;
        public ElementCollection (Element parent)
        {
            Parent = parent;
        }

        protected override AddItem(int index, Element item)
        {
            if(item.Parent!=null)
            {
                through new InvalidOperationException();
            }
            item.Parent=Parent;
            super.AddItem(index,item);
        }
        // TODO: Also implement SetItem, RemoveItem, ClearItem
    }
}


2022-09-30 18:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.