Exemplo n.º 1
0
        private static Item BuildTree(string input)
        {
            var parent = new Item();

            if (input.Length == 0)
            {
                return parent;
            }

            return BuildTree(input, parent);
        }
Exemplo n.º 2
0
        private static Item BuildTree(string input, Item parent)
        {
            int start = -1;
            int nested = 0;

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == '(')
                {
                    if (start == -1)
                    {
                        start = i;
                    }
                    else
                    {
                        nested++;
                    }
                }
                else if (input[i] == ')')
                {
                    if (nested == 0)
                    {
                        if (start != -1)
                        {
                            string value = input.Substring(start, i - start);
                            string content = input.Substring(start + 1, i - start - 2);

                            var child = new Item();
                            child.Value = value;
                            child.Parent = parent;

                            BuildTree(content, child);

                            parent.Children.Add(child);

                            start = -1;
                            nested = 0;
                        }
                    }
                    else
                    {
                        nested--;
                    }
                }
            }

            return parent;
        }