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 7429331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:57:13+00:00 2026-05-29T08:57:13+00:00

I need a tree structure that supports and and oring. For example, given a

  • 0

I need a tree structure that supports “and” and “or”ing. For example, given a regular expression like ab|c(d|e) I want to turn that into a tree.

So, at first we have two “or” branches… it can either go down ab, or c(d|e). If you head down the ab branch, you get two nodes, a and b (or a followed by b, whatever). Then if you go down the c(d|e) branch, you get c and (d|e), then (d|e) is split into d or e.

Making a tree structure is easy, you just have something like

class Node {
    string element;
    Node[] children;
}

But then how do you know if the children should be “anded” or “ored”? I guess each level of the tree should alternate between “anding” and “oring”

Does that make sense? Can anyone suggest a structure for this?


A few people have suggested storing the “operator” on the node, which is fine, but isn’t there a way to take advantage of the fact that each level always alternates or,and,or,and,…?

Edit: Not quite sure why people keep assuming this is a binary tree. It’s not. I was hoping the tiny code snippet would tip you off. The example just happens to have only 2 branches.


Currently leaning towards this:

abstract class Node { }

class DataNode : Node
{
    string data;
}

abstract class OpNode : Node
{
    Node[] children;
}

class OrNode : OpNode { }
class AndNode : OpNode { }
  • 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-05-29T08:57:14+00:00Added an answer on May 29, 2026 at 8:57 am

    Think of a tree structure where every node represents a boolean expression that can be evaluated to be either true or false – in your case a regular expression (match or non-match).
    The tree structure itself represents AND and OR:
    Each route, starting at the root node and ending with a node that has no further children, is a conjunction of expressions, representing AND.
    The tree

        A
       /
      B
     /
    C
    

    would represent A AND B AND C.

    Whenever a node has more than 1 child node, there is an OR (disjunction), branching into several routes:

        A
       / \
      B   D
     /
    C
    

    represents A AND ((B AND C) OR D)

    So you do not even need to store the operators anywhere.

    In your example you have the expression ab|c(d|e) so there is no common root expression to evaluate; I suggest the root in this case is simply true and the tree would look like:

       true
       / \
      A   C
     /   / \
    B   D   E
    

    For a custom tree class in c# look here Tree data structure in C# or search on or make one of your own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to get tree like structure from a given text document using Java.
I need a control that behaves like a treeview (binds to a tree structure,
I need to create MySQL tables that represent a tree structure like this: Root
I need to create an XML schema that validates a tree structure of an
I need to be able to make a tree like structure in the appengine
Given a table that represents a hierarchical tree structure and has three columns ID
I've got a tree structure that I need to rearrange (drag and drop) and
One of my entity managed objects need to setup a runtime-only tree-like structure where
I have a tree structure that represents an org chart and I need to
I need to store a tree data structure in my database, for which I

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.