recursion - C# dynamic tree with many parameters, how to do it? -
i trying fill tree more 1 parameter (points , types) , @ end, show "branch" has max of points , in every branch, show how many equal tupe have.
the tree this:
father (points:200|type:2) |_child01 (p:120|type:3) | |_child4 (p:300|t:3) | | |_child8 (p:220|t:3) | | |_child9 (p:65|t:1) | |_child5 (p:15|t:9) |_child2 (p:10|t:1) |_child3 (p:80|t:2) |_child6 (p:25|t:2) | |_child10 (p:110|t:7) | |_child11 (p:195|t:3) |_child7 (p:50|t:7)
and trying is:
number of points per branch: branch01 -> father (200), child01 (120), child04 (300), child08 (220) -> totalpoints: 840 branch02 -> father (200), child01 (120), child04 (300), child09 (65) -> totalpoints: 685 branch03 -> father (200), child01 (120), child05 (15) -> totalpoints: 335 branch04 -> father (200), child02 (10) -> totalpoints: 210 branch05 -> father (200), child03 (80), child06 (25), child10 (110) -> totalpoints: 415 branch06 -> father (200), child03 (80), child06 (25), child11 (195) -> totalpoints: 500 branch07 -> father (200), child03 (80), child07 (50) -> totalpoints: 330
and
count number of types in branch: typeperbranch01: - type1:0 - type2:1 - type3:2 - type4:1 - type5:0 - type6:0 - type7:0 - type8:0 - type9:0 typeperbranch02: - type1:1 - type2:1 - type3:1 - type4:1 - type5:0 - type6:0 - type7:0 - type8:0 - type9:0 typeperbranch03: - type1:0 - type2:1 - type3:1 - type4:0 - type5:0 - type6:0 - type7:0 - type8:0 - type9:1 typeperbranch04: - type1:1 - type2:1 - type3:0 - type4:0 - type5:0 - type6:0 - type7:0 - type8:0 - type9:0 typeperbranch05: - type1:0 - type2:3 - type3:0 - type4:0 - type5:0 - type6:0 - type7:1 - type8:0 - type9:0 typeperbranch06: - type1:0 - type2:3 - type3:1 - type4:0 - type5:0 - type6:0 - type7:0 - type8:0 - type9:0 typeperbranch07: - type1:0 - type2:2 - type3:0 - type4:0 - type5:0 - type6:0 - type7:1 - type8:0 - type9:0
i have done code not working. here´s function:
// // funÇÃo resizearray public t[,] resizearray<t>(t[,] original, int xsize, int ysize) { var newarray = new t[xsize, ysize]; var xmin = math.min(xsize, original.getlength(0)); var ymin = math.min(ysize, original.getlength(1)); (var x = 0; x < xmin; x++) (var y = 0; y < ymin; y++) newarray[x, y] = original[x, y]; return newarray; } // // funÇÃo treebranchpath int[] totalpontosramo = new int[1]; int[,] folhainfopontos = new int[1, 1]; int[,] folhainfopatamar = new int[1, 1]; int countramos = 0; private void treebranchpath(int idusr, int usrpnts, int usrpata, int nivelnum, int ramonum) { folhainfopontos[nivelnum, ramonum] = usrpnts; folhainfopatamar[nivelnum, ramonum] = usrpata; var afilhadoslist = (from af in db.nrv_users af.idpatrocinador == idusr select af).tolist(); /*se null não tem descendentes */ if (afilhadoslist != null) { int countnumfilhos = afilhadoslist.count(); int countfilhos = 0; nivelnum = nivelnum + 1; folhainfopontos = resizearray(folhainfopontos, nivelnum, ramonum + countnumfilhos); folhainfopatamar = resizearray(folhainfopatamar, nivelnum, ramonum + countnumfilhos); foreach (var descid in afilhadoslist) { countfilhos = countfilhos + 1; /* inicio - quantos pontos o user tem */ var userpoints = (from pnt in db.nrv_userpontos pnt.iduser == descid.id_user && pnt.usrpntact == true select pnt).firstordefault(); int totaluserpoints = userpoints.pontosgrupo + userpoints.pontosproprios; /* fim - quantos pontos o user tem */ totalpontosramo[countramos] = totalpontosramo[countramos] + totaluserpoints; /* inicio - em que patamar o user está */ var auxuserpatamar = (from cp in db.nrv_userpatamar cp.iduser == idusr select cp.idpatamax).firstordefault(); /* fim - em que patamar o user está */ array.resize(ref totalpontosramo, countramos + 1); treebranchpath(descid.id_user, totaluserpoints, auxuserpatamar, nivelnum, countfilhos); } } else { return; } } can me?
here indented results based on level of node.
using system; using system.collections.generic; using system.linq; using system.text; namespace consoleapplication1 { class program { static void main(string[] args) { node father = new node() { name = "father", points = 200, type = 2, children = new list<node>(){ new node() { name = "child01", points = 120, type = 3, children = new list<node>(){ new node(){ name = "child04", points = 300, type = 3, children = new list<node>(){ new node(){ name = "child08", points = 220, type = 3, children = null}, new node(){ name = "child09", points = 65, type = 1, children = null} } }, new node(){ name = "child05", points = 15, type = 9, children = null} } }, new node() { name = "child02", points = 10, type = 1, children = null}, new node(){ name = "child03", points = 80, type = 2, children = new list<node>(){ new node(){ name = "child06", points = 25, type = 2, children = new list<node>(){ new node(){ name = "child10", points = 110, type = 7, children = null}, new node(){ name = "child11", points = 195, type = 3, children = null} } }, new node(){ name = "child07", points = 50, type = 7, children = null} } } } }; count results = father.gettotals(0); foreach (count result in node.results) { console.writeline("{0} name = {1}, points = {2}, types = {3}", string.join("", enumerable.repeat(" ", result.level)), result.name, result.points, string.join(",", result.types.orderby(x => x.key).select(x => x.key.tostring() + ":" + x.value.tostring()).toarray()) ); } } } public class count { public string name; public int level { get; set; } public int points { get; set; } public dictionary<int, int> types { get; set; } } public class node { public string name { get; set; } public int points { get; set; } public int type { get; set; } public list<node> children { get; set; } public static list<count> results = new list<count>(); public count gettotals(int level) { count result = new count(); result.name = name; result.level = level; result.points = points; result.types = new dictionary<int, int>(); result.types.add(type, 1); if (children != null) { (int childcount = children.count - 1; childcount >= 0; childcount--) { node child = children[childcount]; count childresutls = child.gettotals(level + 1); result.points += childresutls.points; foreach (int key in childresutls.types.keys) { if (result.types.containskey(key)) { result.types[key] += childresutls.types[key]; } else { result.types.add(key, childresutls.types[key]); } } } } node.results.insert(0, result); return result; } } }
Comments
Post a Comment