Beispiel #1
0
 public BinaryNode(T element, BinaryNode <T> leftNode, BinaryNode <T> rightNode)
 {
     item  = element;
     left  = leftNode;
     right = rightNode;
 }
        private double EvalTest(BinaryNode <char> node)
        {
            double value = 0;

            return(value);
        }
        public BinaryNode <char> ParseWhile(Queue <char> expressionQueue)
        {
            Stack <BinaryNode <char> > nodeStack   = new Stack <BinaryNode <char> >();
            BinaryNode <char>          currentNode = null;
            BinaryNode <char>          childNode   = null;
            BinaryNode <char>          rootNode    = null;

            while (expressionQueue.Count > 0)
            {
                char token = expressionQueue.Dequeue();
                switch (token)
                {
                case ('('):
                    if (currentNode != null)
                    {
                        //currentNode.push
                        nodeStack.Push(currentNode);
                    }
                    //current node = new node
                    currentNode = new BinaryNode <char>();
                    break;

                case (')'):
                    if (nodeStack.Count > 0)
                    {
                        childNode   = currentNode;
                        currentNode = nodeStack.Pop();
                        if (currentNode.left == null)
                        {
                            currentNode.left = childNode;
                        }
                        else if (currentNode.right == null)
                        {
                            currentNode.right = childNode;
                        }
                        else
                        {
                            Console.WriteLine("Invalid Expression");
                        }
                    }
                    break;

                case ('+'):
                case ('*'):
                case ('-'):
                case ('/'):
                case ('^'):
                    currentNode.item = token;
                    break;

                default:
                    BinaryNode <char> newLeafNode = new BinaryNode <char>();
                    newLeafNode.item = token;

                    if (currentNode.left == null)
                    {
                        currentNode.left = newLeafNode;
                    }
                    else if (currentNode.right == null)
                    {
                        currentNode.right = newLeafNode;
                    }
                    else
                    {
                        Console.WriteLine("Invalid Expression!!");
                    }
                    break;
                } //endSwitch
            }     //endWhile
            rootNode = currentNode;
            return(rootNode);
        }
Beispiel #4
0
 public BinaryExpressionTree(String expression)
 {
     origExpression = expression;
     rootNode       = Parse(ref expression);
 }