Added Readable string function

Apr 23, 2012 at 3:41 AM

I had a little trouble with the reverse polish notation so I created this additional function to print the tree.  Probably needs some cleanup :-)

 

 

 

internal void AddReadableString(StringBuilder stringBuilder, string

indent)

{

 

if

(Operator.Arity == 0)

{

stringBuilder.Append(Operator.Name);

}

 

else

{

stringBuilder.Append(

"("

);

Children[0].AddReadableString(stringBuilder, indent);

stringBuilder.Append(Operator.Name);

Children[1].AddReadableString(stringBuilder, indent);

stringBuilder.Append(

")"

);

}

}

Apr 23, 2012 at 4:58 AM
Edited Apr 23, 2012 at 7:24 PM

This ones a little better, to handle unary operators:

 

 

 

 

        internal void AddReadableString(StringBuilder stringBuilder, string indent)
        {
            if (Operator.Arity == 0)
            {
                stringBuilder.Append(Operator.Name);
            }
 
 
            else
            {
                if (Operator.Arity == 1)
                {
                    stringBuilder.Append(Operator.Name);
                }
 
                stringBuilder.Append("(");
 
                Children[0].AddReadableString(stringBuilder, indent);
 
 
                if (Operator.Arity > 1)
                {
                    stringBuilder.Append(Operator.Name);
 
                    Children[1].AddReadableString(stringBuilder, indent);
                }
 
                stringBuilder.Append(")");
            }
        }

 

Apr 23, 2012 at 7:23 PM

And here is the ToReadableString call of Individual that calls the AddReadableString in OperatorNode:

 

       public string ToReadableString()
        {
            var stringBuilder = new StringBuilder();
            OperatorTree.AddReadableString(stringBuilder, " ");
            return stringBuilder.ToString();
        }