Please help with the algorithm. I have a list of this structure:

public class Stratigraphy { public Stratigraphy(int idType, int idSysstratigraphy, int stratparent, string name, string nameAdd, int ordernum) { IdType = idType; IdSysstratigraphy = idSysstratigraphy; Stratparent = stratparent; Ordernum = ordernum; } public int IdType { get; set; } public int IdSysstratigraphy { get; set; } public int Stratparent { get; set; } public int Ordernum { get; set; } } 

looks like this:

enter image description here

Here is the IdType field for the main sort. StratParent parent refers to IdSysstratigraphy. OrderNum order of elements I am a general StratParent

Visually, I need to present this structure in this way: enter image description here

How to design an algorithm for the image of such a structure based on a given list?

  • Outgoing nodes are not necessarily 2, they may be more or not at all. - Naf

1 answer 1

If you do not resort to drawing, you can use the standard TreeView control.

Allows you to build various data structures based on the nodes of TreeNode . Each TreeNode has a Level property and a name.

Here is an example of filling a tree. The subject area is completely different, but from here you can take the fill pattern.

The fill algorithm is based on the addition of a TreeNode to the TreeView TreeNode .

In your case, you need to do a sample in order of priority ID: First, sample by parent ID -> Fill nodes 0 level -> Sample by ID child to parent -> Fill nodes 1 level -> And so on recursively until you reach the last level .

 public static void RefillTreeDK(DKMSContext context, TreeView tree) { tree.Nodes.Clear(); tree.BeginUpdate(); TreeNode node0level=null; TreeNode node1level = null; TreeNode node2level = null; List<dkFactory> level0Group = context.dkFactory.AsNoTracking().ToList(); List<dkPlant> level1Group = null; List<dkName> level2Group = null; foreach(dkFactory fact in level0Group) { node0level = tree.Nodes.Add(fact.id.ToString(),fact.factoryName); node0level.ImageIndex = 0; node0level.Tag = fact.id; node0level.SelectedImageIndex = 0; level1Group = context.dkPlant.Where(x => x.idFactory == fact.id).OrderBy(x => x.plantName).ToList(); foreach (dkPlant pl in level1Group) { //1 уровень node1level = node0level.Nodes.Add(pl.id.ToString(), pl.plantName); node1level.Tag = pl.id; node1level.ImageIndex = 1; node1level.SelectedImageIndex = 1; //2 уровень level2Group = context.dkName.Where(x => x.idPlant == pl.id).ToList(); foreach (dkName dk in level2Group) { node2level = node1level.Nodes.Add(dk.id.ToString(), dk.dkTag + ": " + dk.dkDescriptor); node2level.Tag = dk.id; node2level.ImageIndex = 3; node2level.SelectedImageIndex = 3; } } } tree.EndUpdate(); }