Understanding Balance Factors/Node Height for AVL rotations

So I am having a hard time understanding how to balance AVL trees. I understand the rotation stuff, but I can't figure out how to find the balance factor of node heights, for example: https://i.stack.imgur.com/UuK5f.png

So I am having a hard time understanding how to balance AVL trees. I understand the rotation stuff, but I can't figure out how to find the balance factor of node heights, for example:

https://i.stack.imgur.com/UuK5f.png

Can anyone explain to me how we actually find the balance factor for EACH node?

I know if the height of [(left subtree) - (right subtree)] is not within (-1<= x <= 1) then its unbalanced...but I can't figure how to find "x".

(edit: I don't need code, I just want to understand how to find BF).

4 Answers

If I understand your question correctly, you want to know how to maintain the balance-factor of a given node. The best way to keep the track is when doing an "insert" in the tree. This is because you know whether your new node is going to the left or the right side of the "current" node. When it goes to the left, you decrement the balance and when it goes to the right, you increment the balance. The idea is to have the tree already balanced before you exit the "insert" method.

Another approach I have seen is NOT doing the balancing during the "insert". You simply insert like you would in BST. After the insert, you begin your balancing act where you get the left subtree and right subtree height at each node and start shuffling pointers. This is not a good approach since you incur O(logn) for every node in finding the height and since you would do that for every node, it results into n*O(logn).

I am posting the "insert" method that I once wrote that keeps the tree balanced at every insert and does NOT compute the height separately at any point during the insert. See if it helps:

Node* AVL::insert(Node* node, int key, int value) { if(node == NULL) node = new Node(key, value); else if (key <= node->key) { node->balance--; node->left = insert(node->left, key, value); } else if (key > node->key) { node->balance++; node->right = insert(node->right, key, value); } // Right Tree Heavy if (node->balance == 2) { // Left tree unbalanced if (node->right && node->right->balance == 1) { // LL node = singleLeftRotation(node); node->balance = 0; node->left->balance = 0; } if (node->right && node->right->balance == -1) { // LR int nodeRLBalance = node->right->left->balance; node = doubleLeftRotation(node); node->balance = 0; node->left->balance = nodeRLBalance == 1 ? -1 : 0; node->right->balance = nodeRLBalance == -1 ? 1 : 0; } } // Left Tree Heavy if (node->balance == -2) { // Right tree unbalanced if (node->left && node->left->balance == -1) { // RR node = singleRightRotation(node); node->balance = 0; node->right->balance = 0; } if (node->left && node->left->balance == 1) { // RL int nodeLRBalance = node->left->right->balance; node = doubleRightRotation(node); node->balance = 0; node->left->balance = nodeLRBalance == 1 ? -1 : 0; node->right->balance = nodeLRBalance == -1 ? 1 : 0; } } return node; } 
3

The balance factor of a node is the difference of the height of it's left and right descendant nodes.

The balance factor is defined by some as: balance = node.left.height - node.right.height

and by other as: balance = node.right.height - node.left.height

so if you want to understand the balance of some nodes in a graph you have to know how they define the balance factor.

For example wikipedia defines it as node.left.height - node.right.height and you can see that the formula results matches the node balance factors Avl Tree Balance of Nodes Example

X is the height of the left subtree - the height of the right subtree.

If the left subtree has a max height of three and the right subtree has a max height of two, then the balance factor would be

3 - 2 = Balance Factor of 1 (Left Heavy) 

The other way around:

2 - 3 = Balance Factor of -1 (Right Heavy) 

If both are the same:

3 - 3 = Balance Factor of 0 (Even Heavy) 

A tree becomes unbalanced when the difference between the heights is greater than 1 or less than -1.

5 - 3 = Balance Factor of 2 (UNBALANCED!) 3 - 5 = Balance Factor of -2 (UNBALANCED!) 

this image will probably help you. Unbalanced AVL tree has a violation in the E-node

unbalanced AVL tree

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaHJqYWaDen6OrqWdnaKowaK6w6KloGWSlrmiusKeZJ%2BZk6m8s7%2BMp6adnV2dsqqzx61kn6eiYq63uIyrpq2ZpJ68r78%3D

 Share!