c# - recursive program to get children of nodes -


I have the following table as an example

  PNLId PNLCode PNLParentId operator sign 0 49 C 51 + faucet 50 jade 51 + faucet 51 v 107 / null 52 b 107 / faucet 53 b 108 + null   

I am looking for relative parent / child So that I can define the following classes:

  class node {public id id (get; set;} public int? ParentId {get; set;} public string operator {received Rn; set;} public string sign {get; set;} public node guardian {receive; set;} public ILIT child (get; set;) public node () {children = new list & lt; node & gt; );} Public override string toasting () {// Return "node:" + operator + "" + + + + + + "+ string .join (", ", child. (X => x.id ); Return to "node:" + operator + "" + + + + + "+ string .join (", ", select child (x => string.format (" ({0}, {1 }) ", x sign, x.id)));}} public That zero GetChild (node, dictionary & lt; Int, node & gt; Map, dictionary & lt; Int, node & gt; Map) {foreach (child in the node) • foreach (var pairf in the map) {if (child.id = = Pairf.Value.ParentId) {child.Operator = pairf.Value.Operator; Var parent = map [pairf.Value.ParentId.Value]; Child.Children.Add (pairf.Value); Mapf [child.Id] = map [pairf.Value.Id]; //rootNodes.Add(pairf.Value); }} GetChild (child, map, mapf); }}   

I have successfully received the code using this code

  var map = new dictionary (); Var rootNodes = new list (); Foreach (Detroit line in dt.ows) {int id = convert toInt32 (line ["PNLID"]); Int? Parent = null; If (! Row.IsNull ("PNLParentId")) {parentId = Convert.ToInt32 (line ["PNLParentId"]); } String session = convert.ToString (line ["operator"]); String sign = Convert.ToString (line ["sign"]); Map [id] = new node {id = id, parent_d = parentId, operator = op, sign = sign}; } Var mapf = new dictionary & lt; Int, node & gt; (); // Mill Routond's Fourhach (different pair in map) {if (pair V. Parent Id Aqualls (empty)) {Map [pair. Value.id] = map [pair.view.ID]; rootNodes.Add (pair.Value); }} // Find operator for Routondod's foreach (different combinations in the map) {foreach (different doubles in the marriage) {if (pair.Value.Id == pairf.Value.ParentId) {pair.Value.Operator = pairf .value operator; Var parent = map [pairf.Value.ParentId.Value]; RootNodes.Add (pairf.Value); GetChild (rootNodes., Map, mapf); }}}   

I need to create node tree first. Then start with root nodes of copying operator from children. Copy the progress of children and operator of your children. For example <9 p>

  [92 nodes: / 92 (, 36), (, 37)] [93 nodes: + 93 (, 38), (, 39), (, 40)] [94 node: / 94 (, 69), (, 70)] [95 nodes: + 95 (, 81), (, 82)] [96 nodes: + 96 (, 83), (, 84), (85) ) The code above does not return this result;), (, 86), (, 87), (, 88), (, 89)]   

How do I modify it?

Starting from scratch:

  public IEnumerable & LT; Node & gt; CreateTree (DatatyLal Table) {var nodes = Gatnodes (table) .Oolist (); Var roots = new list & lt; Node & gt; (); Fresh (in nodes in nodes) {if (node.ParentId == null) root. Add (node); Else {var parent = nodes. Single (N => NID == node PantID); Create relationship (parent, node); }} Foreach (root in root) {UpdateOperators (root); } Return nodes; } Private Zero CreateRelationship (node ​​root, node child) {child.Parent = parent; parent.Children.Add (child); } Private IEnumerable & lt; Node & gt; GetNodes (DataTable table) {return from DataRow row in table. Select the RO CreateNode (line); } Private node CreateNode {date_node} {new node {id = convert.titInt32 (line ["PNID"]), PainterID = line.Isnale ("PNLPenderID") is back? Default (int?): Convert.ToInt32 (line ["PNLParentId"]), Operator = Convert. Toaster (line ["operator"]), sign = convert. Toasting (line ["sign"])}; } Private Zero UpdateOperators (node ​​parent) {if (parents! Child. Any ()) return; mother-father. Operator = parent Children (first). Operator; Forchha (Children in Parents.) {UpdatesApporters (Children); }}   

node should contain everything from your code except the GetChild method. In addition, children to the IList & lt; Node & gt; , should not be declared as a IList .

CreateTree is your entry point - it assigns a IEnumerable of all the nodes, correcting all the parent / child relationship between all the trees .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -