示例#1
0
        public static void RecursivFunc(string[][] mas, Tree tree, int count)
        {
            //choosing best brunch
            List<double> ginilist = new List<double>();
            double[] ginigini = null;
            for (int i = 0; i < count; i++)
            {
                ginigini = CalcGini(i, mas, count);
                ginilist.Add(ginigini[ginigini.Length - 1]);
            }

            int index = ginilist.IndexOf(ginilist.Min());
            double[] ginimasiv = CalcGini(index, mas, count);

            //Adding to the tree
            List<string> children = new List<string>();
            for (int i = 1; i < mas.Length; i++)
            {
                if (!children.Contains(mas[i][index]))
                    children.Add(mas[i][index]);
            }

            tree.Add(mas[0][index], children);

            //if one child is perfect add it's result to the tree
            //else make recursion
            for (int i = 0; i < ginimasiv.Length - 1; i++)
            {
                if (ginimasiv[i] == 0)
                {
                    //you may know yes or no value
                    string yesno = null;
                    for (int j = 1; j < mas.Length; j++)
                        if (mas[j][index] == children[i])
                        {
                            yesno = mas[j][count];
                            break;
                        }

                    tree.Add(yesno, null);

                }
                else
                {
                    //need to cut your masiv
                    int lich = 0;
                    for (int j = 1; j < mas.Length; j++)
                        if (mas[j][index] == children[i]) lich++;

                    string[][] newmas = new string[lich + 1][];

                    newmas[0] = mas[0];
                    int ii = 1;
                    for (int j = 1; j < mas.Length; j++)
                    {
                        if (mas[j][index] == children[i])
                            newmas[ii++] = mas[j];
                    }

                    RecursivFunc(newmas, tree, count);
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            StreamReader streamReader = new StreamReader(@"E:\data.txt");
            {
                string readToEnd = streamReader.ReadToEnd();

                string[] readStrings = readToEnd.Split('*');

                int count = readStrings[0].Count(a => a == ',');

                string[][] mass = new string[readStrings.Length][];
                for (int i = 0; i < readStrings.Length; i++)
                    mass[i] = readStrings[i].Split(',');

                Tree tree = new Tree();
                RecursivFunc(mass, tree, count);

                tree.Show();
            }
            Console.ReadKey();
        }