1

Hello guys, I'm trying to understand how I can see if a binary tree is balanced or not. I tried to print out cout statements in order to try understand it further, but without luck.

The algorithm idea is that it isn't balanced if it returns -1, and is balanced if it returns anything else.

However I don't really grasp how exactly this algorithm is working. But a few things I wonder are;

int checkBalance(node *root)
{
if (root == NULL) return 0;

int left = checkBalance(root->left);
int right = checkBalance(root->right);
 //cout << "Root: " << root->key << " Left: " << left << ", Right: " << right << endl;

if (left == -1 || right == -1) return -1;
if (abs(left-right) > 1) return -1;

if (left > right) return left+1;
return right+1;
}

My points of confusion are with the following lines of code:

  1. if (root == NULL) return 0;
    • What happens when it returns 0, and when does it hit 0? Is it just to prevent the recursion to keep going to unknown memory adresses, or does the return number 0 has any significance?
  2. if (left > right) return left+1;

    • How is left ever bigger than right? As I'm viewing it, it always returns right+1, cause nothing increments 'left' since the condition never is true?
  3. int left = checkBalance(root->left);

    • What does it mean, when you declare an int in this way? Is this the thing which makes left > right?

Thanks for taking your time to read, I have tried to research the problem myself, but I have a hard time understanding how this piece of code works.

Full Code: http://pastebin.com/raw/VaiUNVdJ (case 6 to check, case 1 to add nodes)

3
  • It seems that you have a few misconceptions about the way that this algorithm works. There are certain conditions that define what a balanced binary tree is. Those are the conditions that you are checking at each step of the algorithm. You should read over this post Commented Sep 1, 2016 at 22:54
  • Yeah I do have some misconceptions, I will try to read the post again tommorow. I personally find recursion a bit tricky to understand what really happens sometimes. But I'll do my best to study the link, thanks! Commented Sep 2, 2016 at 0:01
  • To understand recursion, you should practice with some basic algorithms and run them line by line in an IDE like visual studio. That way you can see the memory on the call stack for each recursive call that is made. Over time, you should get better with it. Commented Sep 2, 2016 at 2:05

1 Answer 1

0

This is the basic BBT check. It checks to see whether the subtree is balanced. If not, it returns -1; if so, it returns the depth.

In pseudo-code:

  • If the given subtree is null, return 0 (depth)
  • Recur on both the left and right subtrees (check balance and depth)
  • If either subtree is unbalanced, return -1
  • Compare the left & right depths; if they differ by more than 1, return -1
  • If we got here, this subtree is balanced. Return its depth. This is the larger of the two subtree depths, plus 1.

To address your specific questions:

  1. This algorithm returns the tree depth (-1 if unbalanced). The depth of a null tree is 0.
  2. Left is bigger than right any time the left tree is deeper than the right. Do you still have a problem to see this?
  3. This is how you declare and initialize a variable. It's the same syntax as in left = 0, except that the assignment's RHS is an expression.

Does that explain enough to get you unstuck?


Let's take the node values you gave, using a tree structure with 49 at the root, children 48 and 51, the infix notation could look like:

(48, 49, (50, 51, 52))

In stricter form,

49->left  = 48
49->right = 51
51->left  = 50
51->right = 52
There are no other children

with root = node 49 at the start, we get

int left = checkBalance(root->left);    // returns depth of 1
int right = checkBalance(root->right);  // returns depth of 2

Now, left < right, so the final comparison returns right+1, or 3, which is the correct tree depth.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply! 1. Ok yeah, understand now. 2/3. How though? As my thinking is that int left = checkBalance(root->left); means that 'left' refers to the memory-adress of root->left? In my thinking, then that means that you then compare if the 'memory-adress' of left is bigger than right's 'memory-adress' (left>right) Steer me right: )
Nooo! checkBalance returns the tree depth, not the memory address of the object!
Also, who says that the left subtree is lower in memory than the right? These things come out of the heap.
Reply1: Oh, yeah. True! But I still don't get what happens; Lets say; 48,49,50,51,52 nodes to simplify?; But at what stage is left>right? And how does the code know that? checkBalance as you said returns the tree depth, but I'm not quite sure how that happens. I'm very confused, I'm sorry. Reply2: Yeah, comparing the memory adresses made no sense, I just tried to point out, that I really did not get that part & that was the closest I could get to understand!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.