user tree with dots and profile in C#

Asked

Viewed 229 times

9

I have a list of users in which each has a sponsor (sponsor is also a user). Each user has points and is of one type. I want to know which branch of the tree has the highest number of points and for each branch to know how many equal types I have. Suppose I have the tree:

inserir a descrição da imagem aqui

The result would be: For the number of Points of each branch: inserir a descrição da imagem aqui

and for the number of equal types:

inserir a descrição da imagem aqui

What’s the best way to get this? I created a function that goes through the whole tree and in an array places the points and in another array places the types. Anyway, it is giving error. I’ve been driving around, but I could use some help. The function I created is:

   //
    // FUNÇÃO TreeBranchPath
    int[] TotalPontosRamo = new int[1];
    int[,] FolhaInfoPontos = new int[6, 6];
    int[,] FolhaInfoPatamar = new int[6, 6];
    int CountRamos = 1;
    int CountFilhos = 0;

    private void TreeBranchPath(int idusr, int usrpnts, int usrpata, int ramo, int filho, bool FirstInteract)
    {

        FolhaInfoPontos[ramo, filho] = usrpnts;
        FolhaInfoPatamar[ramo, filho] = usrpata;

        if (FirstInteract)
        {
            ramo = ramo + 1;
        }



        /* Inicio - Verifica se tem descendentes - afilhados */
        Session["ConfDados"] = Session["ConfDados"] + "Verifica se o User " + idusr + " tem descendentes<br>";
        var AfilhadosList = (from af in db.NRV_USERS
                                where af.idpatrocinador == idusr
                                select af).ToList();

        if (AfilhadosList.Count() != 0)
        {


            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;

                Session["ConfDados"] = Session["ConfDados"] + "O User " + descid.id_user + " tem  " + TotalUserPoints + " pontos (" + UserPoints.pontosgrupo + " + " + UserPoints.pontosproprios + ")<br>";
                /* Fim - Quantos Pontos o User tem */


                /* Inicio - Em que Patamar o User está */
                var AuxUserPatamar = (from cp in db.NRV_USERPATAMAR
                                      where cp.iduser == descid.id_user
                                      select cp.idpatamax).FirstOrDefault();
                Session["ConfDados"] = Session["ConfDados"] + "O User " + descid.id_user + " está no Patamar Maximo " + AuxUserPatamar + "<br>";
                /* Fim - Em que Patamar o User está */


                //FolhaInfoPontos = ResizeArray(FolhaInfoPontos, ramo + 1, numfilho + 1);
                //FolhaInfoPatamar = ResizeArray(FolhaInfoPatamar, ramo + 1, numfilho + 1);

                TreeBranchPath(descid.id_user, TotalUserPoints, AuxUserPatamar, ramo, CountFilhos, false);

               // TotalPontosRamo[ramo] = TotalPontosRamo[ramo] + TotalUserPoints;


                ramo = ramo + 1;

            }

        }
        else
        {
            //ramo = ramo + 1;
            CountRamos = CountRamos + 1;
            CountFilhos = 0;
            Session["ConfDados"] = Session["ConfDados"] + "O User " + idusr + " do Ramo " + ramo + " não descendentes<br><br>";
        }



    }

I wonder if someone could help me?

  • I set the arrays to limit 6, but the truth is I don’t know what the limit is. The array should be dynamic in order to always increase the number of branches and children.

  • That’s it! Thank you all for your help and/or tips

  • 1

    Ricardo, it seems from the comments that this question was answered, or solved. If it is, consider answering it with its final solution, or has the option of remove the question.

2 answers

1

Create a class that has the following properties:

class DadosRamo {
  LinkedList<Filho> PontosDoRamo;
  int TotalDePontos = 0;
  Dictionary<Filho, int> contagemPorTipoDeFilho;
}
  • PontosDoRamo: As you walk down the list, you must store the dots on that linked list.
  • TotalDePontos: Add up the points of every child here, at the end you’ll have the total.
  • ContagemPorTipoDeFilho: Add child to dictionary, if already there, add 1 to count.

At the end you should have one List<DadosRamo> with information from all its branches.

-2

the question seems pertinent and I do not know to what extent I will be able to give an adequate answer, but if I had to address a problem of this nature, I would perhaps prefer to use an architecture like Mongodb, perhaps combined with Ruby on Rails

Browser other questions tagged

You are not signed in. Login or sign up in order to post.