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 { }