可能的旋转次数。二进制搜索树

问题描述 投票:1回答:3

为什么在二叉搜索树上确实有n-1个可能的旋转?

rotation binary-search-tree
3个回答
2
投票

在二叉树中有n个节点。我们知道节点的顺序不能改变。每个节点都标有数字{1 ... n}。假设n = 4并且树的标签当前根是1.您可以拥有多少其他可能的根?

唯一的选择是2,3,4因此在树上,树只能有N-1个根,只有N-1个独特的旋转。可能不是理论上的解释,但我希望这可以帮助您想象它。


0
投票

旋转只能应用于左边缘或右边缘。在n个节点BST中存在n-1个边缘,因此n-1个旋转是可能的。


0
投票

我认为这个属性可以通过递归来证明。

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
© www.soinside.com 2019 - 2024. All rights reserved.