Any good algorithm to balanced a strict binary tree for a ordered set

Hi,

I have created a syntax tree of a binary operator that is associative but not commutative. The resultant is a unbalanced strict binary tree.

I have tried using AVL tree as well as a heap. But they will occasionally destruct the non-commutative property of the syntax tree.

Is there any efficient algorithm that I can used to balance this tree?

Thank you.

[390 byte] By [Chiew_Boona] at [2007-10-2 16:00:15]
# 1
Is linear time good enough?1. Traverse your tree to build a list.2. Recursively turn the list into a tree. Pseudocode for this step: fun listToTree( [a] ) = Leaf(a) | listToTree( xs ) = Node( listToTree(firstHalf(xs)), listToTree(secondHalf(xs)) );
YAT_Archivista at 2007-7-13 16:27:46 > top of Java-index,Other Topics,Algorithms...
# 2
Hi, Thanks for your reply. Is it possible to have the tree balanced while I'm building it in the very first time rather than having to balanced it after I created the original tree?As i believe that this approach will be better than a linear time.Thank you.
Chiew_Boona at 2007-7-13 16:27:46 > top of Java-index,Other Topics,Algorithms...