MHB How to Balance an Imbalanced Syntax Tree?

  • Thread starter Thread starter lyd123
  • Start date Start date
  • Tags Tags
    Tree
AI Thread Summary
To balance an imbalanced syntax tree, the first step is to identify the necessary transformations to achieve a complete tree, defined by the condition that the height difference between the shortest and tallest subtrees is no greater than one. Unlike traditional binary search trees, where the order of numbers is crucial, this approach allows for more flexibility, except when dealing with subtraction, which is neither commutative nor associative. The transformation process involves treating subtrees with subtraction as fixed units while rearranging other components based on the properties of addition. A practical example illustrates how to balance the tree by substituting and rearranging terms while maintaining the integrity of the subtraction operation. Ultimately, the challenge lies in developing a systematic method for applying these transformations in code for any imbalanced syntax tree.
lyd123
Messages
11
Reaction score
0
The question asks you to balance a syntax tree. The first part is to describe the tree transformations needed to make it a complete tree. I've attached an example of the transformation.

We will know its a compete tree if (heightOfShortestSubTree -heightOfTallestSubTree<=1).

Normal transformations of BSTs which I know of, won't be fully right because those try to keep the order of numbers correct i.e smaller numbers on the left, bigger on the right. But in this question, order of numbers doesn't matter, except if the operator is a minus. If a node has a 'minus', then that sub-tree's structure can't be changed, because subtraction isn't associative or commutative. How would I approach this question? Thank you!

View attachment 8739
 

Attachments

  • Screenshot (3).png
    Screenshot (3).png
    27.6 KB · Views: 157
Technology news on Phys.org
Hi lyd123,

The subtraction itself is indeed neither commutative nor associative.
But the addition above it is.
That is, we need to treat the subtree with the subtraction as a whole, and swap it with the left hand argument of the addition.

Let me show you. We have:
$$(10 + (40-30)) + 20$$
Suppose we temporarily substitute $A$ for $(40-30)$.
Then we get:
$$(10 + A) + 20$$
Now we can balance it with commutativity and associativity of addition:
$$(10 + A) + 20 = (A + 10) + 20 = A + (10 + 20)$$
Therefore:
$$(10 + (40-30)) + 20= (40-30) + (10 + 20)$$
 
Hi, thank you Klaas, but I am finding it hard to come up with the steps needed that would work for any imbalanced syntax tree. I think all the rotations would be based off the operator in each node, and we have try to keep a node at each level. But how would this would in code e.g for normal BSTs rotation maybe something the left child of node X becomes the left child of the other node at the same level as node X.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top