AVL树平衡

发布于 2024-11-15 03:43:27 字数 658 浏览 3 评论 0原文

给定下面的 AVL 树:

        23
       /    \     
   19        35
  /  \      /  \     
 8   20   27    40
               /
             38
             /
            36

只在 40 度向右旋转一次可以吗?使它像这样:

        23
      /    \     
   19        35
  /  \      /  \     
 8   20   27    38
               /  \
             36    40

它仍然符合 AVL 属性,即与左子树相比具有 -+1 高度。

在答案中,它进行了双重旋转,因此上面 35 处的子树将如下所示:

        23
      /    \     
   19        38
  /  \      /  \     
 8   20   35    40
         /  \
        27  36    

我不明白何时进行双重旋转以及何时进行单次旋转(如果它们都不违反 height 属性)。

Given an AVL tree below:

        23
       /    \     
   19        35
  /  \      /  \     
 8   20   27    40
               /
             38
             /
            36

Is it ok to just do a single rotation at 40, to the right? Making it something like this:

        23
      /    \     
   19        35
  /  \      /  \     
 8   20   27    38
               /  \
             36    40

It still conforms tot he AVL property of having -+1 height compared to the left subtree.

In the answer it does a double rotation so the subtree at 35 above would look like this after:

        23
      /    \     
   19        38
  /  \      /  \     
 8   20   35    40
         /  \
        27  36    

I don't understand when to do a double rotation and when to do a single rotation if they both do not violate the height property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-11-22 03:43:27

双旋转可能是由于使用了特定的 AVL 算法所致。对我来说,这两个答案看起来都是有效的 AVL 树。

The double rotation may be due to a specific AVL algorithm in use. Both answers look like valid AVL trees to me.

り繁华旳梦境 2024-11-22 03:43:27

如果原始问题仅给出不平衡 AVL 树(而不是添加或删除节点之前的平衡树),则单次旋转是有效答案。

如果问题提供了添加或删除节点之前的 AVL 树,则重新平衡算法可能会导致发生双重旋转。

If the original question was given with only the unbalanced AVL tree (and not the balanced tree before a node was added or removed), then the single rotation is a valid answer.

If the question provides the AVL tree before and after a node was added or removed, then the algorithm for rebalancing could result in the double rotation occurring.

四叶草在未来唯美盛开 2024-11-22 03:43:27

两个答案都是正确的,尽管根据我使用的文献:

有四种类型的旋转 LL、RR、LR 和 RL。 这些轮换是
以插入节点 N 的最近祖先 A 为特征,其
平衡因子变为2。

获得了以下旋转类型特征:

  1. LL:N插入到A的左子树的左子树中
  2. LR:N插入A的左子树的右子树
  3. RR:N插入A的右子树的右子树
  4. RL:N插入A的右子树的左子树

根据这些规则,平衡因子变为 2 的最近祖先是40在你的例如,插入是在 40 的左子树的左子树中进行的,因此您必须执行 LL 旋转。遵循这些规则,您的第一个答案将是所选的操作。

尽管如此,这两个答案都是正确的,并且取决于您使用的算法及其遵循的规则。

Both answers are right, though according to the literature that I use:

The are four types of rotations LL, RR,LR and RL. These rotations are
characterized by the nearest ancestor A, of the inserted node N, whose
balance factor becomes 2.

The following characterization of rotation types is obtained:

  1. LL: N is inserted in the left subtree of the left subtree of A
  2. LR: N is inserted in the right subtree of the left subtree of A
  3. RR: N is inserted in the right subtree of the right subtree of A
  4. RL: N is inserted in the left subtree of the right subtree of A

According to these rules, the nearest ancestor whose balance factor becomes 2 is 40 in your example, and the insertion was made in the left subtree of the left subtree of 40 so you have to perform an LL rotation. Following these rules, the first of your answers will be the chosen operation.

Still, both answers are correct and depends of the algorithm you are using and the rules it follows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文