Example #1
0
 private void RotateLeft()
 {
     //NEED TO COMPLETE
     //rotate left about THIS root
     Left   = new ExpressionTree <T>(Key, Left, Right.Left);
     Key    = Right.Key;
     Right  = Right.Right;
     Height = 1 + Math.Max(Right.Height, Left.Height);
 }
Example #2
0
 private void RotateRight()
 {
     //NEED TO COMPLETE
     //rotate right about THIS root
     Right  = new ExpressionTree <T>(Key, Left.Right, Right);
     Key    = Left.Key;
     Left   = Left.Left;
     Height = 1 + Math.Max(Right.Height, Left.Height);
 }
Example #3
0
 public virtual string Inorder(ExpressionTree <T> tree)
 {
     if (tree == NIL)
     {
         return("");
     }
     try
     {
         string data = tree.Key.ToString();
         return("(" + Inorder(tree.Left) + " " + data + " " + Inorder(tree.Right) + ")");
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error: please reformat your initial expression.");
         return("");
     }
 }
Example #4
0
        private ExpressionTree <string> TreeConversion(string postfix)
        {
            Stack <ExpressionTree <string> > s = new Stack <ExpressionTree <string> >();

            char[]   delims = { ' ' };
            string[] pieces = (postfix).Split(delims, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < pieces.Length; i++)
            {
                if (Regex.IsMatch(pieces[i].ToString(), @"^\d+$")) //if the piece is a number
                {
                    //create a singleton tree and push it onto the stack
                    ExpressionTreeLeaf <string> single = new ExpressionTreeLeaf <string>(pieces[i].ToString());
                    s.Push(single);
                }
                //if the current piece is an operator
                else if (Regex.IsMatch(pieces[i].ToString(), "[+-/*]"))
                {
                    //pop the stack twice
                    if (s.Count() >= 2)
                    {
                        ExpressionTree <string> tree1   = s.Pop();
                        ExpressionTree <string> tree2   = s.Pop();
                        ExpressionTree <string> newTree = new ExpressionTree <string>(pieces[i].ToString(), tree2, tree1);
                        s.Push(newTree);
                    }
                    else
                    {
                        return(emptytree);
                    }
                }
                else
                {
                    continue;
                    //whitespace
                }
            }
            if (s.Count() > 0)
            {
                return(s.Pop());
            }
            return(emptytree);
        }
Example #5
0
        private void EvaluateClick(object sender, EventArgs e)
        {
            string expression = uxInitialExpressionTextBox.Text;

            //check if expression is empty or if there's any symbols, letters, or stuff
            if (expression == "" || RemoveSpaces(expression) == " ")
            {
                MessageBox.Show("Error: Please enter something for the initial expression!");
                //uxInitialExpressionTextBox.Text = ""; //clear out textbox
                return;
            }
            else if (!IsProperFormat(expression))
            {
                MessageBox.Show("Error: Please remove letters and unsupported symbols from the initial expression!");
                //uxInitialExpressionTextBox.Text = ""; //clear out textbox
                return;
            }


            //reformat with some spaces between operators
            expression = AddOperatorSpaces(expression);
            //now it's good

            string InitialType = uxInitialTypeComboBox.Text;

            string postfix;
            ExpressionTree <string> tree = new ExpressionTree <string>();

            switch (InitialType)
            {
            case "Infix":
            {
                postfix = InfixtoPost(expression);
                if (postfix == "")
                {
                    MessageBox.Show("Error: Invalid infix expression.");
                    return;
                }
                else
                {
                    tree = TreeConversion(postfix);
                }
                break;
            }

            case "Prefix":
            {
                postfix = PretoPost(expression);
                if (postfix == "")
                {
                    MessageBox.Show("Error: invalid prefix format.");
                    return;
                }
                else
                {
                    tree = TreeConversion(postfix);
                }
                break;
            }

            case "Postfix":
            {
                tree = TreeConversion(expression);
                break;
            }

            default:
                MessageBox.Show("Error: please enter a valid choice!");
                break;
            }

            //now we have our expression tree
            if (tree == emptytree)
            {
                MessageBox.Show("Error: tree conversion unable to be completed.");
                return;
            }
            else
            {
                if (tree.SolveTree() == 0)
                {
                    MessageBox.Show("Error while evaluating");
                }
                else
                {
                    MessageBox.Show("Answer: " + tree.SolveTree());
                }
            }
        }
Example #6
0
        private void ConvertExpressionClick(object sender, EventArgs e)
        {
            /*
             * Read in expression
             * Convert to postfix
             * Build an expression tree
             */
            string expression = uxInitialExpressionTextBox.Text;

            //check if expression is empty or if there's any symbols, letters, or stuff
            if (expression == "" || RemoveSpaces(expression) == " ")
            {
                MessageBox.Show("Error: Please enter something for the initial expression!");
                //uxInitialExpressionTextBox.Text = ""; //clear out textbox
                return;
            }
            else if (!IsProperFormat(expression))
            {
                MessageBox.Show("Error: Please remove letters and unsupported symbols from the initial expression!");
                //uxInitialExpressionTextBox.Text = ""; //clear out textbox
                return;
            }


            //reformat with some spaces between operators
            expression = AddOperatorSpaces(expression);
            //now it's good

            string InitialType = uxInitialTypeComboBox.Text;
            string ResultType  = uxResultTypeComboBox.Text;

            //find the initialtype selected in the combobox
            switch (InitialType)
            {
            case "Infix":
            {
                string postfix = InfixtoPost(expression);
                if (postfix == "")
                {
                    MessageBox.Show("Error: Invalid infix expression.");
                    return;
                }
                else
                {
                    switch (ResultType)
                    {
                    case "Infix":
                    {
                        //update result box to expression
                        uxResultTextBox.Text = RemoveSpaces(expression);
                        break;
                    }

                    case "Prefix":
                    {
                        ExpressionTree <string> t = TreeConversion(postfix);                //convert to exp. tree
                                                                                            //do a preorder transversal
                        if (t == emptytree)
                        {
                            MessageBox.Show("Error: tree conversion unable to be completed.");
                            return;
                        }
                        else
                        {
                            uxResultTextBox.Text = RemoveSpaces(t.Preorder(t));
                        }
                        break;
                    }

                    case "Postfix":
                    {
                        //convert to postfix, using function
                        //also remove any extra spaces and replace them with " "
                        uxResultTextBox.Text = RemoveSpaces(postfix);
                        break;
                    }

                    default:
                        MessageBox.Show("Error: Invalid Option");
                        break;
                    }
                }
                break;
            }

            case "Prefix":
            {
                string postfix = PretoPost(expression);          //convert to postfix
                if (postfix == "")
                {
                    MessageBox.Show("Error: tree conversion unable to be completed.");
                    return;
                }
                else
                {
                    ExpressionTree <string> t = TreeConversion(postfix);        //convert to exp. tree

                    if (t == emptytree)
                    {
                        MessageBox.Show("Error: tree conversion unable to be completed.");
                        return;
                    }
                    else
                    {
                        switch (ResultType)
                        {
                        case "Infix":
                            //now do inorder transversal
                            uxResultTextBox.Text = RemoveSpaces(t.Inorder(t));
                            break;

                        case "Prefix":
                            //update result box to expression
                            uxResultTextBox.Text = RemoveSpaces(expression);
                            break;

                        case "Postfix":
                            //convert from prefix to postfix
                            uxResultTextBox.Text = RemoveSpaces(postfix);
                            break;

                        default:
                            MessageBox.Show("Error: Invalid Option");
                            break;
                        }
                    }
                }
                break;
            }

            case "Postfix":
            {
                string postfix            = expression;              //should already be in postfix
                ExpressionTree <string> t = TreeConversion(postfix); //convert to exp. tree
                if (t == emptytree)
                {
                    MessageBox.Show("Error: tree conversion unable to be completed.");
                    return;
                }
                else
                {
                    switch (ResultType)
                    {
                    case "Infix":
                        //now do inorder transversal
                        uxResultTextBox.Text = RemoveSpaces(t.Inorder(t));
                        break;

                    case "Prefix":
                        //do a preorder transversal
                        uxResultTextBox.Text = RemoveSpaces(t.Preorder(t));
                        break;

                    case "Postfix":
                        //update result box to expression
                        uxResultTextBox.Text = RemoveSpaces(expression);
                        break;

                    default:
                        MessageBox.Show("Error: Invalid Option");
                        break;
                    }
                }
                break;
            }

            default:
                MessageBox.Show("Error: Invalid Choice");
                break;
            }
        }
 public override string Preorder(ExpressionTree <T> tree)
 {
     return(_leafdata);
 }