How to Balance an Imbalanced Syntax Tree?

  • Context: MHB 
  • Thread starter Thread starter lyd123
  • Start date Start date
  • Tags Tags
    Tree
Click For Summary
SUMMARY

This discussion focuses on balancing an imbalanced syntax tree, specifically addressing the transformations required to create a complete tree. A complete tree is defined by the condition that the height difference between the shortest and tallest subtrees must not exceed one. The conversation highlights that traditional Binary Search Tree (BST) transformations are inadequate due to the unique properties of operators, particularly the non-commutative nature of subtraction. The proposed solution involves treating subtrees with subtraction as whole units during balancing operations, utilizing the commutativity and associativity of addition.

PREREQUISITES
  • Understanding of syntax trees and their properties
  • Familiarity with Binary Search Trees (BST) and their transformations
  • Knowledge of operator properties, specifically commutativity and associativity
  • Basic programming skills for implementing tree transformations
NEXT STEPS
  • Research tree balancing algorithms, focusing on AVL and Red-Black trees
  • Learn about operator precedence and its impact on tree structures
  • Explore code implementations for balancing syntax trees in programming languages like Python or Java
  • Investigate the implications of non-commutative operations in data structures
USEFUL FOR

Software developers, computer science students, and anyone involved in algorithm design or tree data structure optimization will benefit from this discussion.

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: 174
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.
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
7K