0

enter image description here

I have this Tree and wonder if this is balanced or not.

From the node 13, it is unbalanced. But other nodes are all balanced since the height difference is not more than 1 or -1.

Then how should I rebalance this tree?

  • 1
    Tree can be rebalanced using rotation. Look at this answer on AVL tree http://stackoverflow.com/a/4219934/558094 – Vinayak Garg Mar 25 '14 at 05:52
  • I know how to rotate and rebalance with coding but confused about what should be the top root for this tree –  Mar 25 '14 at 05:54

1 Answers1

0

The answer is yes. Node 13 has balance factor of -2. So you need to make a left rotation on node 5 and then right rotation on node 13. Like this:

After Left Rotation on Node 5:

                 13 
         |--------|--------|
        10                 17
    |----|     
    5
|---|---|
4       7

After Right Rotation on Node 13:

             10 
    |--------|--------|
    5                 13
|---|---|             |----|     
4       7                  17

And now is balanced.

rareyesdev
  • 1,929
  • 22
  • 39