Enumerating all expression trees (not Catalan)
- From: Tegiri Nenashi <tegirinenashi@xxxxxxxxx>
- Date: Tue, 15 Sep 2009 21:53:01 -0700 (PDT)
I'm writing a program that generates all expressions with binary and
unary operations. E.g. -((x + y) * z). Before worrying about
variables and operations, I wonder how to generate plain tree
structures. The mapping that would enumerate each tree with unique
integer would be ideal for my purposes. (Curiously, such mapping in
simple cases of binary trees or other Catalan structures is appeared
to be not an easy matter to google!)
Here is one such mapping. Assign 2 to a node with two children, 1 to
the node with one child, 0 to the leaf. Write down expression in
polish notation, and interpret it as a ternary number, e.g.
221000
corresponds to the tree or expression like this
(((-x)+y)+z)
However, this mapping is not a bijection, for example the code
22100
doesn't correspond to any tree. This might be a big problem for my
program if asymptotically the mapping is almost empty, then the run
time would be wasted wading through the list of mostly useless codes.
Any guess for asymptotic, or suggestion for better encoding?
.
- Follow-Ups:
- Re: Enumerating all expression trees (not Catalan)
- From: Tegiri Nenashi
- Re: Enumerating all expression trees (not Catalan)
- From: cbrown@xxxxxxxxxxxxxxxxx
- Re: Enumerating all expression trees (not Catalan)
- Prev by Date: Re: PRIVATE NET : 'it', 'it', 'They', 'This' . Who Give's a ***?.
- Next by Date: diff eq. question
- Previous by thread: Re: PRIVATE NET Surpasses Google in India.
- Next by thread: Re: Enumerating all expression trees (not Catalan)
- Index(es):
Loading