예제 #1
0
        private int searchIdx(TreeData[] tree, TreeData data)
        {
            int idx = 0;

            foreach (TreeData td in tree)
            {
                if (td.Context == data.Context)
                {
                    break;
                }
                idx += 1;
            }
            return(idx);
        }
예제 #2
0
        public int CreateTree(ref TreeData[] tree, string path)
        {
            int ret = -1;

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    int N = int.Parse(sr.ReadLine());
                    if (N > 0 && N <= maxSize)
                    {
                        bool[] checkArr = new bool[maxSize];
                        tree = new TreeData[maxSize];
                        for (int i = 0; i < N; i++)
                        {
                            checkArr[i] = true;
                            tree[i]     = new TreeData();
                        }
                        for (int i = 0; i < N; i++)
                        {
                            string[] strs = sr.ReadLine().Split(new string[] { " " }, StringSplitOptions.None);
                            tree[i].Context = strs[0];
                            if (strs[1] != "-")
                            {
                                tree[i].left           = int.Parse(strs[1]);
                                checkArr[tree[i].left] = false;
                            }
                            if (strs[2] != "-")
                            {
                                tree[i].right           = int.Parse(strs[2]);
                                checkArr[tree[i].right] = false;
                            }
                        }
                        for (int i = 0; i < N; i++)
                        {
                            if (checkArr[i])
                            {
                                ret = i;
                                break;
                            }
                        }
                    }
                }
            }
            return(ret);
        }
예제 #3
0
        public int CreateTree(ref TreeData[] tree)
        {
            int ret = -1;
            int N;

            N = int.Parse(Console.ReadLine());
            if (N > 0 && N <= maxSize)
            {
                bool[] checkArr = new bool[maxSize];
                tree = new TreeData[maxSize];
                for (int i = 0; i < N; i++)
                {
                    checkArr[i] = true;
                    tree[i]     = new TreeData();
                }
                for (int i = 0; i < N; i++)
                {
                    string[] strs = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.None);
                    tree[i].Context = strs[0];
                    if (strs[1] != "-")
                    {
                        tree[i].left           = int.Parse(strs[1]);
                        checkArr[tree[i].left] = false;
                    }
                    if (strs[2] != "-")
                    {
                        tree[i].right           = int.Parse(strs[2]);
                        checkArr[tree[i].right] = false;
                    }
                }
                for (int i = 0; i < N; i++)
                {
                    if (checkArr[i])
                    {
                        ret = i;
                        break;
                    }
                }
            }
            return(ret);
        }