为 AVL 进行轮换
我正在尝试实现 AVL 树用于教育目的,但轮换并没有像我预期的那样工作。
我有一些节点,每个节点都有一个指向左节点、右节点和父节点的指针。
下面是我的右向右旋转的代码。首先是输入(只是为了让我说清楚,这就是我所理解的 RR 情况)
a
\
b
\
c
进行轮换的代码是
if (subTreeRoot == root) {
this->root=subTreeRoot->right;
}
else { // not resetting at root
subTreeRoot->right->myParent = subTreeRoot->myParent;
subTreeRoot->myParent->right = subTreeRoot->right;
}
subTreeRoot->right->left = subTreeRoot;
subTreeRoot->right = NULL;
subTreeRoot->height = 1;
return;
这实际上是有效的。 如果 a 不是根,它甚至可以工作。
失败的测试用例是 类似的东西 dcbae
在这种情况下,我进行旋转,然后过来并粘贴其他东西。
我要回去看看
http://www.cse.ohio-state.edu/~sgomori/570/avlrotations.html
但我想首先确保我不仅仅是几乎没有,否则就很远了。
I am trying to implement an AVL tree for educational purposes but the rotation is not working like I expected.
I have nodes which each have a pointer to a left, right and parent node.
Below is my code for the right-right rotation. First the imput (Just so I make it clear, this is what I understand to be the RR case)
a
\
b
\
c
Code to do rotation is
if (subTreeRoot == root) {
this->root=subTreeRoot->right;
}
else { // not resetting at root
subTreeRoot->right->myParent = subTreeRoot->myParent;
subTreeRoot->myParent->right = subTreeRoot->right;
}
subTreeRoot->right->left = subTreeRoot;
subTreeRoot->right = NULL;
subTreeRoot->height = 1;
return;
This actually works.
It even works if a is not the root.
The test case that is failing is
something like
d c b a e
In this case I do the rotate and then come along and stick something else in.
I was going to go back and look at
http://www.cse.ohio-state.edu/~sgomori/570/avlrotations.html
But I wanted to make sure first I wasn't just barely off or else way to far off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论