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: 158
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top