0
Answer

Trying to make a dynamic recursive tree in C#

Ricardo Sousa

Ricardo Sousa

9y
433
1

I am trying to fill a tree with more than one parameter (points and types) and at the end, show which "branch" has the max of points and in every branch, show how many equal tupe I have.

The tree will be something like 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 what I am trying to get 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 THE NUMBER OF TYPES in WHICH 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 some code but its not working.
Here´s the function: 
 
 <pre>
 
//
// 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;
}
}
</pre> 
 
Can someone please help me or give me a clue?
 
Thank you in advance