两个二叉树之间的旋转距离

时间:2016-10-23 19:47:58

标签: binary-tree binary-search-tree tree-rotation

我有以下二叉树,我正在尝试使用最少数量的树旋转将其转换为目标二叉树(帖子中的第二个树)。这棵树的理论最小旋转数是5,但是,我能算出的最小值是6次旋转,我也复制了旋转,我缺少什么?

树: 1 \ \ 3 / \ / \ 2 5 / \ / \ 4 7 / \ / \ 6 11 / \ / \ 9 12 / \ 8 10

目标树 1 \ \ 11 / \ / \ 9 12 / \ / \ 3 10 / \ / \ 2 5 / \ / \ 4 7 / \ 6 8

到目前为止我尝试过的旋转(所有旋转都需要6次旋转):

Order1: Rotate left with root 7 and pivot 11 Rotate left with root 5 and pivot 11 Rotate left with root 3 and pivot 11 Rotate left with root 7 and pivot 9 Rotate left with root 5 and pivot 9 Rotate left with root 3 and pivot 9 Order2: Rotate left with root 7 and pivot 11 Rotate left with root 5 and pivot 11 Rotate left with root 7 and pivot 9 Rotate left with root 3 and pivot 11 Rotate left with root 5 and pivot 9 Rotate left with root 3 and pivot 9

Order3: Rotate left with root 7 and pivot 11 Rotate left with root 5 and pivot 11 Rotate left with root 7 and pivot 9 Rotate left with root 5 and pivot 9 Rotate left with root 3 and pivot 11 Rotate left with root 3 and pivot 9

Order4: Rotate left with root 7 and pivot 11 Rotate left with root 7 and pivot 9 Rotate left with root 5 and pivot 11 Rotate left with root 3 and pivot 11 Rotate left with root 5 and pivot 9 Rotate left with root 3 and pivot 9

Order5: Rotate left with root 7 and pivot 11 Rotate left with root 7 and pivot 9 Rotate left with root 5 and pivot 11 Rotate left with root 5 and pivot 9 Rotate left with root 3 and pivot 11 Rotate left with root 3 and pivot 9

1 个答案:

答案 0 :(得分:1)

用根11和枢轴9向右旋转 向左旋转,带根部7和枢轴9
用根5和枢轴9左旋转 向左旋转,用根3和枢轴9
向左旋转,用根9和枢轴11