Question

Why there are exactly n-1 possible rotations on a binary search tree?

Was it helpful?

Solution

In the binary tree there are n nodes. We know that the order of the nodes cannot be changed. Each of the nodes are labelled with a number {1...n}. Lets assume n=4 and the label current root of the tree is 1. how many other possible roots can you have?

The only alternatives are 2,3,4 Therefore on the tree, there are only N-1 more roots the tree can have and only N-1 unique rotations. May not be a theoretical explanation but I hope this helps you visualize it.

OTHER TIPS

rotations can only be applied on left edge or right edge. In n nodes BST there is n-1 edges hence n-1 rotations are possible.

I think this property can be proved by recursion.

For n = 1, the property is true.
assume that this is true for all binary search tree with k nodes such as k <= n
For a bst A with n + 1 nodes:
nbRotation (A) = 2 + nbRotation (left(A)) + nbRotation (right(A))
assume that the size of the left(A) = p and the size of the right(A) = m then 
nbRotation (left(A)) = p-1 and the nbRotation (right(A)) = m-1
So nbRotation(A) = 2 + m-1 + p-1 = m + p = n
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top