Exemplo n.º 1
0
        public BinNode <int> BuildTreePre(string[] preOrder, string[] inOrder)
        {
            int           i       = 0;
            BinNode <int> newTree = new BinNode <int>(int.Parse(preOrder[0]));

            while (preOrder[0] != inOrder[i])
            {
                i++;
            }

            if (i > 0)
            {
                string[] preOrderL = new string[i];
                string[] inOrderL  = new string[i];
                for (int j = 0; j < i; j++)
                {
                    preOrderL[j] = preOrder[j + 1];
                    inOrderL[j]  = inOrder[j];
                }
                newTree.SetLeft(BuildTreePre(preOrderL, inOrderL));
            }

            if (i < inOrder.Length - 1)
            {
                string[] preOrderR = new string[preOrder.Length - i - 1];
                string[] inOrderR  = new string[preOrder.Length - i - 1];
                for (int j = i + 1; j < inOrder.Length; j++)
                {
                    preOrderR[j - i - 1] = preOrder[j];
                    inOrderR[j - i - 1]  = inOrder[j];
                }
                newTree.SetRight(BuildTreePre(preOrderR, inOrderR));
            }
            return(newTree);
        }
Exemplo n.º 2
0
        public BinNode <string> ToTree(string[] input)
        {
            // closing if input is empty
            if (input.Length == 0)
            {
                return(null);
            }

            // turning all letters to lower case
            for (int j = 0; j < input.Length - 1; j++)
            {
                input[j] = input[j].ToLower();
            }

            BinNode <string> newTree = new BinNode <string>();
            int parenthesesDepth     = 0;

            int i;

            #region searching operators in order
            for (int round = 0; round < 3; round++)
            {
                for (i = input.Length - 1; i >= 0; i--)
                {
                    // updating how many parentheses are open
                    if (input[i] == ")")
                    {
                        parenthesesDepth += 1;
                    }
                    if (input[i].Contains("("))
                    {
                        parenthesesDepth -= 1;
                    }

                    #region parentheses stuff
                    if (i == 0)
                    {
                        if (parenthesesDepth == 0 && input[input.Length - 1] == ")")
                        {
                            string[] newInput = new string[input.Length - 2];

                            if (input[0] == "(")
                            {
                                for (int j = 0; j < input.Length - 2; j++)
                                {
                                    newInput[j] = input[j + 1];
                                }
                                input = newInput;
                                round = -1;
                                break;
                            }

                            else if (input[0] == "sin(")
                            {
                                for (int j = 0; j < input.Length - 2; j++)
                                {
                                    newInput[j] = input[j + 1];
                                }
                                newTree.SetLeft(ToTree(newInput));
                                newTree.SetValue("sin");
                                return(newTree);
                            }

                            else if (input[0] == "cos(")
                            {
                                for (int j = 0; j < input.Length - 2; j++)
                                {
                                    newInput[j] = input[j + 1];
                                }
                                newTree.SetLeft(ToTree(newInput));
                                newTree.SetValue("cos");
                                return(newTree);
                            }

                            else if (input[0] == "tan(")
                            {
                                for (int j = 0; j < input.Length - 2; j++)
                                {
                                    newInput[j] = input[j + 1];
                                }
                                newTree.SetLeft(ToTree(newInput));
                                newTree.SetValue("tan");
                                return(newTree);
                            }

                            else if (input[0] == "sqrt(")
                            {
                                for (int j = 0; j < input.Length - 2; j++)
                                {
                                    newInput[j] = input[j + 1];
                                }
                                newTree.SetLeft(ToTree(newInput));
                                newTree.SetValue("sqrt");
                                return(newTree);
                            }
                        }
                    }
                    #endregion

                    #region add / sub
                    if ((round == 0 && (input[i] == "+" || input[i] == "-")) && parenthesesDepth == 0)
                    {
                        newTree.SetValue(input[i]);

                        string[] inputL = new string[i];
                        for (int j = 0; j < i; j++)
                        {
                            inputL[j] = input[j];
                        }
                        newTree.SetLeft(ToTree(inputL));

                        string[] inputR = new string[input.Length - i - 1];
                        for (int j = i + 1; j < input.Length; j++)
                        {
                            inputR[j - i - 1] = input[j];
                        }
                        newTree.SetRight(ToTree(inputR));
                        return(newTree);
                    }
                    #endregion

                    #region mult / div
                    if ((round == 1 && (input[i] == "*" || input[i] == "/")) && parenthesesDepth == 0)
                    {
                        newTree.SetValue(input[i]);

                        string[] inputL = new string[i];
                        for (int j = 0; j < i; j++)
                        {
                            inputL[j] = input[j];
                        }
                        newTree.SetLeft(ToTree(inputL));

                        string[] inputR = new string[input.Length - i - 1];
                        for (int j = i + 1; j < input.Length; j++)
                        {
                            inputR[j - i - 1] = input[j];
                        }
                        newTree.SetRight(ToTree(inputR));
                        return(newTree);
                    }
                    #endregion

                    #region power
                    if (round == 2 && input[i] == "^" && parenthesesDepth == 0)
                    {
                        newTree.SetValue("^");

                        string[] inputL = new string[i];
                        for (int j = 0; j < i; j++)
                        {
                            inputL[j] = input[j];
                        }
                        newTree.SetLeft(ToTree(inputL));

                        string[] inputR = new string[input.Length - i - 1];
                        for (int j = i + 1; j < input.Length; j++)
                        {
                            inputR[j - i - 1] = input[j];
                        }
                        newTree.SetRight(ToTree(inputR));
                        return(newTree);
                    }
                    #endregion
                }
            }
            #endregion

            // if no operators were found just return the value
            newTree.SetValue(input[0]);
            return(newTree);
        }