private void ReplaceNodeInTree(Expression tree, Expression rem, Expression add)
 {
     Expression[] treeChildren = tree.GetChildren();
     for (int i = 0; i < treeChildren.Length; i++)
     {
         if (treeChildren[i] == rem)
         {
             treeChildren[i] = add;
         }
         else
         {
             ReplaceNodeInTree(treeChildren[i], rem, add);
         }
     }
 }
 private Expression DeepestReducableNode(Expression node)
 {
     if (node.IsLeaf())
     {
         return node;
     }
     else
     {
         Expression[] myChildren = node.GetChildren();
         Expression firstHighestPrec = null;
         bool allLeafNode = true;
         for (int i = 0; i < myChildren.Length; i++)
         {
             if (!myChildren[i].IsLeaf())
             {
                 allLeafNode = false;
             }
             Expression current = DeepestReducableNode(myChildren[i]);
             if (firstHighestPrec == null || firstHighestPrec.GetPrecedence() < myChildren[i].
                 GetPrecedence())
             {
                 firstHighestPrec = current;
             }
         }
         if (firstHighestPrec == null || allLeafNode)
         {
             return node;
         }
         else
         {
             return firstHighestPrec;
         }
     }
 }