Example #1
0
        /// <summary>
        /// Balances the binary trees within the expression.
        /// </summary>
        /// <returns>The balanced expression.</returns>
        public IExpression Balance()
        {
            if (_balanced)
            {
                return(this);
            }

            if (!BalanceOps.TryGetValue(Operator, out CompoundOperator inverseOp))
            {
                return(new CompoundExpression(Operator, First.Balance(), Second.Balance(), true));
            }

            List <IExpression> chain = new List <IExpression>();

            GetChainedValues(this, chain);

            return(Rebuild(chain, Operator, inverseOp));
        }