Consider a binary tree:
- n is a node, if n is an integer
- (+ a b) is a node, if a and b are nodes.
We have the following three operations:
- (+ a b) -> (+ b a)
- (+ (+ a b) c) -> (+ a (+ b c))
- (+ a (+ b c)) -> (+ (+ a b) c) -- (2. in reverse)
I need an algorithm for giving all the possible permutations of a given tree using these operations. Any operation maybe applied to any subtree. With a permutation I mean any tree that has the exact same set of leaves. It's probably not very difficult, but I just can't seem to figure it out.
[Edit] The leaves can also be names (i.e. variables), so relying on their properties as integers is not an option. The trees do represent sums.
[Edit2] The point of this excercise is to reduce a sum by finding terms of the form A and -A, swizzling the tree to get them into a subtree (+ A -A) in order to eliminate them. The three operations above are axioms in my system and they need to be used all the way, otherwise it's not possible to prove that the reduced tree is equal to the original. Since I'm using Twelf logic programming language, if I can figure out an algorithm to do what I originally asked, the rest follows easily, but alternative solutions are of course welcome.