Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:50:02+00:00 2026-06-10T00:50:02+00:00

I am working on translating an expression tree to a format that resembles infix

  • 0

I am working on translating an expression tree to a format that resembles infix notation; I am not evaluating the tree or executing its operations. The tree contains both logical and relational operations, and I would like to emit parentheses in an intelligent manner during the translation.

To illustrate, consider the following contrived expression:

a < x & (a < y | a == c) & a != d

If I walk the expression tree produced by this expression in-order, then I will print out the following expression, which is incorrect.

a < x & a < y | a == c & a != d
// equivalent to (a < x & a < y) | (a == c & a != d)

Alternatively, I can again perform an in-order traversal but emit parentheses before and after processing a binary expression. This will produce the following correct expression, but with several redundant parentheses.

(((a < x) & ((a < y) | (a == c))) & (a != d))

Is there an expression tree traversal algorithm that produces optimally-parenthesized expressions?

For reference, here is a snippet of the ExpressionVisitor I am using to inspect the tree.

class MyVisitor : ExpressionVisitor
{
    protected override Expression VisitBinary(BinaryExpression node)
    {
        Console.Write("(");

        Visit(node.Left);
        Console.WriteLine(node.NodeType.ToString());
        Visit(node.Right);

        Console.Write(")");

        return node;
    }

    // VisitConstant, VisitMember, and VisitParameter omitted for brevity.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:50:05+00:00Added an answer on June 10, 2026 at 12:50 am

    Try something like this, assuming that node.NodeType is of type NodeType, and that function Precedes exists and returns true if first parameter precedes the second.

    protected override Expression Visit(BinaryExpression node, NodeType parentType)
    {
        bool useParenthesis = Precedes(parentType, node.NodeType);
    
        if (useParenthesis)
            Console.Write("(");
    
        Visit(node.Left, node.NodeType);
        Console.WriteLine(node.NodeType.ToString());
        Visit(node.Right, node.NodeType);
    
        if (useParenthesis)
            Console.Write(")");
    
        return node;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble translating my working C# regular expression into JavaScript's regular expression
I'm working on translating some php code to Python. I'm not an expert in
The project that I'm currently working on involves translating a website that was written
I'm working on translating expression trees and I am wondering if there are existing
I am working on translating our django webapp. I'd like to test that our
So, I'm working on translating my C++ app into multiple languages. What I'm currently
Working with an undisclosed API, I found a function that can set the number
I am working on internationalizing a project that has a custom date picker. The
I have a simple translating program i working on in vb 2010. I have
I'm working on a homework assignment translating a C program we wrote to MIPS.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.