具有多个参数的C#动态树,如何实现?
我试图用一个以上的参数(点和类型)填充一棵树,最后,显示哪个“分支”的点最多,在每个分支中,显示我有多少个相等的元组 树将是这样的: 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) 父亲(分数:200 |类型:2) |_CHILD01(P:120 |类型:3) |||u CHILD4(P:300 | T:3) || | | u CHILD8(P:220 | T:3) || | | u CHILD9(P:65 | T:1) |||u CHILD5(P:15 | T:9) |_儿童2(P:10 | T:1) |_儿童3(P:80 | T:2) |_儿童6(P:25 | T:2) |||u CHILD10(P:110 | T:7) |||u CHILD11(P:195 | T:3) |_儿童7(P:50 | T:7) 我想得到的是: 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 每个分支的点数: Branch01->FATHER(200)、CHILD01(120)、CHILD04(300)、CHILD08(220)->总分:840 Branch02->FATHER(200)、CHILD01(120)、CHILD04(300)、CHILD09(65)->总分:685 Branch03->父亲(200)、孩子01(120)、孩子05(15)->总分:335 Branch04->父亲(200),孩子02(10)->总分:210 分支05->父亲(200),孩子03(80),孩子06(25),孩子10(110)->总分:415 Branch06->父亲(200),孩子03(80),孩子06(25),孩子11(195)->总分:500 Branch07->FATHER(200)、CHILD03(80)、CHILD07(50)->总分:330 及 计算分支所在的类型数: 类型PERBRANCH01: -类型1:0 -类型2:1 -类型3:2 -类型4:1 -打字5:0 -打字6:0 -打字7:0 -打字8:0 -打字9:0 类型PERBRANCH02: -类型1:1 -类型2:1 -类型3:1 -类型4:1 -打字5:0 -打字6:0 -打字7:0 -打字8:0 -打字9:0 类型PERBRANCH03: -类型1:0 -类型2:1 -类型3:1 -类型4:0 -打字5:0 -打字6:0 -打字7:0 -打字8:0 -类型9:1 类型PERBRANCH04: -类型1:1 -类型2:1 -类型3:0 -类型4:0 -打字5:0 -打字6:0 -打字7:0 -打字8:0 -打字9:0 类型PERBRANCH05: -类型1:0 -类型2:3 -类型3:0 -类型4:0 -打字5:0 -打字6:0 -类型7:1 -打字8:0 -打字9:0 类型PERBRANCH06: -类型1:0 -类型2:3 -类型3:1 -类型4:0 -打字5:0 -打字6:0 -打字7:0 -打字8:0 -打字9:0 类型PERBRANCH07: -类型1:0 -类型2:2 -类型3:0 -类型4:0 -打字5:0 -打字6:0 -类型7:1 -打字8:0 -打字9:0 我已经做了一些代码,但它不工作。 以下是功能:具有多个参数的C#动态树,如何实现?,c#,recursion,multidimensional-array,C#,Recursion,Multidimensional Array,我试图用一个以上的参数(点和类型)填充一棵树,最后,显示哪个“分支”的点最多,在每个分支中,显示我有多少个相等的元组 树将是这样的: 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
//
// 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));
for (var x = 0; x < xMin; x++)
for (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
where af.idpatrocinador == idusr
select af).ToList();
/*Se for 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
where 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
where 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;
}
}
//
//FUNÃO ResizeArray
公共T[,]大小调整数组(T[,]原始,int-xSize,int-ySize)
{
var newArray=newt[xSize,ySize];
var xMin=Math.Min(xSize,original.GetLength(0));
var yMin=Math.Min(ySize,original.GetLength(1));
对于(变量x=0;x
有人能帮我吗 这里的答案略有不同,节点的顺序与输入的顺序相同
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();
foreach (Count result in Node.results)
{
Console.WriteLine("Name = {0}, Points = {1}, Types = {2}",
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 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()
{
Count result = new Count();
result.name = name;
result.points = points;
result.types = new Dictionary<int, int>();
result.types.Add(type, 1);
if (children != null)
{
for (int childCount = children.Count - 1; childCount >= 0; childCount--)
{
Node child = children[childCount];
Count childResutls = child.GetTotals();
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
节点父节点=新节点()
{
name=“FATHER”,点数=200,类型=2,子项=new List(){
新节点(){
name=“CHILD01”,points=120,type=3,children=newlist(){
新节点(){
name=“CHILD04”,points=300,type=3,children=newlist(){
新节点(){name=“CHILD08”,points=220,type=3,children=null},
新节点(){name=“CHILD09”,points=65,type=1,children=null}
}
},
新节点(){name=“CHILD05”,points=15,type=9,children=null}
}
},
新节点(){name=“CHILD02”,points=10,type=1,children=null},
新节点(){
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();
foreach (Count result in Node.results)
{
Console.WriteLine("Name = {0}, Points = {1}, Types = {2}",
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 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()
{
Count result = new Count();
result.name = name;
result.points = points;
result.types = new Dictionary<int, int>();
result.types.Add(type, 1);
if (children != null)
{
for (int childCount = children.Count - 1; childCount >= 0; childCount--)
{
Node child = children[childCount];
Count childResutls = child.GetTotals();
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;
}
}
}
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)
{
for (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;
}
}
}