Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permutations of a binary tree

Consider a binary tree:

  1. n is a node, if n is an integer
  2. (+ a b) is a node, if a and b are nodes.

We have the following three operations:

  1. (+ a b) -> (+ b a)
  2. (+ (+ a b) c) -> (+ a (+ b c))
  3. (+ 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.

like image 651
TrayMan Avatar asked Mar 16 '09 18:03

TrayMan


People also ask

What is a permutation tree?

Definitions. A permutation tree is a labeled rooted tree that has vertex set {0,1,2,..,n} and root 0, and in which each child is larger than its parent and the children are in ascending order from the left to the right. The power of a permutation tree is the number of descendants of the root.

How many ways can a binary search tree?

A BST can be traversed through three basic algorithms: inorder, preorder, and postorder tree walks. Inorder tree walk: Nodes from the left subtree get visited first, followed by the root node and right subtree.

How many binary trees are possible with 5 nodes?

For n = 5 --> 42 Binary Search Trees are possible.

How many binary trees are possible with 3 keys?

In this program, we need to find out the total number of binary search trees can be constructed with n values. Below diagram shows a possible binary search tree with the key value as 3. So, we can construct a total of five binary search trees.


1 Answers

Seems like the most straightforward solution would be a depth-first traversal of your tree to collect all of the nodes into a list, generate all of the permutations of list, dump each permutation into binary tree.

So, given the list (+ a (+ b c) ), we have the nodes [a; b; c], which have the following permutations:

[a; b; c]
[a; c; b]
[b; a; c]
[b; c; a]
[c; a; b]
[c; b; a]

The first item in the list is your head, the following two items are the child nodes, the next four items are the child-child nodes, and so on.

The complexity of this increases dramatically if you need produce a list of all possible trees, rather than just balanced ones. In that case, you'd need to group them like this:

[a; b; c; d]
[(a; b); c; d]
[(a; b; c); d]
[a; (b; c); d]
[a; (b; c; d)]
[a; b; (c; d)]
[a; b; d; c]
// first permutation
[(a; b); d; c]
[(a; b; d); c]
...

Where each n-tuple is a set of nodes. For more than a few nodes, the universe will experience a heat-death before your algorithm ever completed.

like image 66
Juliet Avatar answered Sep 17 '22 12:09

Juliet