为 AVL 进行轮换

发布于 2024-09-14 06:10:38 字数 912 浏览 1 评论 0原文

我正在尝试实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文