Compute an AVL Node's Balance Factor
Compute an AVL Node's Balance Factor
Complete balanceFactor(Node node) so the tree can report how lopsided each node is.
Reminder: height and balance factor in an AVL tree. Every node in an AVL tree stores its height (a leaf is 1, an empty child counts as 0). A node's balance factor is height(left) - height(right). In an AVL tree every node's balance factor has to be -1, 0 or 1. A positive number means the left side is taller, a negative number means the right side is taller, and 2 or -2 means the node is out of balance and needs a rotation. This tree does not rotate yet, so you will see those 2s and -2s.
30 left height 2, right height 0: bf = 2
/
20 left height 1, right height 0: bf = 1
/
10 a leaf: bf = 0The provided height(Node) helper already turns a missing child into 0, so the subtraction works even when one or both children are empty.
Requirements:
1. Return 0 when node is null (an empty spot is perfectly balanced).
2. Otherwise return the height of node.left minus the height of node.right, in that order.
3. balanceReport() lists every node in pre-order as its value followed by its balance factor, and rootBalance() returns the root's balance factor. Both already call your method.
The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.
Do NOT write a main method.
Expected Output:
50(bf=1) 30(bf=1) 20(bf=0) 70(bf=0) 1 50(bf=2) 30(bf=2) 20(bf=1) 10(bf=0) 70(bf=0) 2
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...